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;
}

luoguP3702 [SDOI2017]序列计数的更多相关文章

  1. [BZOJ 4818/LuoguP3702][SDOI2017] 序列计数 (矩阵加速DP)

    题面: 传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=4818 Solution 看到这道题,我们不妨先考虑一下20分怎么搞 想到暴力,本蒟 ...

  2. [Sdoi2017]序列计数 [矩阵快速幂]

    [Sdoi2017]序列计数 题意:长为\(n \le 10^9\)由不超过\(m \le 2 \cdot 10^7\)的正整数构成的和为\(t\le 100\)的倍数且至少有一个质数的序列个数 总- ...

  3. BZOJ_4818_[Sdoi2017]序列计数_矩阵乘法

    BZOJ_4818_[Sdoi2017]序列计数_矩阵乘法 Description Alice想要得到一个长度为n的序列,序列中的数都是不超过m的正整数,而且这n个数的和是p的倍数.Alice还希望 ...

  4. 【BZOJ 4818】 4818: [Sdoi2017]序列计数 (矩阵乘法、容斥计数)

    4818: [Sdoi2017]序列计数 Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 560  Solved: 359 Description Al ...

  5. P3702 [SDOI2017]序列计数

    P3702 [SDOI2017]序列计数 链接 分析: 首先可以容斥掉,用总的减去一个质数也没有的. 然后可以dp了,f[i][j]表示到第i个数,和在模p下是j的方案数,矩阵快速幂即可. 另一种方法 ...

  6. 【BZOJ4818】[Sdoi2017]序列计数 DP+矩阵乘法

    [BZOJ4818][Sdoi2017]序列计数 Description Alice想要得到一个长度为n的序列,序列中的数都是不超过m的正整数,而且这n个数的和是p的倍数.Alice还希望 ,这n个数 ...

  7. BZOJ4818 LOJ2002 SDOI2017 序列计数 【矩阵快速幂优化DP】*

    BZOJ4818 LOJ2002 SDOI2017 序列计数 Description Alice想要得到一个长度为n的序列,序列中的数都是不超过m的正整数,而且这n个数的和是p的倍数. Alice还希 ...

  8. [BZOJ4818][SDOI2017]序列计数(动规+快速幂)

    4818: [Sdoi2017]序列计数 Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 972  Solved: 581[Submit][Status ...

  9. [bzoj4818][Sdoi2017]序列计数_矩阵乘法_欧拉筛

    [Sdoi2017]序列计数 题目大意:https://www.lydsy.com/JudgeOnline/problem.php?id=4818. 题解: 首先列出来一个递推式子 $f[i][0]$ ...

随机推荐

  1. solr的客户端操作:使用solrj进行curd操作

    导入相关的jar包 <dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-s ...

  2. JSP的原理、JSP的执行过程

    Jsp的本质是servlet, 通过response的printWriter返回,response的getOutputStream只能调用一次,返回流就不能返回页面刷新. JSP起源 在很多动态网页中 ...

  3. html学习代码

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  4. 145. Binary Tree Postorder Traversal (Stack, Tree)

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  5. ubuntu eclipse opencv环境配置

    项目——Properties——C/C++ Build——Settings 配置包含目录: GCC C++ Compiler   ——Includes /usr/include /usr/local/ ...

  6. Opencv Convex Hull (凸包)

    #include <iostream>#include <opencv2/opencv.hpp> using namespace std;using namespace cv; ...

  7. Python学习笔记_使用openpyxl操作Excel,在同一个文件里复制某一个sheet

    应用场景:定制一个Excel模板文件,其中定义了一个模板Sheet,以此模板文件里的模板sheet为样例,制作报表,里面有不止一个模板样例Sheet 一.软件环境: 1.OS:Win10 64位 2. ...

  8. scala的隐式转换

    摘要: 通过隐式转换,程序员可以在编写Scala程序时故意漏掉一些信息,让编译器去尝试在编译期间自动推导出这些信息来,这种特性可以极大的减少代码量,忽略那些冗长,过于细节的代码.   使用方式: 1. ...

  9. [Email] 收发邮件的协议 : IMAP and SMTP , POP3 and SMTP

    支持 IMAP 和 SMTP 的应用 与仅同步收件箱的 POP 不同,IMAP 同步所有电子邮件文件夹. 在电子邮件应用中使用以下设置. 接收 (IMAP) 服务器 服务器地址:imap-mail.o ...

  10. JDBC 连接 MySQL 时碰到的小坑

    最近从MS SQL Server换到了MySQL,已经是8.11版本了,安装的时候似乎还用了新的身份认证方式之类的,连接过程中也是磕磕绊绊,碰到很多奇奇怪怪的问题,在此记录下来. 驱动加载: 以前使用 ...