結果

問題 No.426 往復漸化式
ユーザー はむこはむこ
提出日時 2016-09-21 00:58:43
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4,256 ms / 5,000 ms
コード長 9,918 bytes
コンパイル時間 2,141 ms
コンパイル使用メモリ 185,980 KB
実行使用メモリ 52,992 KB
最終ジャッジ日時 2024-11-17 13:03:12
合計ジャッジ時間 37,564 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 3 ms
6,816 KB
testcase_02 AC 2 ms
6,820 KB
testcase_03 AC 18 ms
6,820 KB
testcase_04 AC 19 ms
6,816 KB
testcase_05 AC 264 ms
8,320 KB
testcase_06 AC 257 ms
8,448 KB
testcase_07 AC 957 ms
52,992 KB
testcase_08 AC 990 ms
52,864 KB
testcase_09 AC 1,757 ms
52,992 KB
testcase_10 AC 1,768 ms
52,864 KB
testcase_11 AC 1,341 ms
52,768 KB
testcase_12 AC 2,513 ms
52,864 KB
testcase_13 AC 2,402 ms
52,844 KB
testcase_14 AC 3,003 ms
52,864 KB
testcase_15 AC 2,101 ms
52,864 KB
testcase_16 AC 3,924 ms
52,864 KB
testcase_17 AC 2,854 ms
52,892 KB
testcase_18 AC 4,256 ms
52,752 KB
testcase_19 AC 989 ms
52,908 KB
testcase_20 AC 1,783 ms
52,812 KB
testcase_21 AC 1,868 ms
52,864 KB
testcase_22 AC 1,778 ms
52,908 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#ifdef _WIN32
#define scanfll(x) scanf("%I64d", x)
#define printfll(x) printf("%I64d", x)
#else
#define scanfll(x) scanf("%lld", x)
#define printfll(x) printf("%lld", x)
#endif
#define rep(i,n) for(long long i = 0; i < (long long)(n); i++)
#define repi(i,a,b) for(long long i = (long long)(a); i < (long long)(b); i++)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mt make_tuple
#define mp make_pair
template<class T1, class T2> bool chmin(T1 &a, T2 b) { return b < a && (a = b, true); }
template<class T1, class T2> bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

using ll = long long; using vll = vector<ll>; using vvll = vector<vll>;
using ld = long double;  using vld = vector<ld>; 
using vi = vector<int>; using vvi = vector<vi>;
vll conv(vi& v) { vll r(v.size()); rep(i, v.size()) r[i] = v[i]; return r; }
using P = pair<ll, ll>;

template <typename T, typename U> ostream &operator<<(ostream &o, const pair<T, U> &v) {  o << "(" << v.first << ", " << v.second << ")"; return o; }
template<size_t...> struct seq{}; template<size_t N, size_t... Is> struct gen_seq : gen_seq<N-1, N-1, Is...>{}; template<size_t... Is> struct gen_seq<0, Is...> : seq<Is...>{};
template<class Ch, class Tr, class Tuple, size_t... Is>
void print_tuple(basic_ostream<Ch,Tr>& os, Tuple const& t, seq<Is...>){ using s = int[]; (void)s{0, (void(os << (Is == 0? "" : ", ") << get<Is>(t)), 0)...}; }
template<class Ch, class Tr, class... Args> 
auto operator<<(basic_ostream<Ch, Tr>& os, tuple<Args...> const& t) -> basic_ostream<Ch, Tr>& { os << "("; print_tuple(os, t, gen_seq<sizeof...(Args)>()); return os << ")"; }
ostream &operator<<(ostream &o, const vvll &v) { rep(i, v.size()) { rep(j, v[i].size()) o << v[i][j] << " "; cout << endl; } return o; }
template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) { o << '['; rep(i, v.size()) o << v[i] << (i != v.size()-1 ? ", " : ""); o << "]";  return o; }
template <typename T>  ostream &operator<<(ostream &o, const set<T> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it << (next(it) != m.end() ? ", " : ""); o << "]";  return o; }
template <typename T, typename U>  ostream &operator<<(ostream &o, const map<T, U> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it << (next(it) != m.end() ? ", " : ""); o << "]";  return o; }
template <typename T, typename U>  ostream &operator<<(ostream &o, const unordered_map<T, U> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it; o << "]";  return o; }
string bits_to_string(ll mask, ll n) { string s; rep(i, n) s += '0' + !!(mask & (1ll << i)); return s; }
#define ldout fixed << setprecision(40) 

