QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#639737#3840. Pass the Ball!laonongminWA 1ms5916kbC++233.8kb2024-10-13 22:00:582024-10-13 22:01:02

Judging History

This is the latest submission verdict.

  • [2024-10-13 22:01:02]
  • Judged
  • Verdict: WA
  • Time: 1ms
  • Memory: 5916kb
  • [2024-10-13 22:00:58]
  • 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);
        }
        for(int j=0;j<tlen;++j)
        {
            F[tlen][j] += FA.c[j].x;
            if(i<tlen-1) F[tlen][j] += FA.c[tlen-j-2].x;
        }
        // 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;
}

详细

Test #1:

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

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: 1ms
memory: 5916kb

input:

3 6
2 3 1
1
2
3
999999998
999999999
1000000000

output:

10
11
14
11
14
10

result:

wrong answer 1st lines differ - expected: '11', found: '10'