https://www.luogu.org/problemnew/show/P3702

题目让我们在 $ [1, m] $ 从中选出 $ n $ 个数,当中要有 > $ 0 $ 个质数,和是 $ p $ 的倍数,直接求是很难求的,我们可以用所有方案减去不含素数的方案,这样求出来的东西就是答案

现在我们考虑如何计算出和是 $ p $ 的倍数的方案

我们令 $ f (x) $ 表示当前选出的数的和模 $ p $ 是 $ x $ 的方案数,把两个这样的多项式进行卷积

设两个多项式分别为 $ f1 $ 和 $ f2 $

$ Ans[i + j - 1] $ 这一项的值包含 $ f1[i] * f2[j] $ 的值,而其他项不包含

我们把下标 $ -1 $ 后

$ Ans[i + j - 2] $ 这一项的值包含 $ f1[i - 1] * f2[j - 1] $ 的值,而其他项不包含

再回到多项式的定义,我们发现,$ Ans[i + j - 2] += f1[i - 1] * f2[j - 1] $ 的意义就是选出的和模 $ p $ 是 $ i - 1 $ 的情况和选出的和模 $ p $ 是 $ j - 1 $ 合在一起,即选出来的数的和模 $ p $ 是 $ i + j - 2 $ 的方案数

所以我们可以先构造出初始的多项式 $ f $,求出 $ f^n(p) $ 的值,即为方案数

然后答案就是所有方案减去不含素数的方案

卷积可以写一个三模 NTT,当然这题数据范围小,直接暴力卷积也可以(跑的比 NTT 还快),但是 FFT 会爆精度(至少我爆了精度),建议暴力卷积

我的代码中有三种卷积方法的实现,可以作为参考

  1. NTT
