题面

题解

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] 序列计数的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. P3702 [SDOI2017]序列计数

    P3702 [SDOI2017]序列计数 链接 分析: 首先可以容斥掉,用总的减去一个质数也没有的. 然后可以dp了,f[i][j]表示到第i个数,和在模p下是j的方案数,矩阵快速幂即可. 另一种方法 ...

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

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

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

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

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

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

随机推荐

  1. 【原创】大叔经验分享(84)spark sql中设置hive.exec.max.dynamic.partitions无效

    spark 2.4 spark sql中执行 set hive.exec.max.dynamic.partitions=10000; 后再执行sql依然会报错: org.apache.hadoop.h ...

  2. MongoDB知识小结

    一.术语 RDBMS MongoDB 数据库 数据库 表格 集合 行 文档 列 字段 表联合 嵌套文档 主键 主键 (MongoDB 提供了 key 为 _id ) 数据库 数据库名可以是满足以下条件 ...

  3. robot framework 如何获取隐藏元素的文本,以及可见元素的文本

    1.下图是获取可见元素的文本内容,运行后得到:${B_name}=公告管理:假设公告管理不可见,那么${B_name}=‘’(为空)

  4. 改写Unity DropDown 支持多次点击同一选项均回调

    [很久前的一个Note,不知道现在的Unity Dropdown是否已经支持该特性] Unity UGUI是开源的: https://bitbucket.org/Unity-Technologies/ ...

  5. 4.闭锁 CountDownLatch

    /*CountDownLatch 闭锁*/ CountDownLatch 是一各同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待 闭锁可以延迟线程的进度 直到 其到达终 ...

  6. windows环境安装haproxy及初步配置负载均衡使用示例

    安装HaProxy 首先需要下载windows环境下需要文件,这里下载的是一个别人编译好的一个文件,这里省去了编译的过程,使用的版本是haproxy-1.7.8. 下载后直接解压到对应的目录下.示例( ...

  7. 批量更新sql

    跨库 批量更新 UPDATE a.table_1upINNER JOIN ( SELECT user_id, user_org_company_id, FROM b.table_2) AS tmp O ...

  8. 阿里P7浅谈SpringMVC

    一.前言 既然是浅谈 SpringMVC,那么我们就先从基础说起,本章节主要讲解以下内容: 1.三层结构介绍 2.MVC 设计模式介绍 3.SpringMVC 介绍 4.入门程序的实现 注:介绍方面的 ...

  9. 二零一八阿里p7笔试116题

    1. junit用法,before,beforeClass,after, afterClass的执行顺序 2. 分布式锁 3. nginx的请求转发算法,如何配置根据权重转发 4. 用hashmap实 ...

  10. 【leetcode】637. Average of Levels in Binary Tree

    原题 Given a non-empty binary tree, return the average value of the nodes on each level in the form of ...