static const double EPS = 1e-14;
static const long long INF = 1e18;
static const long long mo = 1e9+7;

class Mod {
    public:
        int num;
        Mod() : Mod(0) {}
        Mod(long long int n) : num(n) { }
        Mod(const string &s){ long long int tmp = 0; for(auto &c:s) tmp = (c-'0'+tmp*10) % mo; num = tmp; }
        Mod(int n) : Mod(static_cast<long long int>(n)) {}
        operator int() { return num; }
};
istream &operator>>(istream &is, Mod &x) { long long int n; is >> n; x = n; return is; }
ostream &operator<<(ostream &o, const Mod &x) { o << x.num; return o; }
Mod operator+(const Mod a, const Mod b) { return Mod((a.num + b.num) % mo); }
Mod operator+(const long long int a, const Mod b) { return Mod(a) + b; }
Mod operator+(const Mod a, const long long int b) { return b + a; }
Mod operator++(Mod &a) { return a + Mod(1); }
Mod operator-(const Mod a, const Mod b) { return Mod((mo + a.num - b.num) % mo); }
Mod operator-(const long long int a, const Mod b) { return Mod(a) - b; }
Mod operator--(Mod &a) { return a - Mod(1); }
Mod operator*(const Mod a, const Mod b) { return Mod(((long long)a.num * b.num) % mo); }
Mod operator*(const long long int a, const Mod b) { return Mod(a)*b; }
Mod operator*(const Mod a, const long long int b) { return Mod(b)*a; }
Mod operator*(const Mod a, const int b) { return Mod(b)*a; }
Mod operator+=(Mod &a, const Mod b) { return a = a + b; }
Mod operator+=(long long int &a, const Mod b) { return a = a + b; }
Mod operator-=(Mod &a, const Mod b) { return a = a - b; }
Mod operator-=(long long int &a, const Mod b) { return a = a - b; }
Mod operator*=(Mod &a, const Mod b) { return a = a * b; }
Mod operator*=(long long int &a, const Mod b) { return a = a * b; }
Mod operator*=(Mod& a, const long long int &b) { return a = a * b; }
Mod factorial(const long long n) {
    if (n < 0) return 0;
    Mod ret = 1;
    for (int i = 1; i <= n; i++) {
        ret *= i;
    }
    return ret;
}
Mod operator^(const Mod a, const long long n) {
    if (n == 0) return Mod(1);
    Mod res = (a * a) ^ (n / 2);
    if (n % 2) res = res * a;
    return res;
}
Mod modpowsum(const Mod a, const long long b) {
    if (b == 0) return 0;
    if (b % 2 == 1) return modpowsum(a, b - 1) * a + Mod(1);
    Mod result = modpowsum(a, b / 2);
    return result * (a ^ (b / 2)) + result;
}


/*************************************/
// 以下、modは素数でなくてはならない!
/*************************************/
Mod inv(const Mod a) { return a ^ (mo - 2); }
Mod operator/(const Mod a, const Mod b) { assert(b.num != 0); return a * inv(b); }
Mod operator/(const long long int a, const Mod b) { assert(b.num != 0); return Mod(a) * inv(b); }
Mod operator/=(Mod &a, const Mod b) { assert(b.num != 0); return a = a * inv(b); }

/*************************************/
// GF(p)の行列演算
/*************************************/
using number = Mod;
using arr = vector<number>;
using matrix = vector<vector<Mod>>;

ostream &operator<<(ostream &o, const arr &v) { rep(i, v.size()) cout << v[i] << " "; cout << endl; return o; }
ostream &operator<<(ostream &o, const matrix &v) { rep(i, v.size()) cout << v[i]; return o; }

