QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#646403 | #3840. Pass the Ball! | lllei | WA | 7ms | 37380kb | C++20 | 3.4kb | 2024-10-16 22:47:17 | 2024-10-16 22:47:17 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int BS = 400;
const double Pi = acos(-1.0);
struct Complex
{
double x, y;
Complex(int X , int Y ) { x = X, y = Y; }
Complex(double X = 0, double Y = 0) { x = X, y = Y; }
Complex operator+(const Complex b) const { return Complex(x + b.x, y + b.y); }
Complex operator-(const Complex b) const { return Complex(x - b.x, y - b.y); }
Complex operator*(const Complex b) const { return Complex(x * b.x - y * b.y, y * b.x + x * b.y); }
double get() { return x; }
};
int limit = 0, L = 0, rev[1000005];
void initfft(int len)
{
limit = 1, L = 0;
while (limit <= len)
limit <<= 1, L++;
for (int i = 0; i < limit; i++)
rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (L - 1));
}
void FFT(Complex *A, int type)
{
for (int i = 0; i < limit; i++)
if (rev[i] < i)
swap(A[i], A[rev[i]]);
for (int mid = 1; mid < limit; mid <<= 1)
{
Complex w = Complex(cos(Pi / mid), type * sin(Pi / mid));
for (int j = 0; j < limit; j += mid << 1)
{
Complex wn = Complex(1, 0);
for (int k = 0; k < mid; k++, wn = wn * w)
{
Complex x = A[j + k], y = wn * A[j + k + mid];
A[j + k] = x + y, A[j + k + mid] = x - y;
}
}
}
}
Complex A[1000005], B[1000005];
int n, q;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> q;
vector<int> to(n + 1);
vector<int> vis(n + 1, 0);
vector<vector<long long>> b;
vector<vector<long long>> c(BS, vector<long long>(BS));
for (int i = 1; i <= n; i++)
cin >> to[i];
for (int i = 1; i <= n; i++)
if (!vis[i])
{
vector<int> tmp;
for (int j = i; !vis[j]; j = to[j])
{
vis[j] = 1;
tmp.push_back(j);
}
if (tmp.size() < BS)
{
int siz = tmp.size();
for (int j = 0; j < siz; j++)
for (int k = 0; k < siz; k++)
{
int t = (k < j ? k + siz - j : k - j);
c[siz][t] += tmp[j] * 1ll * tmp[k];
}
}
else
{
vector<long long> ss;
int siz = tmp.size();
initfft(siz * 2);
for (int j = 0; j < limit; j++)
A[j] = B[j] = Complex(0, 0);
for (int j = 0; j < siz; j++)
A[j] = Complex(tmp[j], 0);
for (int j = siz; j < 2 * siz; j++)
A[j] = Complex(tmp[j-siz], 0);
for (int j = 0; j < siz; j++)
B[j] = Complex(tmp[siz - 1 - j],0);
FFT(A, 1);
FFT(B, 1);
for (int j = 0; j < limit; j++)
A[j] = A[j] * B[j];
FFT(A, -1);
for (int j = siz - 1; j < 2 * siz - 1; j++)
ss.push_back(A[j].get());
b.push_back(ss);
}
}
while (q--)
{
long long ans = 0;
int k;
cin >> k;
for (int i = 1; i < BS; i++)
ans += c[i][k % i];
for (const auto &i:b)
ans += i[k % i.size()];
cout<<ans<<'\n';
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 37172kb
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: 0ms
memory: 35920kb
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: 0ms
memory: 36128kb
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: 7ms
memory: 37380kb
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:
682668031999 681647103999 680628223999 679611391999 678596607999 677583871999 676573183999 675564543999 674557951999 673553407999 672550911999 671550463999 670552063999 669555711999 668561407999 667569151999 666578943999 665590783999 664604671999 663620607999 662638591999 661658623999 660680703999 6...
result:
wrong answer 1st lines differ - expected: '333334000', found: '682668031999'