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. RHEL7: How to configure a rc-local service

    问题: linux7 /etc/rc.local 不生效: [root@bogon mysql3306]# uname -aLinux bogon 3.10.0-862.el7.x86_64 #1 S ...

  2. [翻译] BKZoomView

    BKZoomView https://github.com/freshking/BKZoomView A UIView that will zoom into its parent view. It ...

  3. 铁乐学python_day28_模块学习3

    大部份内容摘自授课老师的博客http://www.cnblogs.com/Eva-J/ OS模块复习一二 >>> import os >>> os.getcwd() ...

  4. PHP中抽象方法、抽象类和接口的用法

    在类中,没有方法体的方法就是抽象方法. abstract 可见性 function 方法名称(参数1,.....);      // 如果没有显示地指定可见性,则默认为public 如: public ...

  5. Spring+微信小程序 卡券打通

    近期公司项目需要使用到微信卡券模块,主要做的是在小程序打通微信卡券,实现小程序领取卡券的功能效果. 简单说下涉及的东西: Springboot—使用springboot做后端接口,非常便捷 并且根本是 ...

  6. Springboot 设置session超时

    使用版本 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring ...

  7. 4-1 R语言函数 lapply

    #lapply函数 #可以循环处理列表中的每一个元素 #lapply(参数):lapply(列表,函数/函数名,其他参数) #总是返回一个列表 #sapply:简化结果 #结果列表元素长度均为1,返回 ...

  8. 使用jenkins管理uirecorder录制的任务

    在uirecorder官网(http://uirecorder.com/)上,对jenkins的配置只有简单的几句话: How to dock Jenkins? Add commands source ...

  9. B. Vile Grasshoppers

    http://codeforces.com/problemset/problem/937/B The weather is fine today and hence it's high time to ...

  10. Angular动态表单生成(七)

    动态表单生成之拖拽生成表单(上) 这个功能就比较吊炸天了,之前的六篇,都是ng-dynamic-forms自带的功能,可能很多的说明官方的文档都已经写了,我只是个搬运工,而在这篇文章中,我将化身一个工 ...