QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#479487#3840. Pass the Ball!actorRE 1ms4324kbC++143.8kb2024-07-15 17:56:202024-07-15 17:56:20

Judging History

This is the latest submission verdict.

  • [2024-07-15 17:56:20]
  • Judged
  • Verdict: RE
  • Time: 1ms
  • Memory: 4324kb
  • [2024-07-15 17:56:20]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;
#define rep(i,l,r) for (int i = (l); i <= (r); i++)
#define per(i,r,l) for (int i = (r); i >= (l); i--)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define maxn(a, b) a = max(a, b)
#define minn(a, b) a = min(a, b)
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
typedef long double db;
const ll mod = 998244353;
mt19937 gen(114514);
ll rp(ll a,ll b) {ll res=1%mod;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}

namespace fft {
    const db pi = acosl(-1.0);
    struct cp {
        db x, y;
        cp(db xx = 0, db yy = 0) {x = xx, y = yy;}
        friend cp operator + (const cp& a, const cp&b) {return cp(a.x + b.x, a.y + b.y);}
        friend cp operator - (const cp& a, const cp&b) {return cp(a.x - b.x, a.y - b.y);}
        friend cp operator * (const cp& a, const cp&b) {return cp(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);}
    };
    vector<int> rev;
    vector<cp> wn;

    void fft(vector<cp> &a, int ty) {
        int sz = a.size();
        if (rev.size() != sz) {
            rev.clear(); rev.resize(sz);
            wn.clear(); wn.resize(sz);
            int l = __lg(sz);
            for (int i = 0; i < sz; i++) {
                rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << l - 1);
            }
            wn[0] = cp(1, 0);
            for (int i = 1; i < sz; i++) {
                wn[i] = cp(cosl(pi * i / sz), sinl(pi * i / sz));
            }
        }
        for (int i = 0; i < sz; i++) {
            if (i < rev[i]) swap(a[i], a[rev[i]]);
        }
        for (int i = 1; i < sz; i <<= 1) {
            for (int j = 0; j < sz; j += i << 1) {
                auto w = wn.begin();
                for (int k = 0; k < i; k++, w += sz / i) {
                    cp x = a[j + k], y = cp(w->x, ty * w->y) * a[i + j + k];
                    a[j + k] = x + y;
                    a[i + j + k] = x - y;
                }
            }
        }
        return ;
    }

    vector<cp> mul(const vector<cp> &_a, const vector<cp> &_b) {
        auto a = _a, b = _b;
        int nd = a.size() + b.size() - 2, sz = 1;
        while (sz <= nd) sz <<= 1;
        a.resize(sz);
        b.resize(sz);
        fft(a, 1);
        fft(b, 1);
        vector<cp> res(sz);
        for (int i = 0; i < sz; i++) {
            res[i] = a[i] * b[i];
        }
        fft(res, -1);
        // for (int i = 0; i < sz; i++) res[i].x /= sz;
        return res;
    };
};
const int N = 400;
ll sum[N][N], mus[N * N];

int main() {
    int n, q;
    scanf("%d%d", &n, &q);
    VI to(n + 1);
    vector<ll> ans(n + 1);
    rep(i,1,n) {
        scanf("%d", &to[i]);
    }
    vector<bool> vis(n + 1);
    rep(i,1,n) {
        int u = i;
        vector<fft::cp> f;
        while (!vis[u]) {
            f.pb(fft::cp(u, 0));
            vis[u] = true;
            u = to[u];
        }
        if (!f.empty()) {
            int sz = SZ(f);
            auto g = f;
            reverse(all(g));
            auto res = fft::mul(f, g);
            if (sz<N) {
                rep(i,0,SZ(res)-1) {
                    sum[sz][(i + 1) % sz] += (res[i].x + 0.5) / SZ(res);
                }
            } else {
                rep(i,0,n) mus[i] += (res[(i + sz - 1) % sz].x + 0.5) / SZ(res);
            }
        }
    }
    // rep(i,0,n) rep(j,1,N - 1) mus[i] += sum[j][i % j];
    while (q--) {
        int k;
        scanf("%d", &k);
        ll ans = mus[k];
        rep(i,1,N-1) ans += sum[i][k % i];
        printf("%lld\n", ans);
    }
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 4324kb

input:

4 4
2 4 1 3
1
2
3
4

output:

25
20
25
30

result:

ok 4 lines

Test #2:

score: -100
Runtime Error

input:

3 6
2 3 1
1
2
3
999999998
999999999
1000000000

output:


result: