[题解] [SDOI2017] 序列计数
题面
题解
和 SDOI2015 序列统计 比较像
这个无非就是把乘改成了加, NTT 改成了 MTT
再加上了一个小小的容斥 : 拿所有方案减去不合法方案即可
Code
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
const int N = 1005;
const int mod = 20170408;
const double pi = acos(-1);
typedef long long ll;
using namespace std;
int n, m, lim, cnt, a[N], b[N], res1[N], res2[N], vis[20000005], stk[5000005], top, P, r[N];
struct Complex
{
double a, b;
Complex(double x = 0, double y = 0) { a = x, b = y; }
Complex operator + (const Complex &p) const { return Complex(a + p.a, b + p.b); }
Complex operator - (const Complex &p) const { return Complex(a - p.a, b - p.b); }
Complex operator * (const Complex &p) const { return Complex(a * p.a - b * p.b, a * p.b + b * p.a); }
} c[N], d[N], e[N], f[N], A[N], B[N], C[N], D[N];
template < typename T >
inline T read()
{
T x = 0, w = 1; char c = getchar();
while(c < '0' || c > '9') { if(c == '-') w = -1; c = getchar(); }
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * w;
}
void del(int *p)
{
for(int i = 2; i <= m; i++)
{
if(!vis[i]) stk[++top] = i, b[i % P]--;
for(int j = 1; i * stk[j] <= m; j++)
{
vis[i * stk[j]] = 1;
if(!(i % stk[j])) break;
}
}
}
void fft(Complex *p, int opt)
{
for(int i = 0; i < lim; i++) if(i < r[i]) swap(p[i], p[r[i]]);
for(int i = 1; i < lim; i <<= 1)
{
Complex rt = Complex(cos(pi / i), opt * sin(pi / i));
for(int j = 0; j < lim; j += (i << 1))
{
Complex w = Complex(1, 0);
for(int k = j; k < j + i; k++, w = w * rt)
{
Complex x = p[k], y = w * p[k + i];
p[k] = x + y, p[k + i] = x - y;
}
}
}
if(opt == -1)
{
for(int i = 0; i < lim; i++)
p[i].a = (ll) (p[i].a / lim + 0.5) % mod;
}
}
int sum(int x, int y) { return (1ll * x << y) % mod; }
void mul(int *a, int *b, int *ans)
{
for(int i = 0; i < lim; i++)
{
c[i].a = a[i] >> 15, c[i].b = 0, d[i].a = a[i] & 32767, d[i].b = 0;
e[i].a = b[i] >> 15, e[i].b = 0, f[i].a = b[i] & 32767, f[i].b = 0;
}
fft(c, 1), fft(d, 1), fft(e, 1), fft(f, 1);
for(int i = 0; i < lim; i++)
{
A[i] = c[i] * e[i], B[i] = c[i] * f[i];
C[i] = e[i] * d[i], D[i] = d[i] * f[i];
}
fft(A, -1), fft(B, -1), fft(C, -1), fft(D, -1);
for(int i = 0; i < lim; i++)
ans[i] = (((1ll * sum((ll) A[i].a % mod, 30) + sum((ll) B[i].a % mod, 15)) % mod
+ sum((ll) C[i].a % mod, 15)) % mod + sum((ll) D[i].a % mod, 0)) % mod;
for(int i = 0; i < lim; i++)
ans[i] = (1ll * ans[i] + ans[i + P]) % mod, ans[i + P] = 0;
}
int main()
{
n = read <int> (), m = read <int> (), P = read <int> ();
for(lim = 1; lim <= 2 * P; lim <<= 1, cnt++); cnt--;
for(int i = 0; i < lim; i++) r[i] = (r[i >> 1] >> 1) | ((i & 1) << cnt);
for(int i = 0; i < P; i++)
b[i] = a[i] = m / P + (i && i <= m % P);
del(b);
res1[0] = 1, res2[0] = 1;
while(n)
{
if(n & 1)
mul(res1, a, res1), mul(res2, b, res2);
mul(a, a, a), mul(b, b, b);
n >>= 1;
}
printf("%d\n", (res1[0] - res2[0] + mod) % mod);
return 0;
}
MTT太丑了
[题解] [SDOI2017] 序列计数的更多相关文章
- 【BZOJ4818】[Sdoi2017]序列计数 DP+矩阵乘法
[BZOJ4818][Sdoi2017]序列计数 Description Alice想要得到一个长度为n的序列,序列中的数都是不超过m的正整数,而且这n个数的和是p的倍数.Alice还希望 ,这n个数 ...
- [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还希望 ...
- 【BZOJ 4818】 4818: [Sdoi2017]序列计数 (矩阵乘法、容斥计数)
4818: [Sdoi2017]序列计数 Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 560 Solved: 359 Description Al ...
- P3702 [SDOI2017]序列计数
P3702 [SDOI2017]序列计数 链接 分析: 首先可以容斥掉,用总的减去一个质数也没有的. 然后可以dp了,f[i][j]表示到第i个数,和在模p下是j的方案数,矩阵快速幂即可. 另一种方法 ...
- BZOJ4818 LOJ2002 SDOI2017 序列计数 【矩阵快速幂优化DP】*
BZOJ4818 LOJ2002 SDOI2017 序列计数 Description Alice想要得到一个长度为n的序列,序列中的数都是不超过m的正整数,而且这n个数的和是p的倍数. Alice还希 ...
- [BZOJ4818][SDOI2017]序列计数(动规+快速幂)
4818: [Sdoi2017]序列计数 Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 972 Solved: 581[Submit][Status ...
- [BZOJ 4818/LuoguP3702][SDOI2017] 序列计数 (矩阵加速DP)
题面: 传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=4818 Solution 看到这道题,我们不妨先考虑一下20分怎么搞 想到暴力,本蒟 ...
随机推荐
- 数据库入门(mySQL):数据操作与查询
增删改 单表查询 多表查询 一.增删改 1.插入数据记录(增) insert into table_name(field1,field2,field3,...fieldn) valuses(value ...
- win中使用curl上传文件报错
今天晚上复现“WordPress插件Easy WP SMTP反序列化漏洞”时,需要使用curl上传文件,我又用的windows环境,一直出错 curl: (26) couldn't open file ...
- ADF一个EO的事物提交周期
客户端通过传递键对象调用实体定义的findByPrimaryKey(),获得EO.ADF框架首先检查实体缓存, 如果在实体缓存中没有找到实体,就执行SQL SELECT查询,从数据库读取行.示例如下: ...
- undefined reference to `udev_device_get_action'
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libusb-1.0.a(libusb_1_0_la-linux_udev.o): In ...
- Django应用之content type(app应用之一django.contrib.contenttypes)
当一张表作为多个表的FK(主键),并且只能选择其中一个或者几个时,就可以使用content_type表:例如下图的数据关系,因此就可以使用content_type表来将表与表中的对象进行关联,从而做到 ...
- MySQL之Text Protocol
1)[01]COM_QUIT 告诉服务器,客户端想要关闭连接 返回:或者关闭一个连接或者一个OK_Packet 有效负载: 1 [01]COM_QUIT 字段: command(1)--0x01 CO ...
- 什么是领域模型(domain model)?贫血模型(anaemic domain model)和充血模型(rich domain model)有什么区别
领域模型是领域内的概念类或现实世界中对象的可视化表示,又称为概念模型或分析对象模型,它专注于分析问题领域本身,发掘重要的业务领域概念,并建立业务领域概念之间的关系. 贫血模型是指使用的领域对象中只有s ...
- nginx动静分离简单实例实现
什么是动静分离? Nginx 动静分离简单来说就是把动态和静态请求分开,不能理解成只是将动态页面和静态页面物理分离.严格意义上说应该是动态请求和静态请求分开,可以理解成使用 nginx 处理静态页面, ...
- 实例化Vue时的两种挂载方式el与$mount
el 与mount 都是挂载. el vue官网的介绍https://cn.vuejs.org/v2/api/#el mount vue官网的介绍 https://cn.vuejs.org/v2/ap ...
- 堆排序Heap_Sort
堆排序就是借助二叉堆进行排序,不了解二叉堆的可以先看这里.本文以升序排序为例,首先将待排序数组放置在最小堆中,此时堆顶一定是数组中最小的元素,然后删除堆顶元素,此时调整后的最小堆顶会是第二小的元素,从 ...