4818: [Sdoi2017]序列计数

Time Limit: 30 Sec  Memory Limit: 128 MB

Description

Alice想要得到一个长度为n的序列,序列中的数都是不超过m的正整数,而且这n个数的和是p的倍数。Alice还希望
,这n个数中,至少有一个数是质数。Alice想知道,有多少个序列满足她的要求。

Input

一行三个数,n,m,p。
1<=n<=10^9,1<=m<=2×10^7,1<=p<=100

Output

一行一个数,满足Alice的要求的序列数量,答案对20170408取模。

Sample Input

3 5 3

Sample Output

33

月考后日常颓水题,好久没有1A了,可能SDOI比较良心
SBDP,D[i]表示和为i的方案数,用所有的减去没有素数的,写出方程发现可以矩阵转移,然后乱敲

 #include<bits/stdc++.h>
using namespace std;
template <class _T> inline void read(_T &_x) {
int _t; bool flag = false;
while ((_t = getchar()) != '-' && (_t < '' || _t > '')) ;
if (_t == '-') _t = getchar(), flag = true; _x = _t - '';
while ((_t = getchar()) >= '' && _t <= '') _x = _x * + _t - '';
if (flag) _x = -_x;
}
using namespace std;
typedef long long LL;
const int mod = ;
const int maxv = ;
struct Mat {
int n, m, a[maxv][maxv];
Mat() {}
Mat(int x, int y):n(x), m(y) {
for (register int i = , j; i < n; ++i)
for (j = ; j < m; ++j)
a[i][j] = ;
}
inline void init() {for (int i = ; i < n; ++i) a[i][i] = ; }
inline Mat operator * (Mat B) {
Mat C(n, B.m);
for (register int i = , j, k; i < n; ++i)
for (j = ; j < B.m; ++j)
for (k = ; k < m; ++k) {
C.a[i][j] += (int)((LL)a[i][k] * B.a[k][j] % mod);
if (C.a[i][j] >= mod) C.a[i][j] -= mod;
}
return C;
}
inline Mat operator ^ (int t) {
Mat res(n, m), tmp = *this; res.init();
while (t) {
if (t & ) res = res * tmp;
tmp = tmp * tmp, t >>= ;
}
return res;
}
};
const int maxp = ;
const int maxm = ;
int n, m, p;
int cnt_p[maxp], cnt_n[maxp];
bool vis[maxm];
int prime[maxm / ], pcnt;
inline void Init() {
cnt_p[ % p] = ;
for (register int i = , j; i <= m; ++i) {
if (!vis[i]) {
prime[++pcnt] = i;
} else {
++cnt_p[i % p];
}
for (j = ; j <= pcnt && i * prime[j] <= m; ++j) {
vis[i * prime[j]] = true;
if (i % prime[j] == ) break;
}
}
int tmpa = m / p, tmpb = m % p;
for (register int i = ; i < p; ++i) {
cnt_n[i] = tmpa;
cnt_n[i] += (i && i <= tmpb);
}
}
inline int getres(Mat &a) {
Mat x(p, );
x.a[][] = ;
return ((a ^ n) * x).a[][];
}
int main() {
//freopen();
//freopen();
read(n), read(m), read(p);
Init();
Mat a(p, p), b(p, p);
for (int i = , j, to; i < p; ++i) {
for (j = ; j < p; ++j) {
to = i + j;
if (to >= p) to -= p;
a.a[i][to] = cnt_n[j];
b.a[i][to] = cnt_p[j];
}
}
int ans = getres(a) - getres(b);
if (ans < ) ans += mod;
cout << ans << endl;
return ;
}

BZOJ4818 序列计数的更多相关文章

  1. [Bzoj4818]序列计数(矩阵乘法+DP)

    Description 题目链接 Solution 容斥原理,答案为忽略质数限制的方案数减去不含质数的方案数 然后矩阵乘法优化一下DP即可 Code #include <cstdio> # ...

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

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

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

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

  4. 【BZOJ4818】序列计数(动态规划,生成函数)

    [BZOJ4818]序列计数(生成函数) 题面 BZOJ 题解 显然是求一个多项式的若干次方,并且是循环卷积 或者说他是一个\(dp\)也没有问题 发现项数很少,直接暴力乘就行了(\(FFT\)可能还 ...

  5. 【BZOJ4818】【SDOI2017】序列计数 [矩阵乘法][DP]

    序列计数 Time Limit: 30 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description Alice想要得到一个长度为n的序 ...

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

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

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

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

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

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

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

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

随机推荐

  1. 177. Convert Sorted Array to Binary Search Tree With Minimal Height【LintCode by java】

    Description Given a sorted (increasing order) array, Convert it to create a binary tree with minimal ...

  2. 访问kubernetes ingress-controller

    ingress-controller可以理解为一套反向代理系统,本身需要暴露端口到集群外部,以便客户端访问. 根据实际使用,给出两种暴露端口的方式,如下: 方案一 拓扑 说明 ingress-cont ...

  3. Selenium--调用js,对话框处理 (python)

    前言: 本次教程针对Python语言,selenium教程(调用js,对话框处理) 一.对话框处理 更多的时候我们在实际的应用中碰到的并不是简单警告框,而是提供更多功能的会话框. 本节重点: 1.打开 ...

  4. 20135337朱荟潼 Linux第六周学习总结——进程的描述和进程的创建

    朱荟潼 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课http://mooc.study.163.com/course/USTC 1000029000 第六周 进程的描述 ...

  5. 小学四则运算APP 第一个冲刺阶段 第五天

    团队成员:陈淑筠.杨家安.陈曦 团队选题:小学四则运算APP 第一次冲刺阶段时间:11.17~11.27 本次发布的是实现练习功能的成功 代码: public class CalculatorActi ...

  6. php实现文件上传,下载的常见文件配置

    配置文件,php.ini uploadfile  post_max_size 规定表单上传的最大文件:

  7. 制作U盘启动盘并重装系统

    进入网站 http://www.msdn.hk/6/209/ 在列表中选择自己需要的系统,比如win7_64,则可以选择下图系统:Windows 7 Ultimate with Service Pac ...

  8. 通过第三方软件打开sqlite

    1.SQLite Expert 使用之前,可以先下载SQLite Expert,方便查看.db3数据库 下载链接是http://www.sqliteexpert.com/    可以谷歌找到licen ...

  9. SpringBoot 7.SpringBoot 结合 Thymeleaf

    一.引入 Thymeleaf 依赖 <!-- Spring boot - thymeleaf --> <dependency> <groupId>org.sprin ...

  10. Delphi中如何实现模拟组合按键,如发送Ctrl+F的按键

    利用 keybd_event函数可实现,如下面的代码用以实现在一个公共菜单中模拟Ctrl_F按钮以调用DBGridEH的查找对话框功能:这是在一个ActionList中的某一Action的OnExec ...