1 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
// Zhong Ziqian: Time: 15.82 s Mem: 13.7 MB
#pragma GCC optimize("-O3","-funroll-all-loops")
#include <vector>
#include <stdio.h> // printf, scanf
#include <math.h> // sqrt
#include <string.h> // memset
#include <stdlib.h> // atoi
#include <algorithm> // __gcd
using namespace std;
typedef long long ll;
int f[1001][1001], MOD;
int s[11][30001];
int T;
int g[200001], h[2][200001], t[200001];
ll ms[11];
/// a to power b mod MOD
ll qp(ll a, ll b) {
ll x = 1;
a %= MOD;
while (b) {
if (b & 1) x = x * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return x;
}
ll lcm(ll a, ll b) {
return a / __gcd(a, b) * b;
}
/// This is used when n is <= 10
/// The program also assumes that MOD == 1000000007 in that case
/// (which is likely, but not technically part of the constraints).
struct Newton {
ll y[11], g[11];
int N;
void work() {
ll ii = 1;
for (int i = 1; i <= N; ++i)
y[i] = (y[i] % MOD + MOD) % MOD;
for (int i = 1; i <= N; ++i) {
g[i] = y[1] * ii % MOD;
ii = ii * qp(i, MOD-2) % MOD;
for (int j = 1; j <= N - i; ++j) {
y[j] = y[j + 1] - y[j];
if (y[j] < 0)
y[j] += MOD;
}
y[N - i + 1] = 0;
}
}
ll calc(int x) {
ll w = 1, a = 0;
for (int i = 1; i <= N; ++i) {
a = (a + w * g[i]) % MOD;
w = w * (x - i) % MOD;
}
a = (a % MOD + MOD) % MOD;
return a;
}
} ns[11][2520];
int main() {
scanf("%d%d", &T, &MOD);
/// Init table used if n and m are <= 1000 (and n > 10)
f[0][0] = 1;
for (int i = 1; i <= 1000; ++i)
for (int j = 0; j <= 1000; ++j) {
f[i][j] = f[i - 1][j];
if (j >= i)
(f[i][j] += f[i][j - i]) %= MOD;
}
/// Init table used if n <= 10
if (MOD == 1000000007) {
s[0][0] = 1;
for (int i = 1; i <= 10; ++i)
for (int j = 0; j <= 30000; ++j) {
s[i][j] = s[i - 1][j];
if (j >= i)
(s[i][j] += s[i][j - i]) %= MOD;
}
ms[0] = 1;
for (int i = 1; i <= 10; ++i)
ms[i] = lcm(ms[i - 1], i);
for (int i = 2; i <= 10; ++i) {
for (int s = 0; s < ms[i]; ++s) {
int cn = 0;
for (int j = s; j <= 30000 && cn <= 29; j += ms[i])
ns[i][s].y[ns[i][s].N = ++cn] = ::s[i][j];
ns[i][s].work();
}
}
}
while (T--) {
int n;
static char s[100001];
scanf("%d%s", &n, s);
if (n == 1) {
puts("1");
continue;
}
/// Subtask 3: T:1,100000 N:1,10 M:1,10^100000 MOD:1e9+7
if (n <= 10) {
ll w = 0;
for (int i = 0; s[i]; ++i)
w = (w * 10 + s[i] - '0') % (ms[n] * MOD);
printf("%lld\n", ns[n][w % ms[n]].calc(w / ms[n] + 1));
continue;
}
int m = atoi(s);
/// Subtask 1: T:1,100000 N:1,1000 M:1,1000 MOD:1,1e9+7
if (n <= 1000 && m <= 1000)
printf("%d\n", f[n][m]);
/// Subtask 2: T:1 N:1,200000 M:1,200000 MOD:1,1e9+7
else {
int S = sqrt(max(n,m));
if (S > n)
S = n;
if (!S)
++S;
memset(g, 0, sizeof g);
g[0] = 1;
for (int i = 1; i < S; ++i)
for (int j = i; j <= m; ++j)
(g[j] += g[j-i]) %= MOD;
memset(h, 0, sizeof h);
memset(t, 0, sizeof t);
int c = 0;
h[c][0] = 1;
for (int i = 0; i * S <= m; ++i) {
c ^= 1;
for (int j = 0; j + i * S <= m; ++j) {
h[c][j] = h[c ^ 1][j];
if (i && j >= i) {
(h[c][j] += h[c][j - i]) %= MOD;
if (j - i >= n - S)
(h[c][j] -= h[c ^ 1][j - i - (n - S)]) %= MOD;
}
(t[j + i * S] += h[c][j]) %= MOD;
}
}
ll ans = 0;
for (int i = 0; i <= m; ++i)
ans = (ans + g[i] * (ll)t[m-i]) % MOD;
ans = (ans % MOD + MOD) % MOD;
printf("%d\n", int(ans));
}
}
}
|