#include <bits/stdc++.h>
#define double long double
#define CIOS ios::sync_with_stdio(false);
#define For(i, a, b) for(register int i = a; i <= b; i++)
#define Rof(i, a, b) for(register int i = a; i >= b; i--)
#define DEBUG(x) cerr << "DEBUG" << x << " >>> ";
using namespace std; typedef unsigned long long ull;
typedef long long ll; template <typename T>
inline void read(T &f) {
f = 0; T fu = 1; char c = getchar();
while(c < '0' || c > '9') {if(c == '-') fu = -1; c = getchar();}
while(c >= '0' && c <= '9') {f = (f << 3) + (f << 1) + (c & 15); c = getchar();}
f *= fu;
} template <typename T>
void print(T x) {
if(x < 0) putchar('-'), x = -x;
if(x < 10) putchar(x + 48);
else print(x / 10), putchar(x % 10 + 48);
} template <typename T>
void print(T x, char t) {
print(x); putchar(t);
} const int N = 405, mod = 20170408;
inline int mul(int x, int y) { return (int)(1ll * x * y % (ll)mod); } int r[N], n, m, p, len = 1, tot = 0;
ll A[N], B[N], C[N], ans[N], a[N], b[N], c[N], d[N]; const int P[3] = {469762049, 998244353, 167772161}, G = 3, Gi[3] = {P[0] / G + 1, P[1] / G + 1, P[2] / G + 1}; ll fpow(ll x, ll y, ll p) {
ll ans = 1;
while(y) {
if(y & 1) ans = ans * x % p;
y >>= 1; x = x * x % p;
}
return ans;
} void NTT(ll *a, int tp, int t) {
for(register int i = 1; i < len; i++) if(i < r[i]) swap(a[i], a[r[i]]);
for(register int mid = 1; mid < len; mid <<= 1) {
ll wn = fpow(tp == 1 ? G : Gi[t], (P[t] - 1) / (mid << 1), P[t]);
for(register int i = 0; i < len; i += (mid << 1)) {
ll w = 1;
for(register int j = 0; j < mid; j++, w = w * wn % P[t]) {
ll x = a[i + j], y = a[i + j + mid] * w % P[t];
a[i + j] = (x + y) % P[t]; a[i + j + mid] = (x - y + P[t]) % P[t];
}
}
}
if(tp == -1) {
ll inv = fpow(len, P[t] - 2, P[t]);
for(register int i = 0; i < len; i++) a[i] = a[i] * inv % P[t];
}
} ll inv(ll x, ll y) { return fpow(x, y - 2, y); } ll times(ll x, ll y, ll p) {
ll ans = 0;
while(y) {
if(y & 1) ans = (ans + x) % p;
y >>= 1; x = (x + x) % p;
}
return ans;
} void mul(ll *A, ll *B, ll *ans) {
for(register int i = 1; i <= p; i++) A[i - 1] = A[i], B[i - 1] = B[i]; A[p] = B[p] = 0;
len = 1; tot = 0;
while(len <= (p << 1)) len <<= 1, tot++;
for(register int i = 1; i < len; i++) r[i] = (r[i >> 1] >> 1) | ((i & 1) << (tot - 1));
memcpy(a, A, sizeof(a)); memcpy(b, A, sizeof(b)); memcpy(c, A, sizeof(c));
memcpy(d, B, sizeof(d)); NTT(a, 1, 0); NTT(d, 1, 0); for(register int i = 0; i < len; i++) a[i] = a[i] * d[i] % P[0]; NTT(a, -1, 0);
memcpy(d, B, sizeof(d)); NTT(b, 1, 1); NTT(d, 1, 1); for(register int i = 0; i < len; i++) b[i] = b[i] * d[i] % P[1]; NTT(b, -1, 1);
memcpy(d, B, sizeof(d)); NTT(c, 1, 2); NTT(d, 1, 2); for(register int i = 0; i < len; i++) c[i] = c[i] * d[i] % P[2]; NTT(c, -1, 2);
for(register int i = 0; i <= (p << 1); i++) {
ll p = 1ll * P[0] * P[1];
ll s = times(a[i], times(P[1], inv(P[1], P[0]), p), p) + times(b[i], times(P[0], inv(P[0], P[1]), p), p); s %= p;
ll k = (c[i] % P[2] - s % P[2] + P[2]) % P[2] * inv(p % P[2], P[2]) % P[2];
ll Ans = (s + (k * P[0] % mod * P[1] % mod)) % mod;
ans[i + 1] = Ans;
}
for(register int i = 1; i <= p; i++) ans[i] = (ans[i] + ans[i + p]) % mod, ans[i + p] = 0;
for(register int i = p; i >= 1; i--) A[i] = A[i - 1], B[i] = B[i - 1]; A[0] = B[0] = 0;
} int fpow(int x, int y) {
int ans = 1;
while(y) {
if(y & 1) ans = mul(ans, x);
y >>= 1; x = mul(x, x);
}
return ans;
} int pri[5000000], plen;
bool isp[20000005]; void init_pri(int n, ll *a) {
isp[1] = 1;
for(register int i = 2; i <= n; i++) {
if(!isp[i]) { pri[++plen] = i; }
for(register int j = 1; j <= plen && i * pri[j] <= n; j++) {
isp[i * pri[j]] = 1;
if(i % pri[j] == 0) break;
}
}
for(register int i = 1; i <= p; i++) a[i] = 0;
for(register int i = 1; i <= n; i++) if(isp[i]) ++a[i % p + 1];
} int main() {
read(n); read(m); read(p); init_pri(m, A); int t = n;
ans[1] = 1;
while(n) {
if(n & 1) {
memcpy(B, ans, sizeof(B));
mul(A, B, ans);
}
n >>= 1;
memcpy(B, A, sizeof(B));
memcpy(C, A, sizeof(C));
mul(B, C, A);
}
int fir = ans[1];
memset(ans, 0, sizeof(ans));
memset(A, 0, sizeof(A));
for(register int i = 1; i <= m; i++) ++A[i % p + 1];
ans[1] = 1; n = t;
while(n) {
if(n & 1) {
memcpy(B, ans, sizeof(B));
mul(A, B, ans);
}
n >>= 1;
memcpy(B, A, sizeof(B));
memcpy(C, A, sizeof(C));
mul(B, C, A);
}
cout << (ans[1] - fir + mod) % mod << endl;
return 0;
}

