QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#55903 | #3840. Pass the Ball! | Ctrl2333# | WA | 4ms | 6104kb | C++17 | 3.1kb | 2022-10-15 16:05:36 | 2022-10-15 16:05:39 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define re register
#define gc getchar
const long double pi=acos(-1.0);
namespace IO{
inline int getint(){
char c;
while(!isdigit(c=gc()));int num=c^48;
while(isdigit(c=gc()))num=(num+(num<<2)<<1)+(c^48);
return num;
}
}
using namespace IO;
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(2.0*pi/h),sin(typ*2.0*pi/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){
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(){
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);
}
printf("%lld\n",ans);
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 4ms
memory: 6104kb
input:
4 4 2 4 1 3 1 2 3 4
output:
24 19 24 30
result:
wrong answer 1st lines differ - expected: '25', found: '24'