結果

問題 No.510 二次漸化式
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2017-05-02 20:14:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 967 ms / 3,000 ms
コード長 3,067 bytes
コンパイル時間 1,971 ms
コンパイル使用メモリ 180,520 KB
実行使用メモリ 71,672 KB
最終ジャッジ日時 2023-10-12 05:15:39
合計ジャッジ時間 31,168 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 104 ms
70,576 KB
testcase_01 AC 105 ms
70,744 KB
testcase_02 AC 826 ms
70,628 KB
testcase_03 AC 826 ms
70,808 KB
testcase_04 AC 825 ms
70,680 KB
testcase_05 AC 824 ms
70,648 KB
testcase_06 AC 866 ms
70,680 KB
testcase_07 AC 867 ms
70,804 KB
testcase_08 AC 871 ms
70,740 KB
testcase_09 AC 867 ms
70,572 KB
testcase_10 AC 779 ms
70,744 KB
testcase_11 AC 781 ms
70,748 KB
testcase_12 AC 785 ms
70,736 KB
testcase_13 AC 780 ms
70,580 KB
testcase_14 AC 782 ms
70,752 KB
testcase_15 AC 780 ms
70,740 KB
testcase_16 AC 530 ms
71,492 KB
testcase_17 AC 524 ms
71,600 KB
testcase_18 AC 524 ms
71,628 KB
testcase_19 AC 521 ms
71,532 KB
testcase_20 AC 523 ms
71,496 KB
testcase_21 AC 540 ms
71,564 KB
testcase_22 AC 522 ms
71,528 KB
testcase_23 AC 967 ms
71,672 KB
testcase_24 AC 935 ms
71,552 KB
testcase_25 AC 938 ms
71,564 KB
testcase_26 AC 938 ms
71,488 KB
testcase_27 AC 937 ms
71,484 KB
testcase_28 AC 940 ms
71,428 KB
testcase_29 AC 941 ms
71,480 KB
testcase_30 AC 937 ms
71,560 KB
testcase_31 AC 943 ms
71,088 KB
testcase_32 AC 938 ms
71,104 KB
testcase_33 AC 946 ms
71,424 KB
testcase_34 AC 814 ms
70,628 KB
testcase_35 AC 648 ms
70,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)


#define INF 1LL<<60
typedef vector<int> Vec;
typedef vector<Vec> Mat;
int mod = 1000000007;
int add(int x, int y) { return (x += y) >= mod ? x - mod : x; }
template<class... T> int add(int x, T... y) { return add(x, add(y...)); }
int mul(int x, int y) { return 1LL * x * y % mod; }
template<class... T> int mul(int x, T... y) { return mul(x, mul(y...)); }
int sub(int x, int y) { return add(x, mod - y); }
int modpow(int a, long long b) { int ret = 1; while (b > 0) { if (b & 1) ret = 1LL * ret * a % mod; a = 1LL * a * a % mod; b >>= 1; } return ret; }
int modinv(int a) { return modpow(a, mod - 2); }
Vec mulMatVec(Mat a, Vec b) {
    int n = b.size(); Vec ret(n, 0);
    rep(i, 0, n) rep(j, 0, n) ret[i] = add(ret[i], mul(a[i][j], b[j]));
    return ret;
}
Mat mulMatMat(Mat a, Mat b) {
    int n = a.size(); Mat ret(n, Vec(n, 0));
    rep(i, 0, n) rep(j, 0, n) rep(k, 0, n) ret[i][j] = add(ret[i][j], mul(a[i][k], b[k][j]));
    return ret;
}
struct func {
    Mat m;
    func(){
        m = Mat(4, Vec(4, 0));
        m[0][0] = 1;
        m[1][3] = 1;
        m[2][3] = 1;
        m[3][3] = 1;
    }
};

func operator*(func x, func y) {
    func res;
    res.m = mulMatMat(x.m, y.m);
    return res;
}
//-----------------------------------------------------------------
template<class V, int NV> struct SegTree {
    V comp(V l, V r) {
        return r * l;
    };
    vector<V> val; SegTree() { val = vector<V>(NV * 2); }
    V get(int x, int y, int l = 0, int r = NV, int k = 1) { // x<=i<y
        if (r <= x || y <= l) return V();
        if (x <= l && r <= y) return val[k];
        auto A = get(x, y, l, (l + r) / 2, k * 2);
        auto B = get(x, y, (l + r) / 2, r, k * 2 + 1);
        return comp(A, B);
    }
    void update(int i, V v) { i += NV; val[i] = v; while (i>1) i >>= 1, val[i] = comp(val[i * 2], val[i * 2 + 1]); }
};
SegTree<func, 1 << 17> st;
//-----------------------------------------------------------------------------------
int N, Q;
int X[101010], Y[101010];
void make(int i) {
    func f;

    f.m[0][2] = X[i];
    f.m[1][1] = Y[i];
    f.m[2][1] = mul(2, Y[i]);
    f.m[2][2] = mul(Y[i], Y[i]);

    st.update(i, f);
}
//-----------------------------------------------------------------------------------
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    cin >> N >> Q;

    rep(q, 0, Q) {
        char t; cin >> t;
        if (t == 'x') {
            int i, v; cin >> i >> v;
            X[i] = v;
            make(i);
        }
        else if (t == 'y') {
            int i, v; cin >> i >> v;
            Y[i] = v;
            make(i);
        }
        else {
            int i; cin >> i;
            if (i == 0) {
                printf("1\n");
                continue;
            }

            auto f = st.get(0, i);
            Vec v(4, 0);
            v[0] = 1;
            v[1] = 1;
            v[2] = 1;
            v[3] = 1;

            v = mulMatVec(f.m, v);
            printf("%d\n", v[0]);
        }
    }
}
0