\(\mathcal{Description}\)

  Link.

  给定 \(\{f_1,f_2,\cdots,f_n\}\),素数 \(p\)。求字典序最小的 \(\{a_1,a_2,\cdots,a_n\}\),满足对于所有 \(i\in[1,n]\),\(a_i\in\{0,1\}\) 并且

\[\sum_{\{k_{1..n}\}}[(\forall j)\left((a_j=0\land k_j=0)\lor(a_j\not=0\land k_j\ge0\right)]\left[ \sum_{j=1}^njk_j=i \right]\equiv f_i\pmod p
\]

  \(n<2^{18}\)。

\(\mathcal{Solution}\)

  对于某个 \(a_i=1\),其 OGF 为 \(\frac{1}{1-x^i}\),所有 OGF 之积的 \(1\sim n\) 次项系数在\(\bmod p\) 意义下就是 \(f_{1..n}\)。记 \(F(x)=\sum_{i=0}^{+\infty}f_ix^i\),默认运算在\(\bmod p\) 意义下进行,那么:

\[F(x)=\prod_{i=1}^n\left( \frac{1}{1-x^i} \right)^{a_i}
\]

  两边取 \(-\ln\):

\[\Rightarrow~~~~-\ln F(x)=\sum_{i=1}^na_i\ln(1-x^i)
\]

  Tayler 展开右式 \(\ln\):

\[\Rightarrow~~~~-\ln F(x)=\sum_{i=1}^na_i\sum_{j=1}^{+\infty}-\frac{x^{ij}}j
\]

  左右取负,枚举 \(T=ij\):

\[\Rightarrow~~~~\ln F(x)=\sum_{T=1}^{+\infty}x^T\sum_{i|T}a_i\frac{i}T
\]

  对已知的 \(F(x)\) 求 \(\ln\),就能取得 \(T\in[1,n]\) 内的所有 \(\frac{1}T\sum_{i|T}a_ii\),从 \(T=1\) 起刷表求解,每次枚举倍数消除贡献,可以 \(\mathcal O(n\ln n)\) 解出所有 \(a_{1..n}\)(没错,由于 \(\ln F(x)\) 模意义下的多项式系数唯一,\(\{a_n\}\) 唯一确定)。

  综上,复杂度为多项式 \(\ln\) 的 \(\mathcal O(n\log n)\)。建议用毛爷爷的四次 FFT 科技做 MTT。

\(\mathcal{Code}\)

/* Clearink */

#include <cmath>
#include <cstdio>
#include <cassert> #define rep( i, l, r ) for ( int i = l, rpbound##i = r; i <= rpbound##i; ++i )
#define per( i, r, l ) for ( int i = r, rpbound##i = l; i >= rpbound##i; --i ) typedef long long LL; inline int rint () {
int x = 0; char s = getchar ();
for ( ; s < '0' || '9' < s; s = getchar () );
for ( ; '0' <= s && s <= '9'; s = getchar () ) x = x * 10 + ( s ^ '0' );
return x;
} inline void wint ( const int x ) {
if ( 9 < x ) wint ( x / 10 );
putchar ( x % 10 ^ '0' );
} const int MAXLEN = 1 << 19;
const double PI = acos ( -1 );
int n, M, f[MAXLEN + 5], a[MAXLEN + 5]; inline int mul ( const long long a, const int b ) { return a * b % M; }
inline int sub ( int a, const int b ) { return ( a -= b ) < 0 ? a + M : a; }
inline int add ( int a, const int b ) { return ( a += b ) < M ? a : a - M; }
inline int mpow ( int a, int b ) {
int ret = 1;
for ( ; b; a = mul ( a, a ), b >>= 1 ) ret = mul ( ret, b & 1 ? a : 1 );
return ret;
} namespace PolyOper { int rev[MAXLEN + 5], inv[MAXLEN + 5]; inline void initInv () {
inv[1] = 1;
rep ( i, 2, MAXLEN ) inv[i] = mul ( M - M / i, inv[M % i] );
} struct Complex {
double x, y;
Complex (): x ( 0 ), y ( 0 ) {}
Complex ( const double tx, const double ty ): x ( tx ), y ( ty ) {}
inline Complex operator + ( const Complex t ) const {
return Complex ( x + t.x, y + t.y );
}
inline Complex operator - ( const Complex t ) const {
return Complex ( x - t.x, y - t.y );
}
inline Complex operator * ( const Complex t ) const {
return Complex ( x * t.x - y * t.y, x * t.y + y * t.x );
}
inline Complex operator / ( const double t ) const {
return Complex ( x / t, y / t );
}
};
Complex omega[MAXLEN + 5], P[MAXLEN + 5], Q[MAXLEN + 5];
Complex C[MAXLEN + 5], D[MAXLEN + 5], E[MAXLEN + 5], F[MAXLEN + 5]; inline void FFT ( const int n, Complex* A, const int tp ) {
rep ( i, 0, n - 1 ) if ( i < rev[i] ) std::swap ( A[i], A[rev[i]] );
for ( int i = 2, stp = 1; i <= n; i <<= 1, stp <<= 1 ) {
for ( int j = 0; j < n; j += i ) {
rep ( k, 0, stp - 1 ) {
Complex w ( omega[n / stp * k].x, tp * omega[n / stp * k].y );
Complex ev ( A[j + k] ), ov ( w * A[j + k + stp] );
A[j + k] = ev + ov, A[j + k + stp] = ev - ov;
}
}
}
if ( !~tp ) rep ( i, 0, n - 1 ) A[i] = A[i] / n;
} inline void initFFT ( const int lg ) {
int n = 1 << lg;
rep ( i, 0, n - 1 ) rev[i] = ( rev[i >> 1] >> 1 ) | ( ( i & 1 ) << lg >> 1 );
for ( int i = 1; i < n; i <<= 1 ) {
rep ( k, 0, i - 1 ) {
omega[n / i * k] = Complex ( cos ( PI * k / i ), sin ( PI * k / i ) );
}
}
} inline void polyConv ( const int n, const int m, const int* A, const int* B, int* R ) {
rep ( i, 0, n - 1 ) P[i] = Complex ( A[i] & 0x7fff, A[i] >> 15 );
rep ( i, 0, m - 1 ) Q[i] = Complex ( B[i] & 0x7fff, B[i] >> 15 );
int lg = 0, len = 1;
for ( ; len < n + m - 1; len <<= 1, ++ lg );
rep ( i, n, len ) P[i] = Complex ();
rep ( i, m, len ) Q[i] = Complex ();
initFFT ( lg );
FFT ( len, P, 1 ), FFT ( len, Q, 1 );
rep ( i, 0, len - 1 ) {
Complex t ( P[( len - i ) % len].x, -P[( len - i ) % len].y );
C[i] = ( P[i] + t ) / 2, D[i] = Complex ( 0, 1 ) * ( t - P[i] ) / 2;
}
rep ( i, 0, len - 1 ) {
Complex t ( Q[( len - i ) % len].x, -Q[( len - i ) % len].y );
E[i] = ( Q[i] + t ) / 2, F[i] = Complex ( 0, 1 ) * ( t - Q[i] ) / 2;
}
rep ( i, 0, len - 1 ) {
Complex c ( C[i] ), d ( D[i] ), e ( E[i] ), f ( F[i] );
C[i] = c * e, D[i] = c * f + d * e, E[i] = d * f;
P[i] = C[i] + Complex ( 0, 1 ) * E[i];
}
FFT ( len, D, -1 ), FFT ( len, P, -1 );
rep ( i, 0, n + m - 2 ) {
int c = ( LL ( P[i].x + 0.5 ) % M + M ) % M,
d = ( LL ( D[i].x + 0.5 ) % M + M ) % M,
e = ( LL ( P[i].y + 0.5 ) % M + M ) % M;
R[i] = ( c + ( 1ll << 15 ) % M * d % M + ( 1ll << 30 ) % M * e % M ) % M;
}
} inline void polyDer ( const int n, const int* A, int* R ) {
rep ( i, 1, n - 1 ) R[i - 1] = mul ( i, A[i] );
R[n - 1] = 0;
} inline void polyInt ( const int n, const int* A, int* R ) {
per ( i, n - 1, 0 ) R[i + 1] = mul ( inv[i + 1], A[i] );
R[0] = 0;
} inline void polyInv ( const int n, const int* A, int* R ) {
if ( n == 1 ) return void ( R[0] = mpow ( A[0], M - 2 ) );
static int tmp[MAXLEN + 5];
polyInv ( n + 1 >> 1, A, R ); polyConv ( n, n, A, R, tmp );
tmp[0] = sub ( 2, tmp[0] );
rep ( i, 1, n - 1 ) tmp[i] = sub ( 0, tmp[i] );
polyConv ( n, n, tmp, R, R );
} inline void polyLn ( const int n, const int* A, int* R ) {
static int tmp[2][MAXLEN + 5];
polyDer ( n, A, tmp[0] ), polyInv ( n, A, tmp[1] );
polyConv ( n, n, tmp[0], tmp[1], tmp[0] );
polyInt ( n, tmp[0], R );
} } // namesapce PolyOper. int main () {
n = rint (), M = rint ();
PolyOper::initInv ();
f[0] = 1;
rep ( i, 1, n ) f[i] = rint ();
PolyOper::polyLn ( n + 1, f, f );
rep ( i, 1, n ) f[i] = mul ( f[i], i );
int cnt = 0;
rep ( i, 1, n ) {
assert ( !f[i] || f[i] == i );
cnt += a[i] = !!f[i];
if ( a[i] ) rep ( j, 2, n / i ) f[i * j] = sub ( f[i * j], i );
}
wint ( cnt );
for ( int i = 1, flg = 0; i <= n; ++i ) {
if ( a[i] ) {
putchar ( flg ? ' ' : '\n' ), flg = 1;
wint ( i );
}
}
putchar ( '\n' );
return 0;
}

