QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#234462#3840. Pass the Ball!tselmegkh#WA 2ms3988kbC++172.7kb2023-11-01 17:31:022023-11-01 17:31:04

Judging History

This is the latest submission verdict.

  • [2023-11-01 17:31:04]
  • Judged
  • Verdict: WA
  • Time: 2ms
  • Memory: 3988kb
  • [2023-11-01 17:31:02]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
typedef complex<double> C;
typedef vector<double> vd;
#define vi vector<int>
#define all(a) a.begin(),a.end()
#define sz(x) (int)x.size()
#define rep(x, s, e) for(int x = s; x < e; x++)

void fft(vector<C>& a) {
	int n = sz(a), L = 31 - __builtin_clz(n);
	static vector<complex<long double>> R(2, 1);
	static vector<C> rt(2, 1);  // (^ 10% faster if double)
	for (static int k = 2; k < n; k *= 2) {
		R.resize(n); rt.resize(n);
		auto x = polar(1.0L, acos(-1.0L) / k);
		rep(i,k,2*k) rt[i] = R[i] = i&1 ? R[i/2] * x : R[i/2];
	}
	vi rev(n);
	rep(i,0,n) rev[i] = (rev[i / 2] | (i & 1) << L) / 2;
	rep(i,0,n) if (i < rev[i]) swap(a[i], a[rev[i]]);
	for (int k = 1; k < n; k *= 2)
		for (int i = 0; i < n; i += 2 * k) rep(j,0,k) {
			// C z = rt[j+k] * a[i+j+k]; // (25% faster if hand-rolled)  /// include-line
			auto x = (double *)&rt[j+k], y = (double *)&a[i+j+k];        /// exclude-line
			C z(x[0]*y[0] - x[1]*y[1], x[0]*y[1] + x[1]*y[0]);           /// exclude-line
			a[i + j + k] = a[i + j] - z;
			a[i + j] += z;
		}
}
vector<ll> conv(const vector<ll>& a, const vector<ll>& b) {
	if (a.empty() || b.empty()) return {};
	vector<ll> res(sz(a) + sz(b) - 1);
	int L = 32 - __builtin_clz(sz(res)), n = 1 << L;
	vector<C> in(n), out(n);
	copy(all(a), begin(in));
	rep(i,0,sz(b)) in[i].imag(b[i]);
	fft(in);
	for (C& x : in) x *= x;
	rep(i,0,n) out[i] = in[-i & (n - 1)] - conj(in[i]);
	fft(out);
	rep(i,0,sz(res)) res[i] = imag(out[i]) / (4 * n);
	return res;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, q;
    cin >> n >> q;
    vector<int> a(n);
    for (int &i : a) cin >> i, i--;
    vector<vector<ll>> s(n + 1);
    vector<int> v;
    vector<bool> vis(n);
    for (int i = 0; i < n; i++) {
        if (vis[i]) continue;
        vector<int> cyc{i};
        vis[i] = 1;
        while (a[cyc.back()] != i) {
            cyc.push_back(a[cyc.back()]);
            vis[cyc.back()] = 1;
        }
        vector<ll> A;
        for (int i : cyc) A.push_back(i + 1);
        for (int i : cyc) A.push_back(i + 1);
        vector<ll> B;
        for (int i : cyc) B.push_back(i + 1);
        reverse(B.begin(), B.end());
        auto D = conv(A, B);
        int m = cyc.size();
        if (s[m].empty()) {
            v.push_back(m);
            s[m].resize(m + 1);
        }
        for (int i = m - 1; i < 2 * m - 1; i++) {
            s[m][i - m + 1] += D[i];
        }
    }
    while (q--) {
        int k;
        cin >> k;
        ll ans = 0;
        for (int i : v) {
            ans += s[i][k % i];
        }
        cout << ans << "\n";
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 3648kb

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: 3652kb

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: 0
Accepted
time: 2ms
memory: 3824kb

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:

333334000
332835500
332338000
331841500
331346000
330851500
330358000
329865500
329374000
328883500
328394000
327905500
327418000
326931500
326446000
325961500
325478000
324995500
324514000
324033500
323554000
323075500
322598000
322121500
321646000
321171500
320698000
320225500
319754000
319283500
...

result:

ok 10000 lines

Test #5:

score: -100
Wrong Answer
time: 2ms
memory: 3988kb

input:

1000 10000
187 493 316 665 124 40 448 649 657 65 438 730 816 107 789 286 309 469 169 216 488 52 212 111 541 83 990 48 282 867 36 220 676 241 959 372 322 244 481 708 595 957 215 223 120 658 291 176 229 158 431 492 221 986 889 861 606 518 106 349 410 765 745 812 563 998 150 392 358 328 747 793 587 507...

output:

250347169
248662078
245260552
253150327
247096579
249698948
249942589
251180693
248589849
253775352
248472247
248369272
249001282
249611560
251718722
248202948
252492155
251442262
255269934
247070745
248898892
250071493
249262069
247714054
248954719
251676093
251650611
249152315
248608212
249678723
...

result:

wrong answer 4th lines differ - expected: '253150328', found: '253150327'