P3702 [SDOI2017]序列计数

链接

分析:

  首先可以容斥掉,用总的减去一个质数也没有的。

  然后可以dp了,f[i][j]表示到第i个数,和在模p下是j的方案数,矩阵快速幂即可。

  另一种方法:设T[1]是一个生成函数,为选了一个数,和在模p是多少的的方案数,那么T[1] * T[1] 即选了2个的方案数,这是一个卷积的形式,但是p特别小,直接暴力计算即可,然后外面套上快速幂。

代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = , mod = ;
int pri[N], n, m, p, tot;
bool nopri[N]; struct Poly{
int a[];
Poly() { memset(a, , sizeof(a)); }
}A, B;
Poly operator * (const Poly &A, const Poly &B) {
Poly C;
for (int i = ; i < p; ++i)
for (int j = ; j < p; ++j)
(C.a[(i + j) % p] += (1ll * A.a[i] * B.a[j]) % mod) %= mod;
return C;
}
void init(int n) {
nopri[] = true;
for (int i = ; i <= n; ++i) {
if (!nopri[i]) pri[++tot] = i;
for (int j = ; j <= tot && pri[j] * i <= n; ++j) {
nopri[i * pri[j]] = true;
if (i % pri[j] == ) break;
}
}
}
Poly ksm(Poly A,int b) {
Poly res = A; b --;
while (b) {
if (b & ) res = res * A;
A = A * A;
b >>= ;
}
return res;
}
int main() {
n = read(), m = read(); p = read();
init(m);
for (int i = ; i <= m; ++i) {
A.a[i % p] ++;
if (nopri[i]) B.a[i % p] ++;
}
A = ksm(A, n); B = ksm(B, n);
cout << (A.a[] - B.a[] + mod) % mod;
return ;
}

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

  1. 洛谷P3702 [SDOI2017]序列计数

    题目大意: Alice想要得到一个长度为\(n\)的序列,序列中的数都是不超过\(m\)的正整数,而且这\(n\)个数的和是\(p\)的倍数. Alice还希望,这\(n\)个数中,至少有一个数是质数 ...

  2. 洛咕 P3702 [SDOI2017]序列计数

    和https://www.cnblogs.com/xzz_233/p/10060753.html一样,都是多项式快速幂,还比那个题水. 设\(a[i]\)表示\([1,m]\)中$ \mod p\(余 ...

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

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

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

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

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

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

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

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

  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. [翻译] ios-image-filters

    ios-image-filters https://github.com/esilverberg/ios-image-filters photoshop-style filter interface ...

  2. UNIX高级环境编程(5)Files And Directories - 文件相关时间,目录文件相关操作

     1 File Times 每个文件会维护三个时间字段,每个字段代表的时间都不同.如下表所示: 字段说明: st_mtim(the modification time)记录了文件内容最后一次被修改的时 ...

  3. 分析 org.hibernate.HibernateException: No Session found for current thread

    /**      *      * org.hibernate.HibernateException: No Session found for current thread      * 分析:ge ...

  4. (1)I/O流 (2)线程

    1.I/O流1.1 ObjectOutputStream类(重点)(1)基本概念 java.io.ObjectOutputStream类主要用于将Java对象整体写入到输出流中. 只能将支持 java ...

  5. 获取URL网页信息

    static string GetHtml(string url) {string strHTML = ""; WebClient myWebClient = new WebCli ...

  6. CentOS 7.4 yum安装LAMP环境

    配置防火墙,开启80.3306端口.CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙. #停止firewall服务 systemctl stop firewa ...

  7. vue2.* 事件结合双向数据绑定、模块化以及封装Storage实现todolist 待办事项 已经完成 和进行中持久化 06

    ceshi.vue <template> <div id="app"> <input type='text' v-model='todo' @keyd ...

  8. 多启动引导工具——AIO Boot

    该软件功能十分强大 官网介绍的也十分详尽 这里仅仅简单标记一下用来以后查找 https://www.aioboot.com/en/ 效果图: 支持多语言: 简单运用:

  9. docker常用命令(一)

    1. docker命令 docker images //查看本地镜像 docker rmi 镜像名称:标签名称 //删除一个镜像 docker rm 容器ID //删除一个容器 docker comm ...

  10. layer关闭当前窗口并刷新父窗口

    window.parent.location.reload(); var index = parent.layer.getFrameIndex(window.name); parent.layer.c ...