QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#646405#3840. Pass the Ball!llleiWA 11ms37376kbC++203.4kb2024-10-16 22:48:062024-10-16 22:48:07

Judging History

This is the latest submission verdict.

  • [2024-10-16 22:48:07]
  • Judged
  • Verdict: WA
  • Time: 11ms
  • Memory: 37376kb
  • [2024-10-16 22:48:06]
  • Submitted

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 * 3);
                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';
    }
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 37304kb

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: 7ms
memory: 37376kb

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: 3ms
memory: 37364kb

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: 11ms
memory: 36580kb

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:

1365336063999
1363294207999
1361256447999
1359222783999
1357193215999
1355167743999
1353146367999
1351129087999
1349115903999
1347106815999
1345101823999
1343100927999
1341104127999
1339111423999
1337122815999
1335138303999
1333157887999
1331181567999
1329209343999
1327241215999
1325277183999
132331...

result:

wrong answer 1st lines differ - expected: '333334000', found: '1365336063999'