題意 "題目鏈接" Sol 質數的限制並沒有什麼卵用,直接容斥一下:答案 = 忽略質數總的方案 沒有質數的方案 那麼直接dp,設$f[i][j]$表示到第i個位置,當前和為j的方案數 $f[i + 1][(j + k) \% p] += f[i][j]$ 矩乘優化一下。 cpp include de ...
題意
Sol
質數的限制並沒有什麼卵用,直接容斥一下:答案 = 忽略質數總的方案 - 沒有質數的方案
那麼直接dp,設\(f[i][j]\)表示到第i個位置,當前和為j的方案數
\(f[i + 1][(j + k) \% p] += f[i][j]\)
矩乘優化一下。
#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int MAXN = 2e7 + 10, mod = 20170408, SS = 1e5 + 10;
LL GG = 1e17;
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
template<typename A, typename B> inline int add(A x, B y) {
if(x + y < 0) return x + y + mod;
else return x + y >= mod ? x + y - mod : x + y;
}
template<typename A, typename B> inline void add2(A &x, B y) {
if(x + y < 0) x = x + y + mod;
else x = (x + y >= mod ? x + y - mod : x + y);
}
template<typename A, typename B> inline int mul(A x, B y) {
return 1ll * x * y % mod;
}
int N, M, p, Lim;//1 - M, ºÍÊÇpµÄ±¶Êý
int f[SS], vis[MAXN], mu[MAXN], prime[MAXN], tot, cnt, num[SS], tim[SS], val[SS];
struct Ma {
int m[201][201];
Ma() {
memset(m, 0, sizeof(m));
}
void init() {
for(int i = 0; i <= Lim; i++) m[i][i] = 1;
}
void clear() {
memset(m, 0, sizeof(m));
}
void print() {
for(int i = 0; i <= Lim; i++, puts(""))
for(int j = 0; j <= Lim; j++)
printf("%d ", m[i][j]);
}
Ma operator * (const Ma &rhs) const {
Ma ans = {};
for(int i = 0; i <= Lim; i++)
for(int j = 0; j <= Lim; j++) {
__int128 tmp = 0;
for(int k = 0; k <= Lim; k++) {
tmp += 1ll * m[i][k] * rhs.m[k][j];
}
ans.m[i][j] = tmp % mod;
}
return ans;
}
}g;
Ma MatrixPow(Ma a, int p) {
Ma base; base.init();
while(p) {
if(p & 1) base = base * a;
a = a * a; p >>= 1;
}
return base;
}
void sieve(int N) {
vis[1] = 1; mu[1] = 1;
for(int i = 2; i <= N; i++) {
if(!vis[i]) prime[++tot] = i, mu[i] = -1;
for(int j = 1; j <= tot && i * prime[j] <= N; j++) {
vis[i * prime[j]] = 1;
if(i % prime[j]) mu[i * prime[j]] = -mu[i];
else {mu[i * prime[j]] = 0; break;}
}
}
for(int i = 1; i <= N; i++)
if(vis[i]) num[i % p]++;
}
int solve1() {//ºöÊÓÖÊÊýµÄÏÞÖÆ
for(int i = 1; i <= M; i++) f[i % p]++;
for(int j = 0; j < p; j++) {
memset(tim, 0, sizeof(tim));
memset(val, 0, sizeof(val));
int step = M;
for(int k = 1; k <= M; k++) {
int nxt = (j + k) % p;
if(tim[nxt]) {step = k - 1; break;}
tim[nxt] = 1; val[nxt]++;
}
if(step) for(int k = 0; k <= Lim; k++) g.m[k][j] = M / step * val[k];
for(int k = M / step * step + 1; k <= M; k++) g.m[(j + k) % p][j]++;
}
Ma ans = MatrixPow(g, N - 1);
int out = 0;
for(int i = 0; i <= Lim; i++) add2(out, mul(ans.m[0][i], f[i]));
return out;
}
int solve2() {//ÎÞÖÊÊý
memset(f, 0, sizeof(f));
g.clear();
for(int i = 1; i <= M; i++) f[i % p] += (vis[i]);
for(int j = 0; j < p; j++)
for(int k = 0; k < p; k++)
g.m[(j + k) % p][j] += num[k];
Ma ans = MatrixPow(g, N - 1);
int out = 0;
for(int i = 0; i <= Lim; i++)
add2(out, mul(ans.m[0][i], f[i]));
return out;
}
int main() {
N = read(); M = read(); Lim = p = read();
sieve(M);
cout << (solve1() - solve2() + mod) % mod;
return 0;
}