BZOJ4818 序列计数
4818: [Sdoi2017]序列计数
Time Limit: 30 Sec Memory Limit: 128 MB
Description
Input
Output
Sample Input
Sample Output
#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 序列计数的更多相关文章
- [Bzoj4818]序列计数(矩阵乘法+DP)
Description 题目链接 Solution 容斥原理,答案为忽略质数限制的方案数减去不含质数的方案数 然后矩阵乘法优化一下DP即可 Code #include <cstdio> # ...
- 【BZOJ4818】[Sdoi2017]序列计数 DP+矩阵乘法
[BZOJ4818][Sdoi2017]序列计数 Description Alice想要得到一个长度为n的序列,序列中的数都是不超过m的正整数,而且这n个数的和是p的倍数.Alice还希望 ,这n个数 ...
- BZOJ4818 LOJ2002 SDOI2017 序列计数 【矩阵快速幂优化DP】*
BZOJ4818 LOJ2002 SDOI2017 序列计数 Description Alice想要得到一个长度为n的序列,序列中的数都是不超过m的正整数,而且这n个数的和是p的倍数. Alice还希 ...
- 【BZOJ4818】序列计数(动态规划,生成函数)
[BZOJ4818]序列计数(生成函数) 题面 BZOJ 题解 显然是求一个多项式的若干次方,并且是循环卷积 或者说他是一个\(dp\)也没有问题 发现项数很少,直接暴力乘就行了(\(FFT\)可能还 ...
- 【BZOJ4818】【SDOI2017】序列计数 [矩阵乘法][DP]
序列计数 Time Limit: 30 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description Alice想要得到一个长度为n的序 ...
- [BZOJ4818][SDOI2017]序列计数(动规+快速幂)
4818: [Sdoi2017]序列计数 Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 972 Solved: 581[Submit][Status ...
- [bzoj4818][Sdoi2017]序列计数_矩阵乘法_欧拉筛
[Sdoi2017]序列计数 题目大意:https://www.lydsy.com/JudgeOnline/problem.php?id=4818. 题解: 首先列出来一个递推式子 $f[i][0]$ ...
- [Sdoi2017]序列计数 [矩阵快速幂]
[Sdoi2017]序列计数 题意:长为\(n \le 10^9\)由不超过\(m \le 2 \cdot 10^7\)的正整数构成的和为\(t\le 100\)的倍数且至少有一个质数的序列个数 总- ...
- BZOJ_4818_[Sdoi2017]序列计数_矩阵乘法
BZOJ_4818_[Sdoi2017]序列计数_矩阵乘法 Description Alice想要得到一个长度为n的序列,序列中的数都是不超过m的正整数,而且这n个数的和是p的倍数.Alice还希望 ...
随机推荐
- 关于Java开发一职的经验
本人为大四软件工程学生,由于准备不充分也没有前人指点,去年10月份才赶上秋招节奏,然后签下了一家比较起来还行的公司.所以不太期望大家有求职意愿但苦于不知作何准备,所以特列以下知识点检索供大家查阅.如果 ...
- 【翻译】Brewer's CAP Theorem CAP定理
Brewer's CAP Theorem 原文地址:http://www.julianbrowne.com/article/brewers-cap-theorem Brewer’s (CAP) The ...
- strongSwan配置、运行及测试
版本信息:strongSwan v5.7.2 1. 编译 tar xvf strongswan-5.7.2.tar.gz ./configure --prefix=/usr/ --sysco ...
- mark1-git
Administrator@-20131003RY MINGW64 ~ $ pwd /c/Users/Administrator Administrator@-20131003RY MINGW64 ~ ...
- LeetCode 638 Shopping Offers
题目链接: LeetCode 638 Shopping Offers 题解 dynamic programing 需要用到进制转换来表示状态,或者可以直接用一个vector来保存状态. 代码 1.未优 ...
- C#简述(一)
详情请参考:http://www.runoob.com/csharp/csharp-tutorial.html 1.C# 是一个简单的.现代的.通用的.面向对象的编程语言,它是由微软(Microsof ...
- 『编程题全队』Alpha阶段事后诸葛亮分析
一.设想和目标 1. 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? Answer: (1)我们软件主要解决个人和团队的事务管理问题 (2)我们软件的定义明确和清楚 ...
- activiti-explorer 启动报错 Error creating bean with name 'demoDataConfiguration'
来源:http://blog.csdn.net/huangning2/article/details/9247099 Activiti database setup As said in the on ...
- python的logging日志模块
1. 简单的将日志打印到屏幕 import logging logging.debug('This is debug message') logging.info('This is info mess ...
- 一本通1622Goldbach’s Conjecture
1622:Goldbach’s Conjecture 时间限制: 1000 ms 内存限制: 524288 KB [题目描述] 原题来自:Ulm Local,题面详见:POJ 2262 ...