2.FFT & 暴力卷积(当然 FFT 是错误的)

#include <bits/stdc++.h>
#define CIOS ios::sync_with_stdio(false);
#define For(i, a, b) for(register int i = a; i <= b; i++)
#define Rof(i, a, b) for(register int i = a; i >= b; i--)
#define DEBUG(x) cerr << "DEBUG" << x << " >>> ";
using namespace std; typedef unsigned long long ull;
typedef long long ll; template <typename T>
inline void read(T &f) {
f = 0; T fu = 1; char c = getchar();
while(c < '0' || c > '9') {if(c == '-') fu = -1; c = getchar();}
while(c >= '0' && c <= '9') {f = (f << 3) + (f << 1) + (c & 15); c = getchar();}
f *= fu;
} template <typename T>
void print(T x) {
if(x < 0) putchar('-'), x = -x;
if(x < 10) putchar(x + 48);
else print(x / 10), putchar(x % 10 + 48);
} template <typename T>
void print(T x, char t) {
print(x); putchar(t);
} const int N = 405, P = 20170408;
inline int mul(int x, int y) { return (int)(1ll * x * y % (ll)P); } /*const double PI = acos(-1.0); struct cp {
double x, y;
cp (double xx = 0, double yy = 0) {
x = xx; y = yy;
}
}a[N], b[N]; cp operator + (const cp a, const cp b) { return cp(a.x + b.x, a.y + b.y); }
cp operator - (const cp a, const cp b) { return cp(a.x - b.x, a.y - b.y); }
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); }*/ int r[N], n, m, p, len = 1, tot = 0;
int A[N], B[N], C[N], ans[N]; /*void FFT(cp *a, int d) {
for(register int i = 1; i < len; i++) if(i < r[i]) swap(a[i], a[r[i]]);
for(register int mid = 1; mid < len; mid <<= 1) {
cp wn = cp(cos(PI / mid), sin(PI / mid) * d);
for(register int i = 0; i < len; i += (mid << 1)) {
cp w = cp(1.0, 0.0);
for(register int j = 0; j < mid; j++, w = w * wn) {
cp x = a[i + j], y = a[i + j + mid] * w;
a[i + j] = x + y; a[i + j + mid] = x - y;
}
}
}
}*/ /*void mul(int *A, int *B, int *ans) {
len = 1, tot = 0;
while(len <= (p << 1)) len <<= 1, tot++;
for(register int i = 1; i <= len; i++) r[i] = (r[i >> 1] >> 1) | ((i & 1) << (tot - 1));
for(register int i = 1; i <= len; i++) a[i - 1] = cp(A[i], 0), b[i - 1] = cp(B[i], 0);
FFT(a, 1); FFT(b, 1);
for(register int i = 0; i < len; i++) a[i] = a[i] * b[i];
FFT(a, -1);
for(register int i = 1; i <= len; i++) ans[i] = (int)((ll)(a[i - 1].x / (double)len + 0.5) % (ll)P);
for(register int i = 1; i <= p; i++) ans[i] = (ans[i] + ans[i + p]) % P, ans[i + p] = 0;
}*/ /*void mul(int *A, int *B, int *ans) {
for(register int i = 1; i <= p << 1; i++) A[i - 1] = A[i], B[i - 1] = B[i], ans[i] = 0; ans[0] = 0;
for(register int i = 0; i < p; i++) {
for(register int j = 0; j < p; j++) {
ans[i + j] += mul(A[i], B[j]);
if(ans[i + j] >= P) ans[i + j] -= P;
}
}
for(register int i = p << 1; i >= 1; i--) ans[i] = ans[i - 1], A[i] = A[i - 1], B[i] = B[i - 1]; A[0] = B[0] = 0;
for(register int i = 1; i <= p; i++) ans[i] = (ans[i] + ans[i + p]) % P, ans[i + p] = 0;
}*/ void mul(int *A, int *B, int *ans) {
memset(ans, 0, (N + 1) * 4);
for(register int i = 1; i <= p; i++) {
for(register int j = 1; j <= p; j++) {
ans[i + j - 1] += mul(A[i], B[j]);
ans[i + j - 1] %= P;
}
}
for(register int i = 1; i <= p; i++) ans[i] = (ans[i] + ans[i + p]) % P, ans[i + p] = 0;
} int fpow(int x, int y) {
int ans = 1;
while(y) {
if(y & 1) ans = mul(ans, x);
y >>= 1; x = mul(x, x);
}
return ans;
} int pri[5000000], plen;
bool isp[20000005]; void init_pri(int n, int *a) {
isp[1] = 1;
for(register int i = 2; i <= n; i++) {
if(!isp[i]) { pri[++plen] = i; }
for(register int j = 1; j <= plen && i * pri[j] <= n; j++) {
isp[i * pri[j]] = 1;
if(i % pri[j] == 0) break;
}
}
for(register int i = 1; i <= p; i++) a[i] = 0;
for(register int i = 1; i <= n; i++) if(isp[i]) ++a[i % p + 1];
} int main() {
read(n); read(m); read(p); init_pri(m, A); int t = n;
ans[1] = 1;
while(n) {
if(n & 1) {
memcpy(B, ans, sizeof(B));
mul(A, B, ans);
}
n >>= 1;
memcpy(B, A, sizeof(B));
memcpy(C, A, sizeof(C));
mul(B, C, A);
}
int fir = ans[1];
memset(ans, 0, sizeof(ans));
memset(A, 0, sizeof(A));
for(register int i = 1; i <= m; i++) ++A[i % p + 1];
ans[1] = 1; n = t;
while(n) {
if(n & 1) {
memcpy(B, ans, sizeof(B));
mul(A, B, ans);
}
n >>= 1;
memcpy(B, A, sizeof(B));
memcpy(C, A, sizeof(C));
mul(B, C, A);
}
cout << (ans[1] - fir + P) % P << endl;
return 0;
}

