QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#639559 | #3840. Pass the Ball! | laonongmin | WA | 1ms | 4228kb | C++23 | 2.8kb | 2024-10-13 20:22:42 | 2024-10-13 20:22:42 |
Judging History
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];
vector<ll> A,B;
void solve()
{
cin>>n>>q;
for(int i=1;i<=n;++i) cin>>p[i];
int len = 1<<(Log2[n-1]+1);
A.resize(len);
B.resize(len);
for(int u=1,i=0;i<n;++i,u=p[u]) A[i]=B[n-i-1]=u;
Polynomial FA(A), FB(B);
for(int i=0;i<len;++i) FA.c[i] = FA.c[i] * FB.c[i];
FA.fft(FA.c, len, -1);
while(q--)
{
int k; cin>>k;
cout<<(ll)(FA.c[(k+n-1)%n].x+0.5)<<'\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: 4228kb
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: 4212kb
input:
3 6 2 3 1 1 2 3 999999998 999999999 1000000000
output:
6 8 14 8 14 6
result:
wrong answer 1st lines differ - expected: '11', found: '6'