Solution -「SDOI 2017」「洛谷 P3784」遗忘的集合的更多相关文章

  1. 洛谷P3784 [SDOI2017]遗忘的集合(生成函数)

    题面 传送门 题解 生成函数这厮到底还有什么是办不到的-- 首先对于一个数\(i\),如果存在的话可以取无限多次,那么它的生成函数为\[\sum_{j=0}^{\infty}x^{ij}={1\ove ...

  2. 「区间DP」「洛谷P1043」数字游戏

    「洛谷P1043」数字游戏 日后再写 代码 /*#!/bin/sh dir=$GEDIT_CURRENT_DOCUMENT_DIR name=$GEDIT_CURRENT_DOCUMENT_NAME ...

  3. [CodePlus 2017 11月赛&洛谷P4058]木材 题解(二分答案)

    [CodePlus 2017 11月赛&洛谷P4058]木材 Description 有 n棵树,初始时每棵树的高度为 Hi ,第 i棵树每月都会长高 Ai.现在有个木料长度总量为 S的订单, ...

  4. Solution -「JSOI 2019」「洛谷 P5334」节日庆典

    \(\mathscr{Description}\)   Link.   给定字符串 \(S\),求 \(S\) 的每个前缀的最小表示法起始下标(若有多个,取最小的).   \(|S|\le3\time ...

  5. Solution -「洛谷 P4372」Out of Sorts P

    \(\mathcal{Description}\)   OurOJ & 洛谷 P4372(几乎一致)   设计一个排序算法,设现在对 \(\{a_n\}\) 中 \([l,r]\) 内的元素排 ...

  6. Solution -「POI 2010」「洛谷 P3511」MOS-Bridges

    \(\mathcal{Description}\)   Link.(洛谷上这翻译真的一言难尽呐.   给定一个 \(n\) 个点 \(m\) 条边的无向图,一条边 \((u,v,a,b)\) 表示从 ...

  7. Solution -「APIO 2016」「洛谷 P3643」划艇

    \(\mathcal{Description}\)   Link & 双倍经验.   给定 \(n\) 个区间 \([a_i,b_i)\)(注意原题是闭区间,这里只为方便后文描述),求 \(\ ...

  8. 「洛谷4197」「BZOJ3545」peak【线段树合并】

    题目链接 [洛谷] [BZOJ]没有权限号嘤嘤嘤.题号:3545 题解 窝不会克鲁斯卡尔重构树怎么办??? 可以离线乱搞. 我们将所有的操作全都存下来. 为了解决小于等于\(x\)的操作,那么我们按照 ...

  9. 「洛谷3338」「ZJOI2014」力【FFT】

    题目链接 [BZOJ] [洛谷] 题解 首先我们需要对这个式子进行化简,否则对着这么大一坨东西只能暴力... \[F_i=\sum_{j<i} \frac{q_iq_j}{(i-j)^2}-\s ...

随机推荐

  1. 安装Cacti-plugin

    安装pluginunzip cacti-plugin-0.8.7e-PA-v2.6.zip -d cacti-plugin-archcp -R cacti-plugin-arch/* /data/ww ...

  2. 微信小程序配置域名的时候提示“校验文件验证失败”

    在微信小程序后台配置web-view的业务域名跟扫普通链接二维码打开小程序两项功能时, 一直提示"校验文件验证失败,请下载校验文件,上传到服务器指定的目录" 实际访问校验文件的路径 ...

  3. iview在ie9及以上的兼容问题解决方案

    兼容requestAnimationFrame let lastTime = 0 let vendors = ['ms', 'moz', 'webkit', 'o'] for (let x = 0; ...

  4. less与sass的区别

    在介绍less和sass的区别之前,我们先来了解一下他们的定义: 一.Less.Sass/Scss是什么? 1.Less: 是一种动态样式语言. 对CSS赋予了动态语言的特性,如变量.继承.运算.函数 ...

  5. console.log(a)和console.log(window.a)的区别?

    console.log(window.l); //undefined console.log(l); //Uncaught ReferenceError: l is not defined js对于未 ...

  6. Mysql存储过程二

    1.MySQL中创建存储过程时通过DEFINER和SQL SECURITY设置访问权限 procedure与function.trigger等创建时紧接着CREATE都有个definer可选项,该de ...

  7. 《剑指offer》面试题41. 数据流中的中位数

    问题描述 如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值.如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中间两个数的平均值. 例 ...

  8. Solon Web 开发,十一、国际化

    Solon Web 开发 一.开始 二.开发知识准备 三.打包与运行 四.请求上下文 五.数据访问.事务与缓存应用 六.过滤器.处理.拦截器 七.视图模板与Mvc注解 八.校验.及定制与扩展 九.跨域 ...

  9. 今日学习——蓝桥杯 2019年 C语言 B组

    1.手淦(亲身体验,,,没啥大用,最终还是代码) 2.代码(下面是我看其他博主代码答案能看的懂的....具体的可以直接去下面的网址看) https://blog.csdn.net/qq_4452491 ...

  10. 一些Markdown扩展语法

    相信很多人跟我一样,对Markdown是"一知半解",会打一点,知道一点,但是其实从没花哪怕一分钟了解过.其实除了标题粗体插入代码,Markdown还有很多有趣的基础语法和扩展语法 ...