QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#639744#3840. Pass the Ball!laonongminWA 3ms5936kbC++233.9kb2024-10-13 22:07:092024-10-13 22:07:09

Judging History

This is the latest submission verdict.

  • [2024-10-13 22:07:09]
  • Judged
  • Verdict: WA
  • Time: 3ms
  • Memory: 5936kb
  • [2024-10-13 22:07:09]
  • Submitted

answer

#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define N 100005
#define MOD 998244353
using namespace std;
struct Polynomial
{
    double PI = acos(-1);
    struct Complex
    {
        double x, y;
        Complex(double _x = 0.0, double _y = 0.0)
        {
            x = _x;
            y = _y;
        }
        Complex operator-(const Complex &rhs) const
        {
            return Complex(x - rhs.x, y - rhs.y);
        }
        Complex operator+(const Complex &rhs) const
        {
            return Complex(x + rhs.x, y + rhs.y);
        }
        Complex operator*(const Complex &rhs) const
        {
            return Complex(x * rhs.x - y * rhs.y, x * rhs.y + y * rhs.x);
        }
    };
    vector<Complex> c;
    Polynomial(vector<ll> &a)
    {
        int n = a.size();
        c.resize(n);
        for (int i = 0; i < n; i++)
        {
            c[i] = Complex(a[i], 0);
        }
        fft(c, n, 1);
    }
    void change(vector<Complex> &a, int n)
    {
        for (int i = 1, j = n / 2; i < n - 1; i++)
        {
            if (i < j)
                swap(a[i], a[j]);
            int k = n / 2;
            while (j >= k)
            {
                j -= k;
                k /= 2;
            }
            if (j < k)
                j += k;
        }
    }
    void fft(vector<Complex> &a, int n, int opt)
    {
        change(a, n);
        for (int h = 2; h <= n; h *= 2)
        {
            Complex wn(cos(2 * PI / h), sin(opt * 2 * PI / h));
            for (int j = 0; j < n; j += h)
            {
                Complex w(1, 0);
                for (int k = j; k < j + h / 2; k++)
                {
                    Complex u = a[k];
                    Complex t = w * a[k + h / 2];
                    a[k] = u + t;
                    a[k + h / 2] = u - t;
                    w = w * wn;
                }
            }
        }
        if (opt == -1)
        {
            for (int i = 0; i < n; i++)
            {
                a[i].x /= n;
            }
        }
    }
};
int n,q;
int p[N];
int Log2[N];
bitset<N> vis;
vector<ll> F[N];
vector<int> you;
int cnt=0;
void solve()
{
    cin>>n>>q;
    for(int i=1;i<=n;++i) cin>>p[i];
    for(int i=1;i<=n;++i)
    {
        vector<ll> A;
        for(int u=i,j=0; ;++j,u=p[u]) 
        {
            if(vis[u]) break;
            vis[u]=1;
            A.push_back(u);
        }
        if(A.empty()) continue;
        vector<ll> B(A);
        reverse(B.begin(), B.end());
        int tlen = A.size();
        int len = 1<<(Log2[2*A.size()-2]+1); // 线性卷积
        A.resize(len); B.resize(len);
        // for(int j=0;j<len;++j) cout<<A[j]<<' '; cout<<'\n';
        // for(int j=0;j<len;++j) cout<<B[j]<<' '; cout<<'\n';
        Polynomial FA(A), FB(B);
        for(int j=0;j<len;++j)
        {
            FA.c[j] = FA.c[j]*FB.c[j];
        }
        FA.fft(FA.c, len, -1);
        // for(int j=0;j<len;++j) cout<<FA.c[j].x<<' '; cout<<'\n';
        
        if(F[tlen].empty()) 
        {
            F[tlen].resize(tlen);
            you.push_back(tlen);
        }
        // cout<<tlen<<"FFF\n";
        for(int j=0;j<tlen;++j)
        {
            F[tlen][j] += (ll)(FA.c[j].x+0.5);
            if(i<tlen-1) F[tlen][j] += (ll)(FA.c[tlen-j-2].x+0.5);
            // cout<<F[tlen][j]<<' ';
        }
        // cout<<'\n';
        // for(auto &&tlen:you) cout<<tlen<<'\n';
    }
    
    while(q--)
    {
        int k; cin>>k;
        ll ans=0;
        for(auto &&tlen:you) {ans += F[tlen][(k+tlen-1)%tlen];}
        cout<<ans<<'\n';
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    Log2[0]=-1;
    for(int i=1;i<N;++i) Log2[i]=Log2[i/2]+1;
    int 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: 1ms
memory: 4204kb

input:

4 4
2 4 1 3
1
2
3
4

output:

25
20
25
30

result:

ok 4 lines

Test #2:

score: 0
Accepted
time: 1ms
memory: 4200kb

input:

3 6
2 3 1
1
2
3
999999998
999999999
1000000000

output:

11
11
14
11
14
11

result:

ok 6 lines

Test #3:

score: 0
Accepted
time: 0ms
memory: 4172kb

input:

3 6
3 1 2
1
2
3
999999998
999999999
1000000000

output:

11
11
14
11
14
11

result:

ok 6 lines

Test #4:

score: 0
Accepted
time: 2ms
memory: 4592kb

input:

1000 10000
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100...

output:

333334000
332835500
332338000
331841500
331346000
330851500
330358000
329865500
329374000
328883500
328394000
327905500
327418000
326931500
326446000
325961500
325478000
324995500
324514000
324033500
323554000
323075500
322598000
322121500
321646000
321171500
320698000
320225500
319754000
319283500
...

result:

ok 10000 lines

Test #5:

score: 0
Accepted
time: 0ms
memory: 4532kb

input:

1000 10000
187 493 316 665 124 40 448 649 657 65 438 730 816 107 789 286 309 469 169 216 488 52 212 111 541 83 990 48 282 867 36 220 676 241 959 372 322 244 481 708 595 957 215 223 120 658 291 176 229 158 431 492 221 986 889 861 606 518 106 349 410 765 745 812 563 998 150 392 358 328 747 793 587 507...

output:

250347169
248662078
245260552
253150328
247096579
249698948
249942589
251180693
248589849
253775352
248472247
248369272
249001282
249611561
251718722
248202949
252492155
251442262
255269934
247070745
248898892
250071493
249262069
247714054
248954719
251676093
251650611
249152315
248608212
249678723
...

result:

ok 10000 lines

Test #6:

score: -100
Wrong Answer
time: 3ms
memory: 5936kb

input:

1000 10000
301 793 604 129 545 524 625 540 271 616 710 629 682 190 152 287 40 5 921 699 730 427 833 680 514 782 641 754 15 218 725 862 238 468 917 368 850 35 243 339 688 460 597 979 398 748 552 25 189 68 115 76 888 844 890 102 835 581 266 342 571 749 574 156 717 538 440 764 601 831 130 39 136 842 78...

output:

141664397
150850588
158528656
167314474
169892026
181453287
180813025
188291036
193858464
197569453
197926391
197101890
200098990
204551075
215023852
211860570
205930135
205287899
203215311
211399963
206798501
210054576
207985124
217494845
211190635
213560077
214491067
209945961
201914389
212459760
...

result:

wrong answer 1st lines differ - expected: '250889096', found: '141664397'