QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#575964 | #3840. Pass the Ball! | ganking | WA | 50ms | 266108kb | C++20 | 2.6kb | 2024-09-19 17:40:31 | 2024-09-19 17:40:31 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define db long double
#define fo(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define fd(i,a,b) for(int (i)=(a);(i)>=(b);(i)--)
using namespace std;
const int N=2e6+10;
const db pi=acos(-1.0);
struct comp{
db x,y;
comp(db xx=0,db yy=0) {x=xx;y=yy;}
friend comp operator + (const comp &a,const comp &b) {return comp(a.x+b.x,a.y+b.y);}
friend comp operator - (const comp &a,const comp &b) {return comp(a.x-b.x,a.y-b.y);}
friend comp operator * (const comp &a,const comp &b) {return comp(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);}
}f[N],g[N],a[N],b[N];
int limit=1,le=0,rev[N];
void fft(comp *a,int flag)
{
for(int i=0;i<limit;i++)
if(i<rev[i]) swap(a[i],a[rev[i]]);
for(int mid=1;mid<limit;mid<<=1)
{
comp wn(cos(pi/mid),(db)flag*sin(pi/mid));
for(int l=0,R=(mid<<1);l<limit;l+=R){
comp w(1,0);
for(int k=0;k<mid;k++,w=w*wn){
comp a1=a[l+k],a2=w*a[l+k+mid];
a[l+k]=a1+a2;
a[l+k+mid]=a1-a2;
}
}
}
}
vector<ll> ans[N]; //ans[p][k]
int res[N],num,len[N];
void calc(int n,int m,comp* f,comp *g){
limit=1,le=0,rev[0]=0;
while(limit<n+m+1) limit<<=1,le++;
for(int i=1;i<limit;i++) rev[i]=(rev[i>>1]>>1)|((i&1)<<(le-1));
// fo(i,0,n){
// cout<<f[i].x<<" ";
// }
// cout<<"\n";
// fo(i,0,m){
// cout<<g[i].x<<" ";
// }
// cout<<"\n";
fft(f,1);
fft(g,1);
fo(i,0,limit-1){
f[i]=f[i]*g[i]+1e-9;
}
fft(f,-1);
m++;
if(res[m]==0){
res[m]=++num;
ans[num].resize(m);
len[num]=m;
}
fd(i,2*m-1,m){
int k=2*m-1-i;
// cout<<(ll)(f[i].x/limit+0.5)<<"\n";
ans[res[m]][k]+=(ll)(f[i].x/limit+0.5001);
}
limit=limit+limit/2;
fo(i,0,limit) f[i]=g[i]=comp(0,0);
}
int v[N],p[N];
vector<int> e[N];int cnt=0;
void solve(){
int n,q;
num=0;
cin>>n>>q;
fo(i,1,n) {
cin>>p[i];
res[i]=0;
v[i]=0;
e[i].clear();
ans[i].clear();
}
fo(i,1,n) {
if(!v[i]){
int x=i;
cnt++;
while(!v[x]){
v[x]=1;
e[cnt].push_back(x);
x=p[x];
}
}
}
// fo(i,1,cnt){
// for(auto y:e[i]) cout<<y<<" ";
// cout<<"\n";
// }
int tot;
fo(i,1,cnt){
tot=-1;
for(auto t:e[i]) {
// cout<<t<<"?";
a[++tot]=comp(t+1e-6,0);
}
for(int j=0;j<=tot;j++) {
b[j]=a[tot-j];
}
for(int j=tot+1;j<=2*tot+1;j++) {
a[j]=a[j-tot-1];
}
calc(2*tot+1,tot,a,b);
}
int k;ll final;
fo(i,1,q) {
cin>>k;
final=0;
fo(j,1,num){
final+=ans[j][k%len[j]];
}
cout<<final<<"\n";
}
}
int main(){
// freopen("data.in","r",stdin);
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
solve();
return 0;
}
/*
6 4
2 4 1 3 6 5
1
2
3
4
*/
詳細信息
Test #1:
score: 100
Accepted
time: 50ms
memory: 265348kb
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: 47ms
memory: 266108kb
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: 44ms
memory: 265996kb
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: -100
Wrong Answer
time: 46ms
memory: 264420kb
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:
333334001 332835501 332338001 331841501 331346001 330851501 330358001 329865501 329374001 328883501 328394001 327905501 327418001 326931501 326446001 325961501 325478001 324995501 324514001 324033501 323554001 323075501 322598001 322121501 321646001 321171501 320698001 320225501 319754001 319283501 ...
result:
wrong answer 1st lines differ - expected: '333334000', found: '333334001'