結果

問題 No.1261 数字集め
ユーザー msm1993msm1993
提出日時 2020-10-23 15:40:47
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,562 ms / 8,000 ms
コード長 4,100 bytes
コンパイル時間 4,127 ms
コンパイル使用メモリ 168,724 KB
実行使用メモリ 133,628 KB
最終ジャッジ日時 2023-09-28 15:31:44
合計ジャッジ時間 167,239 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,562 ms
133,228 KB
testcase_01 AC 817 ms
57,080 KB
testcase_02 AC 1,702 ms
100,276 KB
testcase_03 AC 1,642 ms
101,300 KB
testcase_04 AC 1,061 ms
70,948 KB
testcase_05 AC 2,232 ms
128,440 KB
testcase_06 AC 1,208 ms
79,152 KB
testcase_07 AC 1,670 ms
104,244 KB
testcase_08 AC 970 ms
65,576 KB
testcase_09 AC 1,300 ms
80,656 KB
testcase_10 AC 237 ms
22,076 KB
testcase_11 AC 2,395 ms
126,712 KB
testcase_12 AC 2,088 ms
115,240 KB
testcase_13 AC 1,789 ms
105,568 KB
testcase_14 AC 275 ms
27,784 KB
testcase_15 AC 892 ms
60,104 KB
testcase_16 AC 1,778 ms
104,532 KB
testcase_17 AC 1,354 ms
87,320 KB
testcase_18 AC 914 ms
61,216 KB
testcase_19 AC 1,682 ms
101,716 KB
testcase_20 AC 309 ms
29,956 KB
testcase_21 AC 2,325 ms
133,296 KB
testcase_22 AC 2,336 ms
133,524 KB
testcase_23 AC 2,327 ms
133,472 KB
testcase_24 AC 2,311 ms
133,336 KB
testcase_25 AC 2,298 ms
133,380 KB
testcase_26 AC 2,304 ms
133,336 KB
testcase_27 AC 2,296 ms
133,336 KB
testcase_28 AC 2,305 ms
133,368 KB
testcase_29 AC 2,299 ms
133,340 KB
testcase_30 AC 2,327 ms
133,360 KB
testcase_31 AC 2,295 ms
133,460 KB
testcase_32 AC 2,274 ms
133,380 KB
testcase_33 AC 2,296 ms
133,376 KB
testcase_34 AC 2,270 ms
133,628 KB
testcase_35 AC 2,279 ms
133,508 KB
testcase_36 AC 2,295 ms
133,356 KB
testcase_37 AC 2,257 ms
133,384 KB
testcase_38 AC 2,286 ms
133,320 KB
testcase_39 AC 2,249 ms
133,364 KB
testcase_40 AC 2,268 ms
133,416 KB
testcase_41 AC 2,255 ms
133,420 KB
testcase_42 AC 2,262 ms
133,420 KB
testcase_43 AC 2,239 ms
133,340 KB
testcase_44 AC 1,979 ms
119,520 KB
testcase_45 AC 502 ms
42,428 KB
testcase_46 AC 1,087 ms
73,568 KB
testcase_47 AC 785 ms
57,224 KB
testcase_48 AC 1,679 ms
102,576 KB
testcase_49 AC 1,967 ms
118,340 KB
testcase_50 AC 1,762 ms
107,560 KB
testcase_51 AC 539 ms
45,148 KB
testcase_52 AC 237 ms
21,248 KB
testcase_53 AC 560 ms
45,228 KB
testcase_54 AC 626 ms
48,560 KB
testcase_55 AC 344 ms
32,052 KB
testcase_56 AC 405 ms
35,548 KB
testcase_57 AC 1,607 ms
98,384 KB
testcase_58 AC 505 ms
42,908 KB
testcase_59 AC 2,070 ms
121,184 KB
testcase_60 AC 615 ms
47,736 KB
testcase_61 AC 809 ms
58,632 KB
testcase_62 AC 1,878 ms
110,516 KB
testcase_63 AC 370 ms
34,064 KB
testcase_64 AC 2,202 ms
129,276 KB
testcase_65 AC 1,387 ms
91,176 KB
testcase_66 AC 1,192 ms
79,528 KB
testcase_67 AC 1,810 ms
111,204 KB
testcase_68 AC 1,141 ms
76,592 KB
testcase_69 AC 1,092 ms
75,112 KB
testcase_70 AC 876 ms
63,644 KB
testcase_71 AC 1,709 ms
106,016 KB
testcase_72 AC 656 ms
51,328 KB
testcase_73 AC 760 ms
56,360 KB
testcase_74 AC 393 ms
35,800 KB
testcase_75 AC 1,054 ms
71,544 KB
testcase_76 AC 1,542 ms
98,484 KB
testcase_77 AC 1,749 ms
112,064 KB
testcase_78 AC 222 ms
19,824 KB
testcase_79 AC 1,865 ms
115,348 KB
testcase_80 AC 225 ms
21,004 KB
testcase_81 AC 276 ms
28,544 KB
testcase_82 AC 1,461 ms
94,000 KB
testcase_83 AC 699 ms
53,076 KB
testcase_84 AC 2,233 ms
133,584 KB
testcase_85 AC 2,294 ms
133,392 KB
testcase_86 AC 2,312 ms
133,444 KB
testcase_87 AC 2,300 ms
133,396 KB
testcase_88 AC 2,275 ms
133,384 KB
testcase_89 AC 2,334 ms
133,520 KB
testcase_90 AC 2,279 ms
133,468 KB
testcase_91 AC 2,314 ms
133,432 KB
testcase_92 AC 2,321 ms
133,476 KB
testcase_93 AC 2,273 ms
133,440 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int MOD = 1000000007;
//const int MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
const double pi = acos(-1.0);
const double EPS = 1e-10;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};