最新文章

  1. Lambda GroupBy Sum
  2. Lumia 830 win10m 启用触摸按键
  3. Docker-2 的创建、启动、终止、删除、迁移等
  4. Libevent windows/linux下编译
  5. mysql-添加删除字段
  6. windows phone 中的TextBlock的一些特性(TextWrapping,TextWrapping)
  7. orczhou----MYSQL
  8. node.js 访问sql server的 node_modules “msnodesql&quot;的安装编译方法
  9. Java基础知识强化之IO流笔记20:FileOutputStream写出数据实现换行和追加写入
  10. HDOJ 1058 Humble Numbers(打表过)
  11. 【转】Android LCD(二):LCD常用接口原理篇
  12. BestCoder Round #38
  13. vim中多行注释 和 删除多行注释
  14. UVa1025 (DAG上的dp)
  15. ACM1008
  16. 2016/1/9:深度剖析安卓Framebuffer设备驱动
  17. Jjava8 Lambda 神操作
  18. SublimeText3按ctrl+b执行python无反应
  19. Maximum Average Subarray I LT643
  20. Linux - 针对用户账号的常用操作

热门文章

  1. 蓝桥杯练习系统算法训练习题加答案java版本
  2. Unity牛逼的2D纹理功能
  3. 75. Sort Colors (Array)
  4. mysql数据库优化总结 有图 有用
  5. CentOS 6.5 下安装 Sun JDK 1.7
  6. array_combine()
  7. centos7如何知道jdk的在哪个目录
  8. javascrip总结42:属性操作案例: 点击盒子,改变盒子的位置和背景颜色
  9. C# JackLib系列之自定义鼠标风格的实现
  10. Exception (2) Java Exception Handling