Submission #1356130


Source Code Expand

// Copyright (C) 2017 __debug.

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; version 3

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; If not, see <http://www.gnu.org/licenses/>.


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/priority_queue.hpp>

#define x first
#define y second
#define MP std::make_pair
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()
#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#ifdef __linux__
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif

using std::pair;
using std::vector;
using std::string;

typedef long long LL;
typedef pair<int, int> Pii;

const int oo = 0x3f3f3f3f;

template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, true : false; }
template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, true : false; }
string procStatus()
{
    std::ifstream t("/proc/self/status");
    return string(std::istreambuf_iterator<char>(t), std::istreambuf_iterator<char>());
}
template<typename T> T read(T &x)
{
    int f = 1;
    char ch = getchar();
    for (; !isdigit(ch); ch = getchar())
        f = (ch == '-' ? -1 : 1);
    for (x = 0; isdigit(ch); ch = getchar())
        x = 10 * x + ch - '0';
    return x *= f;
}
template<typename T> void write(T x)
{
    if (x == 0) {
        putchar('0');
        return;
    }
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    static char s[20];
    int top = 0;
    for (; x; x /= 10)
        s[++top] = x % 10 + '0';
    while (top)
        putchar(s[top--]);
}
// EOT

const int MAXM = 1e5 + 5;
const int MOD = 1e9 + 7;

struct Matrix {
    static const int SIZE = 3;
    int n, m;
    int a[SIZE][SIZE];

    Matrix() { }
    Matrix(int n_, int m_): n(n_), m(m_)
    {
        memset(a, 0, sizeof(a));
    }
    friend Matrix operator+ (const Matrix &a, const Matrix &b)
    {
        assert(a.n == b.n && a.m == b.m);
        Matrix ret(a.n, a.m);
        for (int i = 0; i < a.n; ++i) {
            for (int j = 0; j < a.m; ++j) {
                ret.a[i][j] = (a.a[i][j] + b.a[i][j]) % MOD;
            }
        }
        return ret;
    }
    friend Matrix operator- (const Matrix &a, const Matrix &b)
    {
        assert(a.n == b.n && a.m == b.m);
        Matrix ret(a.n, b.m);
        for (int i = 0; i < a.n; ++i) {
            for (int j = 0; j < a.m; ++j) {
                ret.a[i][j] = (a.a[i][j] - b.a[i][j] + MOD) % MOD;
            }
        }
        return ret;
    }
    friend Matrix operator* (const Matrix &a, int b)
    {
        Matrix ret(a.n, a.m);
        for (int i = 0; i < a.n; ++i) {
            for (int j = 0; j < a.m; ++j) {
                ret.a[i][j] = (LL)a.a[i][j] * b % MOD;
            }
        }
        return ret;
    }
    friend Matrix operator* (const Matrix &a, const Matrix &b)
    {
        assert(a.m == b.n);
        Matrix ret(a.n, b.m);
        for (int i = 0; i < a.n; ++i) {
            for (int j = 0; j < b.m; ++j) {
                for (int k = 0; k < a.m; ++k) {
                    ret.a[i][j] = (ret.a[i][j] + (LL)a.a[i][k] * b.a[k][j]) % MOD;
                }
            }
        }
        return ret;
    }
};

Matrix fpm(Matrix base, int exp)
{
    assert(base.n == base.m);
    Matrix ret(base.n, base.n);
    for (int i = 0; i < base.n; ++i) {
        ret.a[i][i] = 1;
    }
    for (; exp; exp >>= 1) {
        if (exp & 1)
            ret = ret * base;
        base = base * base;
    }
    return ret;
}

int N, M;
int A[MAXM];

void input()
{
    read(N); read(M);
    for (int i = 1; i <= M; ++i) {
        read(A[i]);
    }
}

const int TRANS[3][3] = {
    {4, MOD - 2, 1},
    {1, 0, 0},
    {0, 1, 0}
};
const int INIT[3][3] = {{0}, {1}, {3}};

Matrix trans(3, 3);
Matrix init(3, 1);