template<int mod>
struct Mod_Int{
    ll x;

    Mod_Int() : x(0) {}

    Mod_Int(ll y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

    Mod_Int &operator += (const Mod_Int &p){
        x = (x + p.x) % mod;
        return *this;
    }

    Mod_Int &operator -= (const Mod_Int &p){
        x = (x + mod - p.x) % mod;
        return *this;
    }

    Mod_Int &operator *= (const Mod_Int &p){
        x = (x * p.x) % mod;
        return *this;
    }

    Mod_Int &operator /= (const Mod_Int &p){
        *this *= p.inverse();
        return *this;
    }

    Mod_Int &operator ++ () {return *this += Mod_Int(1);}

    Mod_Int operator ++ (int){
        Mod_Int tmp = *this;
        ++*this;
        return tmp;
    }

    Mod_Int &operator -- () {return *this -= Mod_Int(1);}

    Mod_Int operator -- (int){
        Mod_Int tmp = *this;
        --*this;
        return tmp;
    }

    Mod_Int operator - () const {return Mod_Int(-x);}

    Mod_Int operator + (const Mod_Int &p) const {return Mod_Int(*this) += p;}

    Mod_Int operator - (const Mod_Int &p) const {return Mod_Int(*this) -= p;}

    Mod_Int operator * (const Mod_Int &p) const {return Mod_Int(*this) *= p;}

    Mod_Int operator / (const Mod_Int &p) const {return Mod_Int(*this) /= p;}

    bool operator == (const Mod_Int &p) const {return x == p.x;}

    bool operator != (const Mod_Int &p) const {return x != p.x;}

    Mod_Int inverse() const {
        assert(*this != Mod_Int(0));
        return pow(mod-2);
    }

    Mod_Int pow(ll k) const{
        Mod_Int now = *this, ret = 1;
        while(k){
            if(k&1) ret *= now;
            now *= now, k >>= 1;
        }
        return ret;
    }

    friend ostream &operator << (ostream &os, const Mod_Int &p){
        return os << p.x;
    }

    friend istream &operator >> (istream &is, Mod_Int &p){
        ll a;
        is >> a;
        p = Mod_Int<mod>(a);
        return is;
    }
};

using mint = Mod_Int<MOD>;

const int MAX = 1000000;
int A[MAX+1];
mint iv[MAX+1], pw[MAX+1];

mint inv(int a){
    if(a > 0) return iv[a];
    else return iv[abs(a)]*(-1);
}

mint calc(int i, int j, int k){
    int a = A[i], b = A[j], c = A[k];
    return (pw[a]*(b-c)+pw[b]*(c-a)+pw[c]*(a-b))*inv(a-b)*inv(b-c)*inv(c-a);
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int N, M;
    cin >> N >> M;
    
    rep2(i, 2, N){
        cin >> A[i];
        A[i]--;
    }
    bool st[N+1], en[N+1];
    fill(st, st+N+1, false), fill(en, en+N+1, false);
    rep2(i, 2, N-1){
        st[i] = true, en[i] = (N%i == 0);
    }
    
    rep2(i, 1, MAX) iv[i] = mint(i).inverse();
    rep2(i, 1, MAX) pw[i] = mint(i).pow(M-1);

    vector<int> es[N+1];

    mint ans = 0;
    rep2(i, 2, N-1){
        for(int j = i+i; j <= N; j += i){
            es[j].pb(i);
            if(st[i] && en[j]) ans -= calc(i, j, N);
        }
    }

    cout << ans << '\n';

    int Q;
    cin >> Q;

    while(Q--){
        int x, y; cin >> x >> y;
        if(y < N){
            es[y].pb(x);
            if(st[x] && en[y]) ans -= calc(x, y, N);
        }
        else{
            en[x] = true;
            for(auto &e: es[x]){
                if(st[e]) ans -= calc(e, x, N);
            }
        }
        cout << ans << '\n';
    }
}
0