BZOJ 4818

感觉不难。

首先转化一下题目,“至少有一个质数”$=$“全部方案”$ - $“一个质数也没有”。

注意到$m \leq 2e7$,$[1, m]$内的质数可以直接筛出来。

设$f_{i, j}$表示当前长度序列为$i$,当前和模$p$的值是$j$的方案数,直接无脑枚举$m$转移复杂度是$O(nmp)$的,但是发现每一次转移形式都是相同的。

$$f_{i, x} = \sum f_{i - 1, y}(y + z \equiv x(\mod p))$$

其实在模$p$的意义下大于等于$p$的数可以直接归类到这个数模$p$这一档里面,也就是说,我们可以记一个$cnt_x$表示模$p$意义下相同的数有$x$个。

$$f_{i, (x + y) \mod p} = \sum f_{i - 1, x} \times cnt_y$$

发现这个式子的形式很像矩阵快速幂的样子,然后就把转移写成矩阵的形式快速幂一下就好了。

转移矩阵的第$(i, j)$个格子是$\sum_{(i + k) \equiv j(\mod p)}cnt_k$

时间复杂度$O(m + p^3logn)$。

咕,感觉时间刚刚好。

然而再次观察一下这个式子发现是一个卷积的形式,因此可以直接$NTT$,时间复杂度可以降到$O(m + plogplogn)$,但是在这题中$p$太小了$ + $模数不好,直接暴力卷积的时间表现应该比$NTT$要优秀。

Code:

#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll; const int N = 2e7 + ;
const int M = ;
const ll P = 20170408LL; int n, m, K, pCnt = , pri[N], cnt[M];
bool np[N]; template <typename T>
inline void inc(T &x, T y) {
x += y;
if (x >= P) x -= P;
} template <typename T>
inline void sub(T &x, T y) {
x -= y;
if (x < ) x += P;
} struct Matrix {
int tn, tm;
ll s[M][M]; inline void init() {
tn = tm = ;
memset(s, , sizeof(s));
} friend Matrix operator * (const Matrix x, const Matrix y) {
Matrix res;
res.init();
res.tn = x.tn, res.tm = y.tm;
for (int k = ; k < x.tm; k++)
for (int i = ; i < x.tn; i++)
for (int j = ; j < y.tm; j++)
inc(res.s[i][j], x.s[i][k] * y.s[k][j] % P);
return res;
} inline Matrix fpow(int y) {
Matrix x = *this, res;
res.init();
res.tn = x.tn, res.tm = x.tm;
for (int i = ; i < x.tn; i++) res.s[i][i] = ;
for (; y ; y >>= ) {
if (y & ) res = res * x;
x = x * x;
}
return res;
} inline void print() {
for (int i = ; i < tn; i++)
for (int j = ; j < tm; j++)
printf("%lld%c", s[i][j], " \n"[j == tm - ]);
printf("\n");
} } trans, ans; inline void sieve() {
np[] = ;
for (int i = ; i <= m; i++) {
if (!np[i]) pri[++pCnt] = i;
for (int j = ; j <= pCnt && pri[j] * i <= m; j++) {
np[i * pri[j]] = ;
if (i % pri[j] == ) break;
}
}
} inline ll solve1() {
memset(cnt, , sizeof(cnt));
for (int i = ; i <= m; i++) ++cnt[i % K]; trans.init();
trans.tn = trans.tm = K;
for (int i = ; i < K; i++)
for (int j = ; j < K; j++)
inc(trans.s[i][(i + j) % K], 1LL * cnt[j]);
// trans.print(); trans = trans.fpow(n); // trans.print(); ans.init();
ans.s[][] = ;
ans.tn = , ans.tm = K;
ans = ans * trans;
return ans.s[][];
} inline ll solve2() {
sieve();
memset(cnt, , sizeof(cnt));
for (int i = ; i <= m; i++)
if (np[i]) ++cnt[i % K]; /* for (int i = 0; i < K; i++)
printf("%d%c", cnt[i], " \n"[i == K - 1]); */ trans.init();
trans.tn = trans.tm = K;
for (int i = ; i < K; i++)
for (int j = ; j < K; j++)
inc(trans.s[i][(i + j) % K], 1LL * cnt[j]);
// trans.print(); trans = trans.fpow(n); // trans.print(); ans.init();
ans.s[][] = ;
ans.tn = , ans.tm = K;
ans = ans * trans;
return ans.s[][];
} int main() {
scanf("%d%d%d", &n, &m, &K);
// printf("%lld\n", solve1());
// printf("%lld\n", solve2());
printf("%lld\n", (solve1() - solve2() + P) % P);
return ;
}

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

  1. luogu 3702 [SDOI2017]序列计数 矩阵乘法+容斥

    现在看来这道题真的不难啊~ 正着求不好求,那就反着求:答案=总-全不是质数 这里有一个细节要特判:1不是质数,所以在算全不是质数的时候要特判1 code: #include <bits/stdc ...

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

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

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

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

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

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

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

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

  6. P3702 [SDOI2017]序列计数

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

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

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

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

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

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

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

随机推荐

  1. iperf/iperf3网络测试工具的安装与使用

    1.官网及下载路径: iperf3 homepage at: http://software.es.net/iperf/Report bugs to: https://github.com/esnet ...

  2. ACdream区域赛指导赛之手速赛系列(2)

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/DaiHaoC83E15/article/details/26187183        回到作案现场 ...

  3. PCB 设计文件中哪些可以不做成元件

    PCB 文件中哪些可以不做成元件 在 PCB 设计时有些元件为了快速,实际并不需要做成元件. 因为做成元件会浪费时间,并且生产时也没有实际的元件. 如下(持续更新): PCB 与 PCB 之前的连接人 ...

  4. 关于AM335X移植SDIO WIFI的简易教程(转)

    最近应一个朋友邀请,帮他移植了SDIO WIFI到3.2版本内核.因为之前已经成功移植了3.14内核,所以整个过程花了一个下午就完成了.话不多说,先交待一下平台: CPU:TI AM3352 600M ...

  5. Web验证方式(2)--Form Authentication

    Form验证方式并不是HTTP标准,而是在微软ASP.NET Web框架下提供的一种验证方式.其大致流程如下: 在上图的流程中,ASP.NET框架提供了如下支持类:( FormsAuthenticat ...

  6. linux CentOS 安装rz和sz命令 lrzsz 实现windows和linux之间的文件上传 下载

    https://blog.nbhao.org/1902.html https://bbs.csdn.net/topics/391989523 https://www.cnblogs.com/zhoul ...

  7. (转)为C# Windows服务添加安装程序

    本文转载自:http://kamiff.iteye.com/blog/507129 最近一直在搞Windows服务,也有了不少经验,感觉权限方面确定比一般程序要受限很多,但方便性也很多.像后台运行不阻 ...

  8. 学习笔记之C# 教程 | 菜鸟教程

    C# 教程 | 菜鸟教程 http://www.runoob.com/csharp/csharp-tutorial.html 菜鸟教程在线编辑器 http://www.runoob.com/try/r ...

  9. 学习笔记之Tips for Macbook

    写给Mac新手的入门指南 - 威锋网 https://mp.weixin.qq.com/s/pqmqGZhNwevx57KeLnzZmg https://bbs.feng.com/read-htm-t ...

  10. 生成Excel

    生成Excel 需要引用MyXls.SL2.dll的类库: 下载地址:http://sourceforge.net/projects/myxls/ 命名空间using org.in2bits.MyXl ...