BZOJ 3992

点开这道题之后才发现我对原根的理解大概只停留在$998244353$的原根是$3$……

关于原根: 点我

首先写出$dp$方程,设$f_{i, j}$表示序列长度为$i$当前所有数乘积模$m$为$j$的方案数,有转移

$$f_{i, x * y \mod m} = \sum_{y \in s} f_{i - 1, x}$$

把$x$和$y$取个对数就可以变成卷积的形式了。

然而在模意义下,我们可以用原根的$k$次方来代替原来的数,这样子就达到了取对数的效果。

注意到每一次转移形式都是相同的,我们可以用快速幂的方式来优化。

时间复杂度$O(mlogmlogn)$。

然而我的代码只有在有$c++11$的时候才是对的,哪位大佬如果知道了为什么教教我呗。

Code:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector <ll> poly; const int N = ; int n, m, tar, fac[N], buc[N]; namespace Poly {
const int L = << ;
const ll P = 1004535809LL; int lim, pos[L]; template <typename T>
inline void inc(T &x, T y) {
x += y;
if (x >= P) x -= P;
} inline ll fpow(ll x, ll y, ll mod = P) {
ll res = ;
for (; y > ; y >>= ) {
if (y & ) res = res * x % mod;
x = x * x % mod;
}
return res;
} inline void prework(int len) {
int l = ;
for (lim = ; lim < len; lim <<= , ++l);
for (int i = ; i < lim; i++)
pos[i] = (pos[i >> ] >> ) | ((i & ) << (l - ));
} inline void ntt(poly &c, int opt) {
c.resize(lim, );
for (int i = ; i < lim; i++)
if (i < pos[i]) swap(c[i], c[pos[i]]);
for (int i = ; i < lim; i <<= ) {
ll wn = fpow(, (P - ) / (i << ));
if (opt == -) wn = fpow(wn, P - );
for (int len = i << , j = ; j < lim; j += len) {
ll w = ;
for (int k = ; k < i; k++, w = w * wn % P) {
ll x = c[j + k], y = w * c[j + k + i] % P;
c[j + k] = (x + y) % P, c[j + k + i] = (x - y + P) % P;
}
}
} if (opt == -) {
ll inv = fpow(lim, P - );
for (int i = ; i < lim; i++) c[i] = c[i] * inv % P;
}
} poly operator * (const poly x, const poly y) {
poly u = x, v = y, res;
prework(x.size() + y.size() - );
ntt(u, ), ntt(v, );
for (int i = ; i < lim; i++) res.push_back(u[i] * v[i] % P);
ntt(res, -);
res.resize(x.size() + y.size() - );
return res;
} inline void adj(poly &c) {
for (int i = ; i < m - ; i++) inc(c[i], c[i + m - ]);
c.resize(m - );
} inline poly pow(poly x, int y) {
poly res;
res.resize(m - );
res[] = ;
for (; y; y >>= ) {
if (y & ) res = res * x, adj(res);
x = x * x, adj(x);
}
return res;
} } using Poly :: P;
using Poly :: fpow;
using Poly :: pow; template <typename T>
inline void read(T &X) {
X = ; char ch = ; T op = ;
for (; ch > ''|| ch < ''; ch = getchar())
if (ch == '-') op = -;
for (; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} inline int getRoot() {
int cnt = ;
for (int i = ; i <= m - ; i++)
if ((m - ) % i == ) fac[++cnt] = i;
for (int i = ; ; i++) {
bool flag = ;
for (int j = ; j <= cnt; j++)
if (fpow(i, fac[j], m) == ) {
flag = ;
break;
}
if (flag) return i;
}
} int main() {
/* #ifndef ONLINE_JUDGE
freopen("8.in", "r", stdin);
#endif */ int s, root;
read(n), read(m), read(tar), read(s); root = getRoot();
for (int i = ; i <= m - ; i++) buc[fpow(root, i, m)] = i; poly trans;
trans.resize(m - );
for (int x, i = ; i <= s; i++) {
read(x);
if (!x) continue;
trans[buc[x]] = ;
}
poly ans = pow(trans, n); printf("%lld\n", ans[buc[tar]]);
return ;
}

Luogu 3321 [SDOI2015]序列统计的更多相关文章

  1. Luogu P3321 [SDOI2015]序列统计

    一道不错的多项式好题.还涉及了一些数论内容. 首先我们看到题目是求乘积模\(m\)的方案数,考虑到这种方案数我们一般都可以用生成函数来做. 但显然卷积的下标有加(FFT,NTT等)有位运算(FWT)但 ...

  2. 洛谷3321 SDOI2015 序列统计

    懒得放传送[大雾 有趣的一道题 前几天刚好听到Creed_神犇讲到相乘转原根变成卷积的形式 看到这道题当然就会做了啊w 对于m很小 我们暴力找原根 如果你不会找原根的话 出门左转百度qwq 找到原根以 ...

  3. BZOJ 3992: [SDOI2015]序列统计 [快速数论变换 生成函数 离散对数]

    3992: [SDOI2015]序列统计 Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 1017  Solved: 466[Submit][Statu ...

  4. [SDOI2015]序列统计

    [SDOI2015]序列统计 标签: NTT 快速幂 Description 给你一个模m意义下的数集,需要用这个数集生成一个数列,使得这个数列在的乘积为x. 问方案数模\(1004535809\). ...

  5. 3992: [SDOI2015]序列统计

    3992: [SDOI2015]序列统计 链接 分析: 给定一个集和s,求多少个长度为n的序列,满足序列中每个数都属于s,并且所有数的乘积模m等于x. 设$f=\sum\limits_{i=0}^{n ...

  6. [BZOJ 3992][SDOI2015]序列统计

    3992: [SDOI2015]序列统计 Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 2275  Solved: 1090[Submit][Stat ...

  7. 【LG3321】[SDOI2015]序列统计

    [LG3321][SDOI2015]序列统计 题面 洛谷 题解 前置芝士:原根 我们先看一下对于一个数\(p\),它的原根\(g\)有什么性质(好像就是定义): \(g^0\%p,g^1\%p,g^2 ...

  8. [BZOJ3992][SDOI2015]序列统计(DP+原根+NTT)

    3992: [SDOI2015]序列统计 Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 1888  Solved: 898[Submit][Statu ...

  9. 【BZOJ3992】[SDOI2015]序列统计 NTT+多项式快速幂

    [BZOJ3992][SDOI2015]序列统计 Description 小C有一个集合S,里面的元素都是小于M的非负整数.他用程序编写了一个数列生成器,可以生成一个长度为N的数列,数列中的每个数都属 ...

随机推荐

  1. 【idea】Error:java: Compilation failed: internal java compiler error 解决办法

    报错信息: 报错原因: 项目中Java版本不一致,可以查看项目中的jdk配置 1.查看项目的jdk(Ctrl+Alt+shift+S) File ->Project Structure-> ...

  2. 新手,Visual Studio 2013 配置Boost库,如何编译和选择

    QuantLib installation in VC++ 2010 and later 参考:http://quantlib.org/install/vc10.shtml 1,到官网下载最新的boo ...

  3. prisma 集成tidb 安装试用

    以前官方提供的ansible 的脚本,现在有了docker的版本,可以方便测试使用 实际完整配置参考 https://github.com/rongfengliang/prisma-tidb 安装ti ...

  4. 使用dev_add_pack注冊新的以太网类型

    接着上一篇文件:使用PF_PACKET和SOCK_RAW发送自己定义type以太网数据包 上一篇文章我们使用wireshare抓包.尽管在Linux下也能够使用抓包工具,可是我打算自己动手,在内核添加 ...

  5. pthread线程初始化(pthread_once)

    pthread_once 语法 int pthread_once(pthread_once_t *once_control, void (*init_routine)(void)); #include ...

  6. jmap打dump异常

    背景 用jmap打dump文件经常遇到如下异常,打不出来,哥今天告拆大家一个终极解决方法,嘘,不要告拆别人.. Attaching to core -F from executable 421442, ...

  7. 每日一条 git 命令行:git clone https://xxxxx.git -b 12.0 --depth 1

    每日一条 git 命令行:git clone https://xxxxx.git -b 12.0 --depth 1 -b 12.0:分支 12.0 --depth 1:depth 克隆深度,1 为最 ...

  8. js中setAttribute 的兼容性

    js中setAttribute 的兼容性class和className兼容方法: object.setAttribute("class","content") ...

  9. 【python】函数名存在变量中

    变量函数:意思就是将函数名存在变量中,然后根据变量值动态的调用需要的函数. LOGIN = 'xxxx' PASSWD = "xxx" URL = 'xxxxx' def hand ...

  10. emacs里面模拟vim按键操作的插件evil

    emacsConfig/evil-setting.el (setq evil-mode t) (setq evil-shift-width ) ;; some modes aren't meant f ...