QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#693721#3840. Pass the Ball!liliwaWA 2ms12020kbC++142.8kb2024-10-31 16:38:142024-10-31 16:38:16

Judging History

This is the latest submission verdict.

  • [2024-10-31 16:38:16]
  • Judged
  • Verdict: WA
  • Time: 2ms
  • Memory: 12020kb
  • [2024-10-31 16:38:14]
  • Submitted

answer

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> Pll;

#define x first
#define y second
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define lep(i,a,b) for(int i=(a);i>=(b);i--)
#define sci(x) cin >> (x)
#define pli(x) cout << (x) << " ";
#define pll(x) cout << (x) << '\n';
#define yes cout << "Yes" << '\n';
#define no cout << "No" << '\n';
#define ls u << 1
#define rs u << 1 | 1
//cout << "? " << << ' ';
//printf("case:%d %d\n", );

const int N = 1e6 + 10;
const ll mod = 4179340454199820289, G = 3, Gi = 1393113484733273430;
ll n, m, k;

ll a[N], b[N], f[N], vis[N], p[N], q[N], rev[N], res[N];
ll ck[505][505];
ll bit, tot;

ll qpow(ll a, ll b, ll mod)
{
    ll res = 1;
    while(b){
        if (b & 1) res = (__int128)res * a % mod;
        b >>= 1;
        a = (__int128)a * a % mod;
    }
    return res;
}

void NTT(ll a[], int type){
    for(int i = 0; i < tot; i++)
        if (i > rev[i])
            swap(a[i], a[rev[i]]);
    for(int mid = 1; mid < tot; mid <<= 1){
        ll w1 = qpow(type == 1 ? G : Gi, (mod - 1) / (mid * 2), mod);
        for(int i = 0; i < tot; i += mid * 2){
            ll  wk = 1;
            for(int j = 0; j < mid; j++, wk = (__int128)wk * w1 % mod){
                ll x = a[i + j], y = (__int128)wk * a[i + j + mid] % mod;
                a[i + j] = (x + y) % mod, a[i + j + mid] = (x - y + mod) % mod;
            }
        }
    }
    if (type == -1){
        ll inv = qpow(tot, mod - 2, mod);
        for(int i = 0; i < tot; i++)
            a[i] = (__int128)a[i] * inv % mod;
    }
}

void solve()
{
    cin >> n >> m;
    rep(i, 1, n) sci(p[i]); rep(i, 1, n) sci(q[i]);
    
    rep(i, 1, n) if(!vis[i])
    {
        ll now = i, c1 = 0;
        while(1)
        {
            a[c1] = b[c1] = now; vis[now] = 1; now = p[now]; c1++;
            if(vis[now]) break;
        }
        reverse(b, b + c1); for(int i = 0; i < c1; i++) b[i + c1] = b[i];
        while((1 << bit) < 3 * c1 + 1) bit++; tot = 1 << bit;
        for(int i = 0; i < tot; i++) rev[i] = (rev[i / 2] / 2) | ((i & 1) << (bit - 1));
        for(int i = c1; i < tot; i++) a[i] = 0; for(int i = c1 * c1; i < tot; i++) b[i] = 0;
        NTT(a, 1), NTT(b, 1); for(int i = 0; i < tot; i++) a[i] = (__int128)a[i] * b[i] % mod;
        NTT(a, -1);
        if(c1 >= 500) rep(i, 1, m) res[i] += a[q[i] % c1 + c1 - 1];
        else for(int i = 0; i < c1; i++) ck[c1][i] += a[i + c1 - 1];
    }
    rep(i, 1, m)
    {
        rep(j, 2, 500) res[i] += ck[j][q[i] % j];
        cout << res[i] << '\n';
    }
}

signed main() {
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int T; T = 1;
    //cin >> T;
    while (T--)
    {
        solve();
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 12020kb

input:

4 4
2 4 1 3
1
2
3
4

output:

25
20
25
30

result:

ok 4 lines

Test #2:

score: -100
Wrong Answer
time: 2ms
memory: 11756kb

input:

3 6
2 3 1
1
2
3
999999998
999999999
1000000000

output:

11
11
14
14
14
14

result:

wrong answer 4th lines differ - expected: '11', found: '14'