QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#479400 | #3840. Pass the Ball! | actor | RE | 0ms | 3956kb | C++14 | 3.7kb | 2024-07-15 17:08:26 | 2024-07-15 17:08:27 |
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 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 = acos(-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(cos(pi * i / sz), sin(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];
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: 0ms
memory: 3956kb
input:
4 4 2 4 1 3 1 2 3 4
output:
25 20 25 30
result:
ok 4 lines
Test #2:
score: -100
Runtime Error
input:
3 6 2 3 1 1 2 3 999999998 999999999 1000000000