QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#479408 | #3840. Pass the Ball! | actor | WA | 6ms | 4376kb | C++14 | 3.7kb | 2024-07-15 17:13:49 | 2024-07-15 17:13:49 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define rep(i,l,r) for (int i = (l); i <= (r); i++)
#define per(i,r,l) for (int i = (r); i >= (l); i--)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define maxn(a, b) a = max(a, b)
#define minn(a, b) a = min(a, b)
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
typedef long double db;
const ll mod = 998244353;
mt19937 gen(114514);
ll rp(ll a,ll b) {ll res=1%mod;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
namespace fft {
const db pi = acosl(-1.0);
struct cp {
db x, y;
cp(db xx = 0, db yy = 0) {x = xx, y = yy;}
friend cp operator + (const cp& a, const cp&b) {return cp(a.x + b.x, a.y + b.y);}
friend cp operator - (const cp& a, const cp&b) {return cp(a.x - b.x, a.y - b.y);}
friend cp operator * (const cp& a, const cp&b) {return cp(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);}
};
vector<int> rev;
vector<cp> wn;
void fft(vector<cp> &a, int ty) {
int sz = a.size();
if (rev.size() != sz) {
rev.clear(); rev.resize(sz);
wn.clear(); wn.resize(sz);
int l = __lg(sz);
for (int i = 0; i < sz; i++) {
rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << l - 1);
}
wn[0] = cp(1, 0);
for (int i = 1; i < sz; i++) {
wn[i] = cp(cosl(pi * i / sz), sinl(pi * i / sz));
}
}
for (int i = 0; i < sz; i++) {
if (i < rev[i]) swap(a[i], a[rev[i]]);
}
for (int i = 1; i < sz; i <<= 1) {
for (int j = 0; j < sz; j += i << 1) {
auto w = wn.begin();
for (int k = 0; k < i; k++, w += sz / i) {
cp x = a[j + k], y = cp(w->x, ty * w->y) * a[i + j + k];
a[j + k] = x + y;
a[i + j + k] = x - y;
}
}
}
return ;
}
vector<cp> mul(const vector<cp> &_a, const vector<cp> &_b) {
auto a = _a, b = _b;
int nd = a.size() + b.size() - 2, sz = 1;
while (sz <= nd) sz <<= 1;
a.resize(sz);
b.resize(sz);
fft(a, 1);
fft(b, 1);
vector<cp> res(sz);
for (int i = 0; i < sz; i++) {
res[i] = a[i] * b[i];
}
fft(res, -1);
// for (int i = 0; i < sz; i++) res[i].x /= sz;
return res;
};
};
const int N = 400;
ll sum[N][N], mus[N * N];
int main() {
int n, q;
scanf("%d%d", &n, &q);
VI to(n + 1);
vector<ll> ans(n + 1);
rep(i,1,n) {
scanf("%d", &to[i]);
}
vector<bool> vis(n + 1);
rep(i,1,n) {
int u = i;
vector<fft::cp> f;
while (!vis[u]) {
f.pb(fft::cp(u, 0));
vis[u] = true;
u = to[u];
}
if (!f.empty()) {
int sz = SZ(f);
auto g = f;
reverse(all(g));
auto res = fft::mul(f, g);
if (sz<N) {
rep(i,0,SZ(res)-1) {
sum[sz][(i + 1) % sz] += (res[i].x + 0.5) / SZ(res);
}
} else {
rep(i,0,n - 1) mus[i] += (res[(i + sz - 1) % sz].x + 0.5) / SZ(res);
}
}
}
while (q--) {
int k;
scanf("%d", &k);
ll ans = mus[k % n];
rep(i,1,N-1) {
ans += sum[i][k % i];
}
printf("%lld\n", ans);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 4000kb
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: 1ms
memory: 4376kb
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: 1ms
memory: 4260kb
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: 6ms
memory: 4148kb
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:
1000 2999 5996 9990 14980 20965 27944 35916 44880 54835 65780 77714 90636 104545 119440 135320 152184 170031 188860 208670 229460 251229 273976 297700 322400 348075 374724 402346 430940 460505 491040 522544 555016 588455 622860 658230 694564 731861 770120 809340 849520 890659 932756 975810 1019820 1...
result:
wrong answer 1st lines differ - expected: '333334000', found: '1000'