嘟嘟嘟
這題有那麼難????
提供一個吊打std的作法
直接令\(dp[i][j][0/1]\)表示前\(i\)個數的和模\(p\)爲\(j\),且這\(i\)個數中沒有/有質數的方案數。
先想一下暴力,枚舉第\(i\)個數是哪個,而後根據這個數是不是質數轉移便可。複雜度\(O(nmp)\)。
優化:
發現\(n \leqslant 10 ^ 9\),就能想到多項式快速冪,轉移的時候分四種狀況討論。由於\(p\)很小,暴力乘就能過,複雜度\(O(m + p^ 2 logn)\)(\(O(m)\)是篩質數複雜度)。
這 題 就 沒 了。ios
#include<cstdio> #include<iostream> #include<algorithm> #include<cmath> #include<cstring> #include<cstdlib> #include<cctype> #include<queue> #include<vector> #include<ctime> #include<assert.h> using namespace std; #define enter puts("") #define space putchar(' ') #define Mem(a, x) memset(a, x, sizeof(a)) #define In inline #define forE(i, x, y) for(int i = head[x], y; (y = e[i].to) && ~i; i = e[i].nxt) typedef long long ll; typedef double db; const int INF = 0x3f3f3f3f; const db eps = 1e-8; const int maxs = 105; const int maxm = 2e7 + 5; const int maxp = 105; const ll mod = 20170408; In ll read() { ll ans = 0; char ch = getchar(), las = ' '; while(!isdigit(ch)) las = ch, ch = getchar(); while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar(); if(las == '-') ans = -ans; return ans; } In void write(ll x) { if(x < 0) putchar('-'), x = -x; if(x >= 10) write(x / 10); putchar(x % 10 + '0'); } In void MYFILE() { #ifndef mrclr freopen("ha.in", "r", stdin); freopen("ha.out", "w", stdout); #endif } int n, m, p; In ll inc(ll a, ll b) {return a + b < mod ? a + b : a + b - mod;} int prm[maxm], v[maxm]; In void init() { for(int i = 2; i < maxm; ++i) { if(!v[i]) v[i] = i, prm[++prm[0]] = i; for(int j = 1; j <= prm[0] && i * prm[j] < maxm; ++j) { v[i * prm[j]] = prm[j]; if(i % prm[j] == 0) break; } } } ll dp[maxs][maxp][2]; In void work0() { dp[0][0][0] = 1; for(int i = 1; i <= n; ++i) { for(int j = 0; j < p; ++j) for(int k = 1; k <= m; ++k) { int t = (j + k) % p; if(v[k] ^ k) dp[i][t][0] = inc(dp[i][t][0], dp[i - 1][j][0]); dp[i][t][1] = inc(dp[i][t][1], dp[i - 1][j][1]); if(v[k] == k) dp[i][t][1] = inc(dp[i][t][1], dp[i - 1][j][0]); } } write(dp[n][0][1]), enter; } ll f[maxp][2], g[maxp][2], c[maxp][2]; In void mul(ll a[][2], ll b[][2], bool flg) { Mem(c, 0); for(int i = 0; i < p; ++i) for(int j = 0; j < p; ++j) { int t = i + j < p ? i + j : i + j - p; c[t][0] = inc(c[t][0], a[i][0] * b[j][0] % mod); c[t][1] = inc(c[t][1], a[i][1] * b[j][0] % mod); c[t][1] = inc(c[t][1], a[i][0] * b[j][1] % mod); c[t][1] = inc(c[t][1], a[i][1] * b[j][1] % mod); } memcpy(a, c, sizeof(c)); } In ll Quickpow(int n) { f[0][0] = 1; for(int i = 1; i <= m; ++i) ++g[i % p][v[i] == i]; for(; n; n >>= 1, mul(g, g, 0)) if(n & 1) mul(f, g, 1); return f[0][1]; } int main() { // MYFILE(); init(); n = read(), m = read(), p = read(); if(n <= 100) {work0(); return 0;} write(Quickpow(n)), enter; return 0; }