QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#55919#3840. Pass the Ball!Ctrl2333#WA 2ms6172kbC++203.1kb2022-10-15 17:08:372022-10-15 17:08:46

Judging History

This is the latest submission verdict.

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-10-15 17:08:46]
  • Judged
  • Verdict: WA
  • Time: 2ms
  • Memory: 6172kb
  • [2022-10-15 17:08:37]
  • Submitted

answer

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define re register
#define gc getchar
const long double pi=acos((long double)-1.0);

struct Comp{
    long double x,y;
    Comp(long double _x=0.0,long double _y=0.0){
        x=_x,y=_y;
    }
    Comp operator -(const Comp &b)const{
        return Comp(x-b.x,y-b.y);
    }
    Comp operator +(const Comp &b)const{
        return Comp(x+b.x,y+b.y);
    }
    Comp operator *(const Comp &b)const{
        return Comp(x*b.x-y*b.y,x*b.y+y*b.x);
    }
};

const int maxn=3010000;

typedef vector<Comp> Poly;

int r[maxn];
inline void FFT(Poly &A,int len, int typ){
    for(int i=0;i<len;i++)if(i<r[i])swap(A[i],A[r[i]]);
    for(int h=2;h<=len;h<<=1){
        Comp wn(cos((long double)2.0*pi/(long double)h),sin((long double)typ*(long double)2.0*pi/(long double)h));
        for(int j=0;j<len;j+=h){
            Comp w(1,0);
            for(int k=j;k<j+(h>>1);k++){
                Comp u=A[k], t=w*A[k+(h>>1)];
                A[k]=u+t,A[k+(h>>1)]=u-t;
                w=w*wn;
            }
        }
    }
    if(typ==-1)for(int i=0;i<len;i++)A[i].x/=(long double)len;
}
inline void init_rev(int len){
    for(int i=0;i<len;i++)r[i]=r[i>>1]>>1|((i&1)*(len>>1));
}

inline Poly operator+(const Poly &a, const Poly &b){
    Poly c=a;c.resize(max(a.size(),b.size()));
    for(int i=0;i<b.size();i++)c[i]=c[i]+b[i];
    return c;
}

Poly operator*(Poly a,Poly b){
    int n=a.size(),m=b.size(),l=1;
    while(l<n+m-1)l<<=1;
    init_rev(l);
    a.resize(l),FFT(a,l,1);
    b.resize(l),FFT(b,l,1);
    for(int i=0;i<l;i++)a[i]=a[i]*b[i];
    FFT(a,l,-1);
    a.resize(n+m-1);
    return a;
}


int n,q;
int f[100005];
Poly poly[100005];
vector<int>ring;
int vis[100005];
int stk[100005],top;
inline void print(Poly &a){
    for(int i=0;i<a.size();i++){
        printf("%lf ",a[i].x);
    }
    printf("\n");
}
void dfs(int u){
    //cout<<"U:"<<u<<endl;
    vis[u]=1;
    stk[++top]=u;
    if(vis[f[u]]){
        Poly p1,p2;
        for(int k=0;k<2;k++){
            for(int i=1;i<=top;i++){
                Comp ne(stk[i],0);
                p1.push_back(ne);
            }
        }
        for(int i=top;i>=1;i--){
            //cout<<"U:"<<u<<" node:"<<stk[i]<<endl;
            Comp ne(stk[i],0);
            p2.push_back(ne);
        }
        Poly res=p1*p2;
        //print(res);
        poly[top] = poly[top] + res;
    }
    else{
        dfs(f[u]);
    }
    top--;
}
int main(){
    //long long xx=0;
    //for(long long i=1;i<=100000ll;i++)xx+=i*i;
    //cout<<"X:"<<xx<<endl;
    scanf("%d%d",&n,&q);
    for(int i=1;i<=n;i++){
        scanf("%d",&f[i]);
    }
    for(int i=1;i<=n;i++){
        if(!vis[i])dfs(i);
    }
    for(int i=2;i<=100000;i++){
        if(poly[i].size())ring.push_back(i);
        //print(poly[i]);
    }
    for(int qq=1;qq<=q;qq++){
        long long k;scanf("%lld",&k);
        long long ans=0;
        for(int i=0;i<ring.size();i++){
            ans+=(long long)(poly[ring[i]][k%ring[i]+ring[i]-1].x+(long double)1.0);
        }
        printf("%lld\n",ans);
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 6168kb

input:

3 6
2 3 1
1
2
3
999999998
999999999
1000000000

output:

12
12
15
12
15
12

result:

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