QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#402080 | #8543. Periodic Sequence | oscaryang | TL | 0ms | 0kb | C++20 | 2.3kb | 2024-04-29 21:07:41 | 2024-04-29 21:07:41 |
answer
#include<bits/stdc++.h>
#define vc vector
#define pb emplace_back
#define pii pair<int, int>
#define mkp make_pair
#define rep(i, a, b) for(int i = (a); i <= (b); ++i)
#define lep(i, a, b) for(int i = (a); i >= (b); --i)
using namespace std;
mt19937 gen(time(0));
inline int read() {
int x = 0, w = 0; char ch = getchar(); while(!isdigit(ch)) w |= (ch == '-'), ch = getchar();
while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar(); return w ? -x : x;
}
const int N = 2e5 + 5, B = 450;
int MOD = 998244353;
// CALC
inline void inc (int &x, int y) { x += y - MOD; x += (x >> 31) & MOD; }
inline int qpow (int a, int b) { int res = 1; for(; b; b >>= 1, a = 1ll * a * a % MOD) if(b & 1) res = 1ll * res * a % MOD; return res; }
inline int qinv (int a) { return qpow(a, MOD - 2); }
struct mint {
int x;
mint (int ix = 0) { x = ix % MOD; if(x < 0) x += MOD; }
mint operator += (mint a) { inc (x, a.x); return a; }
mint operator -= (mint a) { inc (x, MOD - a.x); return a; }
mint operator *= (mint a) { x = 1ll * x * a.x % MOD; return a; }
mint friend operator + (mint a, mint b) { a += b; return a; }
mint friend operator - (mint a, mint b) { a -= b; return a; }
mint friend operator * (mint a, mint b) { a *= b; return a; }
} fc[N], ifc[N], pw[N], ans[N], f[N][B + 5];
inline void init (int n) {
fc[0] = ifc[0] = pw[0] = 1;
rep (i, 1, n) fc[i] = fc[i - 1] * i, pw[i] = pw[i - 1] * 2;
ifc[n] = qinv (fc[n].x);
lep (i, n - 1, 1) ifc[i] = ifc[i + 1] * (i + 1);
}
inline mint C (int a, int b) { return a < b ? 0 : fc[a] * ifc[b] * ifc[a - b]; }
inline void prework (int n) {
// k <= B
rep (i, 1, B) {
vc <mint> dp (n + 1, 0), s (n + 1, 0);
dp[0] = s[0] = 1;
rep (j, 1, n) {
dp[j] = s[j - 1] - (j > i ? s[j - i - 1] : 0);
if (j >= i) ans[j] += dp[j - i];
s[j] = s[j - 1] + dp[j];
}
}
rep (i, 1, n) ans[i] += ans[i - 1];
// k > B
mint val;
rep (x, 0, B - 1) for (int y = 0; y + (x + 1) * (B + 1) <= n; y ++) {
val = C (y, x) * pw[y - x];
if (x & 1) val = 0 - val;
f[y + (x + 1) * (B + 1)][x + 1] += val;
}
rep (i, 1, n) {
rep (j, 1, B) if (i >= j) f[i][j] += f[i - j][j];
rep (j, 1, B) ans[i] += f[i][j];
}
}
signed main() {
int t = read (); MOD = read ();
init (2e5);
prework (2e5);
while (t --) printf ("%d\n", ans[read ()].x);
return 0;
}
詳細信息
Test #1:
score: 0
Time Limit Exceeded
input:
5 1000000007