[题解] [SDOI2015] 序列统计
题面
题解
设 \(f[i][j]\) 代表长度为 \(i\) 的序列, 乘积模 \(m\) 为 \(j\) 的序列有多少个
转移方程如下
\]
复杂度是 \(O(nm^2)\) 的
考虑倍增, 用类似快速幂那样的东西
\]
恩, 复杂度变为了 \(O(m^2logn)\) 的
继续优化
上式相当于一个东西, 看到这个地方
\]
如果是这样一种形式
\]
我们就可以用 NTT 优化了
我们知道对数可以把乘法转成加法
但是对数是一个实数, 我们需要考虑一个模意义下的对数
把原根当做底数就可以了, 于是我们将上式转化为
\]
考虑到 \(log_gx+log_gy\) 可能会大于 \(m\)
但是它一定不会大于 \(2m\) , 所以我们对于 \(c[z]\) 这个位置, 加上 \(c[z + m - 1]\) , 再将 \(c[z + m - 1]\) 清零即可
Code
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <map>
const int N = 40005;
const int mod = 1004535809;
using namespace std;
int n, m, X, S, lim, cnt, r[N], g, gg, a[N], b[N], res[N], f[N], top, fact[20005];
map<int, int> mp;
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;
}
int fpow(int x, int y, int p)
{
int res = 1;
for( ; y; y >>= 1, x = 1ll * x * x % p)
if(y & 1) res = 1ll * res * x % p;
return res;
}
int getroot(int x)
{
top = 0;
int rem = x - 1, p = rem;
for(int i = 2; i * i <= x; i++)
if(!(rem % i))
{
fact[++top] = i;
while(!(rem % i)) rem /= i;
}
if(rem > 1) fact[++top] = rem;
for(int flag = 1, i = 2; i <= p; i++, flag = 1)
{
for(int j = 1; j <= top && flag; j++)
if(fpow(i, p / fact[j], x) == 1) flag = 0;
if(flag) return i;
}
return -1;
}
void ntt(int *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)
{
int rt = fpow(opt == 1 ? g : gg, (mod - 1) / (i << 1), mod);
for(int j = 0; j < lim; j += (i << 1))
{
int w = 1;
for(int k = j; k < j + i; k++, w = 1ll * w * rt % mod)
{
int x = p[k], y = 1ll * w * p[k + i] % mod;
p[k] = (1ll * x + y) % mod, p[k + i] = (1ll * x - y + mod) % mod;
}
}
}
if(opt == -1)
{
int inv = fpow(lim, mod - 2, mod);
for(int i = 0; i < lim; i++) a[i] = 1ll * a[i] * inv % mod;
}
}
void mul(int *A, int *B, int *C)
{
for(int i = 0; i < lim; i++) a[i] = A[i], b[i] = B[i];
ntt(a, 1), ntt(b, 1);
for(int i = 0; i < lim; i++) a[i] = 1ll * a[i] * b[i] % mod;
ntt(a, -1);
for(int i = 0; i < m - 1; i++) a[i] = (1ll * a[i] + a[i + m - 1]) % mod, a[i + m - 1] = 0;
for(int i = 0; i < lim; i++) C[i] = a[i];
}
int main()
{
n = read <int> (), m = read <int> (), X = read <int> (), S = read <int> ();
g = getroot(m), gg = fpow(g, m - 2, m);
for(int tmp = 1, i = 0; i < m - 1; i++, tmp = 1ll * tmp * g % m) mp[tmp] = i;
for(int x, i = 1; i <= S; i++)
{
x = read <int> ();
if(x) f[mp[x]]++;
}
res[mp[1]] = 1;
for(lim = 1; lim <= 2 * m; lim <<= 1, cnt++); cnt--;
for(int i = 0; i < lim; i++) r[i] = (r[i >> 1] >> 1) | ((i & 1) << cnt);
g = getroot(mod), gg = fpow(g, mod - 2, mod);
while(n)
{
if(n & 1) mul(res, f, res);
mul(f, f, f);
n >>= 1;
}
printf("%d\n", res[mp[X]]);
return 0;
}
[题解] [SDOI2015] 序列统计的更多相关文章
- 【题解】SDOI2015序列统计
[题解]SDOI2015序列统计 来自永不AFO的YYB的推荐 这里是乘积,比较麻烦,不过由于给定的序列膜数是个小质数,所以可以\(O(m^2\log m)\)找原跟(实际上不需要这么多). 乘积有点 ...
- [BZOJ 3992][SDOI2015]序列统计
3992: [SDOI2015]序列统计 Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 2275 Solved: 1090[Submit][Stat ...
- 【LG3321】[SDOI2015]序列统计
[LG3321][SDOI2015]序列统计 题面 洛谷 题解 前置芝士:原根 我们先看一下对于一个数\(p\),它的原根\(g\)有什么性质(好像就是定义): \(g^0\%p,g^1\%p,g^2 ...
- 【BZOJ3992】[SDOI2015]序列统计 NTT+多项式快速幂
[BZOJ3992][SDOI2015]序列统计 Description 小C有一个集合S,里面的元素都是小于M的非负整数.他用程序编写了一个数列生成器,可以生成一个长度为N的数列,数列中的每个数都属 ...
- BZOJ 3992: [SDOI2015]序列统计 快速幂+NTT(离散对数下)
3992: [SDOI2015]序列统计 Description 小C有一个集合S,里面的元素都是小于M的非负整数.他用程序编写了一个数列生成器,可以生成一个长度为N的数列,数列中的每个数都属于集合S ...
- BZOJ 3992: [SDOI2015]序列统计 [快速数论变换 生成函数 离散对数]
3992: [SDOI2015]序列统计 Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 1017 Solved: 466[Submit][Statu ...
- [SDOI2015]序列统计
[SDOI2015]序列统计 标签: NTT 快速幂 Description 给你一个模m意义下的数集,需要用这个数集生成一个数列,使得这个数列在的乘积为x. 问方案数模\(1004535809\). ...
- 3992: [SDOI2015]序列统计
3992: [SDOI2015]序列统计 链接 分析: 给定一个集和s,求多少个长度为n的序列,满足序列中每个数都属于s,并且所有数的乘积模m等于x. 设$f=\sum\limits_{i=0}^{n ...
- [BZOJ3992][SDOI2015]序列统计(DP+原根+NTT)
3992: [SDOI2015]序列统计 Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 1888 Solved: 898[Submit][Statu ...
随机推荐
- JS OOP -04 JS中的公有成员,私有成员和静态成员
JS中的公有成员,私有成员和静态成员 a.实现类的公有成员 b.实现类的私有成员 c.实现类的静态成员 a.实现类的公有成员 之前定义的任何类型成员都属于公有成员的范畴,该类的任何实例都对外公开这些属 ...
- Html5+Mui前端框架,开发记录(一)
1.下载HBuilder X,地址:http://www.dcloud.io/hbuilderx.html,根据需求选择版本下载. Mui文档,地址:http://dev.dcloud.net.cn/ ...
- 编译 recastnavigation
1. https://github.com/memononen/recastnavigation 下载zip并解压 2. 打开https://www.libsdl.org/download-2.0 ...
- 前端知识总结--js异步事件顺序
js中异步事件中容易混淆的 Promise 和 setTimeout 的执行顺序是怎样的? setTimeout(() => console.log(1), 0); new Promise(fu ...
- python2.7.5安装docker-compose的方法
yum -y install epel-release && yum install -y python-pip && pip install --upgrade pi ...
- ccs编译.lib
新建 New一个CCS Project Output type选择"Static Library" 添加源文件 右击工程 -> Add Files- 编译 编译生成的.lib ...
- C#基础 类及常用函数【string 、Math 、DiteTime 、TimeSpan】
一 string 类型 string str = "abcdefg"; str.Length - 字符串长度,返回int类型 str.TrimStart() ...
- javaWeb文件上传与下载
文件上传与下载在项目中运用的使用频率很大 今天也花时间整理了一下 多文件上传图片回显 和文件下载 1.多文件上传 这里会涉及到几个属性 fileSizeThreshold:缓冲区文件的大小 如果上传 ...
- C和指针--预处理器
编译一个C程序的第1个步骤是预处理(preprocessing)阶段.C预处理器在源代码编译之前对其进行一些文本性质的操作.它的主要任务包括删除注释.插入被#include指令包含的文件的内容.定义和 ...
- linux 基础10-磁盘配额管理
1. 基本概念 1.1 概念: 在linux系统中,由于是多人多任务的使用环境,所以会有多人共同使用一个硬盘空间的情况,如果其中少数几个人大量使用了硬盘空间的话,势必会压缩其他使用者的使用空间,因此管 ...