void solve()
{
    memcpy(trans.a, TRANS, sizeof(TRANS));
    memcpy(init.a, INIT, sizeof(INIT));

    static int f[MAXM];
    static Matrix g[MAXM];

    A[++M] = N;
    g[0] = Matrix(3, 1);
    for (int i = 1; i <= M; ++i) {
        Matrix t = fpm(trans, A[i] - A[i - 1]) * g[i - 1];
        f[i] = (fpm(trans, A[i]) * init - t).a[0][0];
        g[i] = t + init * f[i];
    }
    int ans = f[M];

    printf("%d\n", ans);
}

int main()
{
#ifdef __DEBUG
    freopen("E.in", "r", stdin);
    freopen("E.out", "w", stdout);
#endif

    input();
    solve();

    return 0;
}

// 日色欲尽花含烟,月明欲素愁不眠。
//     -- 李白《长相思·其二》

Submission Info

Submission Time
Task E - Placing Squares
User Ivlleiooq
Language C++14 (GCC 5.4.1)
Score 1600
Code Size 5083 Byte
Status AC
Exec Time 476 ms
Memory 5376 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 1600 / 1600
Status
AC × 4
AC × 38
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt
All sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt, sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt, subtask_1_01.txt, subtask_1_02.txt, subtask_1_03.txt, subtask_1_04.txt, subtask_1_05.txt, subtask_1_06.txt, subtask_1_07.txt, subtask_1_08.txt, subtask_1_09.txt, subtask_1_10.txt, subtask_1_11.txt, subtask_1_12.txt, subtask_1_13.txt, subtask_1_14.txt, subtask_1_15.txt, subtask_1_16.txt, subtask_1_17.txt, subtask_1_18.txt, subtask_1_19.txt, subtask_1_20.txt, subtask_1_21.txt, subtask_1_22.txt, subtask_1_23.txt, subtask_1_24.txt, subtask_1_25.txt, subtask_1_26.txt, subtask_1_27.txt, subtask_1_28.txt, subtask_1_29.txt, subtask_1_30.txt
Case Name Status Exec Time Memory
sample_01.txt AC 1 ms 256 KB
sample_02.txt AC 1 ms 256 KB
sample_03.txt AC 1 ms 256 KB
sample_04.txt AC 1 ms 256 KB
subtask_1_01.txt AC 151 ms 3840 KB
subtask_1_02.txt AC 34 ms 640 KB
subtask_1_03.txt AC 75 ms 1408 KB
subtask_1_04.txt AC 200 ms 3968 KB
subtask_1_05.txt AC 475 ms 5376 KB
subtask_1_06.txt AC 476 ms 5376 KB
subtask_1_07.txt AC 345 ms 5376 KB
subtask_1_08.txt AC 347 ms 5376 KB
subtask_1_09.txt AC 5 ms 256 KB
subtask_1_10.txt AC 4 ms 256 KB
subtask_1_11.txt AC 4 ms 256 KB
subtask_1_12.txt AC 2 ms 256 KB
subtask_1_13.txt AC 2 ms 256 KB
subtask_1_14.txt AC 3 ms 256 KB
subtask_1_15.txt AC 2 ms 256 KB
subtask_1_16.txt AC 4 ms 256 KB
subtask_1_17.txt AC 66 ms 3840 KB
subtask_1_18.txt AC 162 ms 4352 KB
subtask_1_19.txt AC 1 ms 256 KB
subtask_1_20.txt AC 201 ms 5376 KB
subtask_1_21.txt AC 1 ms 256 KB
subtask_1_22.txt AC 1 ms 256 KB
subtask_1_23.txt AC 1 ms 256 KB
subtask_1_24.txt AC 1 ms 256 KB
subtask_1_25.txt AC 1 ms 256 KB
subtask_1_26.txt AC 1 ms 256 KB
subtask_1_27.txt AC 1 ms 256 KB
subtask_1_28.txt AC 1 ms 256 KB
subtask_1_29.txt AC 1 ms 256 KB
subtask_1_30.txt AC 1 ms 256 KB