matrix zero(int n) { return matrix(n, arr(n, 0)); } // O(n^2)
matrix identity(int n) { matrix A(n, arr(n, 0)); rep(i, n) A[i][i] = 1; return A; } // O(n^2)
// O(n^2)
arr mul(const matrix &A, const arr &x) { 
//    assert(A[0].size() == x.size());
    arr y(A.size(), 0); 
    rep(i, A.size()) rep(j, A[0].size()) y[i] += A[i][j] * x[j]; 
    return y; 
} 
// O(n^3)
matrix mul(const matrix &A, const matrix &B) {
    matrix C(A.size(), arr(B[0].size(), 0));
    rep(i, C.size())
        rep(j, C[i].size())
        rep(k, A[i].size())
        C[i][j] += A[i][k] * B[k][j];
    return C;
}
// O(n^2)
matrix plu(const matrix &A, const matrix &B) {
    matrix C(A.size(), arr(B[0].size(), 0));
    rep(i, C.size())
        rep(j, C[i].size())
            C[i][j] += A[i][j] + B[i][j];
    return C;
}
// O(n^2)
arr plu(const arr &A, const arr &B) {
    assert(A.size() == B.size());
    arr C(A.size());
    rep(i, A.size())
        C[i] += A[i] + B[i];
    return C;
}

int T = 2;
ll n;
vector<matrix> A, B, S;
vector<matrix> Ab, Bb, Tri;
arr a0(3), bn(2);
ll bucket_num;

void updateBucket(ll bucket_index) {
    if (bucket_index >= bucket_num) return;
    matrix Ab_i = identity(3);
    matrix Bb_i = identity(2);
    matrix Tri_i = matrix(2, arr(3, 0));

    rep(i, T) {
        Tri_i = plu(Tri_i, mul(mul(Bb_i, S[bucket_index*T+1+i]), Ab_i));
        Ab_i = mul(A[bucket_index*T+1+i], Ab_i);
        Bb_i = mul(Bb_i, B[bucket_index*T+1+i]);
    }

    Ab[bucket_index] = Ab_i;
    Bb[bucket_index] = Bb_i;
    Tri[bucket_index] = Tri_i;
}

arr get_a(ll index) {
    if (index <= 0) return a0;
    arr ret = a0;
    ll i = 0;
    while (i != index) {
        if (i % T == 1 && i + T <= index) {
            ret = mul(Ab[i/T], ret);
            i += T;
        } else { 
            ret = mul(A[i], ret);
            i++;
        }
    }
    return ret;
}
arr get_b(ll index) {
    index--;
    arr ret = bn;
    ll i = n;
    while (i != index) {
        if (i % T == 0 && i - T >= index) {
            ret = mul(Bb[i/T-1], ret);
            i -= T;
        } else { 
            ret = mul(B[i], ret);
            i--;
        }
    }
    return ret;
}

matrix get_P(ll index) {
    if (index >= n) 
        return matrix(2, arr(3, 0));
    
    if (index % T == 0 && index + T <= n) {
        matrix ret = Tri[index / T];
        ret = plu(ret, mul(mul(Bb[index / T], get_P(index + T)), Ab[index / T]));
        return ret;
    } else {
        matrix ret = S[index+1];
        ret = plu(ret, mul(mul(B[index+1], get_P(index + 1)), A[index+1]));
        return ret;
    }
}

int main(int argc, char** argv) {
    cin.tie(0); ios::sync_with_stdio(false);
    cin >> n;
    T = 10 + (ll)(200. * ((double)n / 100000.));

    A.resize(n+1); 
    B.resize(n+1); 
    S.resize(n+1); 
    rep(i, n+1) {
        A[i] = identity(3);
        B[i] = identity(2);
        S[i] = matrix(2, arr(3, 0));
    }
    repi(i, 1, n+1) {
        rep(j, 6) {
            S[i][j/3][j%3] = 6 * i + j;
        }
    }

    bucket_num = n / T;
    Ab.resize(bucket_num); 
    Bb.resize(bucket_num); 
    Tri.resize(bucket_num); 
    rep(i, bucket_num) {
        updateBucket(i);
    }

    rep(i, 3) cin >> a0[i];
    rep(i, 2) cin >> bn[i];

    ll q; cin >> q;
    rep(_, q) {
        string type; cin >> type;
        ll index; cin >> index;
        if (type == "a") {
            rep(i, 9) cin >> A[index][i/3][i%3];
            updateBucket((index - 1) / T);
        } else if (type == "b") {
            rep(i, 4) cin >> B[index][i/2][i%2];
            updateBucket((index - 1)/ T);
        } else if (type == "ga") {
            auto ret = get_a(index);
            cout << ret[0] << " " << ret[1] << " " << ret[2] << endl;
        } else {
            matrix P = get_P(index);
            arr a_chain = get_a(index+1);
            arr b_chain = get_b(index+1);
            arr b = plu(b_chain, mul(P, a_chain));
            cout << b[0] << " " << b[1] << endl;
        } 
    }

    return 0;
}
0