题目

  点这里看题目。

分析

  设\(count(x)\)为\(x\)的二进制中\(1\)的个数。因此\(f(u,v)=count(u\oplus v)\)

  看一看每次转移,我们发现最不友好的东西就是\(f(u,v)\),因此我们得想办法把它从我们的计算中丢掉。

  发现对于\([0,n)\)中的所有数,两两异或之后不会超过\(n\)。并且对于一个固定的数\(x\),其\(count(x)\)是不会变的。因此我们考虑将\(b\)数组转存出来:

\[a[i]=b[count(i)]
\]

  因此有:

\[e[u]=\sum_v a[u\oplus v]e[v]
\]

  考虑改变枚举顺序:

\[\begin{aligned}
e[u] &=\sum_v a[u\oplus v]e[v]\\
&=\sum_{i=0}^ma[i]\sum_{u\oplus v=i}e[v]\\
&=\sum_{i=0}^m a[i]\sum_{v\oplus i=u}e[v]\\
&=\sum_{v\oplus i=u}a[i]e[v]\end{aligned}
\]

  因此每次转移都是一个异或卷积的形式,可以用 FWT 优化一发。由于需要转移\(t\)次,还可用快速幂。 FWT 只需要在初始和最后做,中途快速幂不需要。时间是\(O(mn + n\log_2t)\)。

  这里还有一问题。由于本题给的是任意模数,可能不存在\(2\)逆元。

  众所周知, 异或 FWT 还有一种版本,也就是像 FFT 一样,正变换和逆变换大部分一样,但是逆变换会在最后除掉向量长度(事实上 FWT 和 FFT 有很多相似处,可以在 K 进制 FWT 中了解到)

  因此我们可以使用上述的 FWT 。但是这里还有问题,\(p\)可能也没有\(n\)的逆元。根据同余基本性质:

\[a\equiv b\pmod p\Leftrightarrow \frac a d\equiv \frac b d\pmod {\frac p d}(d|\gcd\{a,b,m\})
\]

  我们把\(p\)扩大\(n\)倍之后就可以直接除得正确答案了。

  最后一个问题,\(n\times p\)是\(10^{15}\)的,如果直接乘法会溢出(什么? __int128 ?)。因此我们需要用 long double 来模拟取模(龟速乘太慢了)。

代码

#include <cstdio>

typedef long long LL;
typedef long double LB; const int MAXM = 25, MAXN = 1.5e6 + 5; template<typename _T>
void read( _T &x )
{
x = 0;char s = getchar();int f = 1;
while( s > '9' || s < '0' ){if( s == '-' ) f = -1; s = getchar();}
while( s >= '0' && s <= '9' ){x = ( x << 3 ) + ( x << 1 ) + ( s - '0' ), s = getchar();}
x *= f;
} template<typename _T>
void write( _T x )
{
if( x < 0 ){ putchar( '-' ); x = ( ~ x ) + 1; }
if( 9 < x ){ write( x / 10 ); }
putchar( x % 10 + '0' );
} LL E[MAXN], C[MAXN];
int B[MAXM];
LL T, mod;
int N, M; int lowbit( const int &x ) { return x & ( -x ); }
LL fix( const LL a ) { return ( a % mod + mod ) % mod; }
int count( int x ) { int ret = 0; while( x ) ret ++, x -= lowbit( x ); return ret; } LL mul( const LL a, const LL b ) { return fix( a * b - ( LL ) ( ( LB ) a / mod * b ) * mod ); } void FWT( LL *f, const int mode )
{
LL t1, t2;
for( int s = 2 ; s <= N ; s <<= 1 )
for( int i = 0, t = s >> 1 ; i < N ; i += s )
for( int j = i ; j < i + t ; j ++ )
{
t1 = f[j], t2 = f[j + t];
f[j] = ( t1 + t2 ) % mod, f[j + t] = fix( t1 - t2 );
}
if( ~ mode ) return ;
for( int i = 0 ; i < N ; i ++ ) f[i] /= N;
} void mul( LL *ret, LL *mult )
{
for( int i = 0 ; i < N ; i ++ )
ret[i] = mul( ret[i], mult[i] );
} int main()
{
read( M ), read( T ), read( mod );
N = 1 << M, mod *= N;
for( int i = 0 ; i < N ; i ++ ) read( E[i] );
for( int i = 0 ; i <= M ; i ++ ) read( B[i] );
for( int i = 0 ; i < N ; i ++ ) C[i] = B[count( i )];
FWT( E, 1 ), FWT( C, 1 );
while( T )
{
if( T & 1 ) mul( E, C );
mul( C, C ), T >>= 1;
}
FWT( E, -1 );
for( int i = 0 ; i < N ; i ++ ) write( E[i] ), puts( "" );
return 0;
}

[CF453D]Little Pony and Elements of Harmony的更多相关文章

  1. 【CF453D】 Little Pony and Elements of Harmony(FWT)

    题面 传送门 设\(a\)的递推公式为 \[a_i=\sum_ja_jb[count(i\oplus j)]\] 其中\(\oplus\)为异或,\(count(i)\)表示\(i\)的二进制中\(1 ...

  2. 453D Little Pony and Elements of Harmony

    传送门 分析 我们可以将所有的b[i^j]直接对应到b[f(i^j)]上 于是显然可以fwt 我们对b进行t次fwt之后直接将答案与e0卷起来即可 注意由于模数不确定,我们可以将模数扩大$2^m$然后 ...

  3. CF453(Div1 简单题解)

    A .Little Pony and Expected Maximum pro:给定M,N,表示一个M面的骰子,甩N次,问出现的最大的数的期望. sol:容斥,f(i)表示最大数<=i的期望,那 ...

  4. CF453B Little Pony and Harmony Chest (状压DP)

    CF453B CF454D Codeforces Round #259 (Div. 2) D Codeforces Round #259 (Div. 1) B D. Little Pony and H ...

  5. Codeforces Round #259 (Div. 2) D. Little Pony and Harmony Chest 状压DP

    D. Little Pony and Harmony Chest   Princess Twilight went to Celestia and Luna's old castle to resea ...

  6. Codeforces Round #259 (Div. 2) D

    D. Little Pony and Harmony Chest time limit per test 4 seconds memory limit per test 256 megabytes i ...

  7. codeforces Round #259(div2) D解决报告

    D. Little Pony and Harmony Chest time limit per test 4 seconds memory limit per test 256 megabytes i ...

  8. Codeforces 4538 (状态压缩dp)Little Pony and Harmony Chest

    Little Pony and Harmony Chest 经典状态压缩dp #include <cstdio> #include <cstring> #include < ...

  9. [CF453B]Little Pony and Harmony Chest

    [CF453B]Little Pony and Harmony Chest 题目大意: 给你一个长度为\(n(n\le100)\)的正整数序列\(A(A_i\le30)\),求一个正整数序列\(B\) ...

随机推荐

  1. nodejs+express搭建小程序后台服务器

    本文使用node.js和express来为小程序搭建服务器.node.js简单说是运行在服务端的javascript:而express是node.js的一个Web应用框架,使用express可以非常简 ...

  2. ShoneSharp语言(S#)的设计和使用介绍系列(9)— 一等公民“函数“爱炫巧

    ShoneSharp语言(S#)的设计和使用介绍 系列(9)— 一等公民“函数“爱炫巧 作者:Shone 声明:原创文章欢迎转载,但请注明出处,https://www.cnblogs.com/Shon ...

  3. Python中ThreadLocal的理解与使用

    一.对 ThreadLocal 的理解 ThreadLocal,有的人叫它线程本地变量,也有的人叫它线程本地存储,其实意思一样. ThreadLocal 在每一个变量中都会创建一个副本,每个线程都可以 ...

  4. Mysql数值类型,小数点后保留两个零

    如有不足请帮忙留言区补充谢谢~ 一,数值类型保留小数点后两个0 在存入数据时,应客户需求数值类型,比如钱数,分数等等需要精确到小数点后几位. 800存入时显示为800.00 方法:在建表时直接定义此数 ...

  5. [256个管理学理论]006.刺猬效应(Hedgehog Effect)

    刺猬效应(Hedgehog Effect) 来自于大洋彼岸的让你看不懂的解释: 刺猬效应(刺猬法则)就是人际交往中的“心理距离效应”.人与人之间都应该保持这条底线,过犹不及. 刺猬效应强调的就是人际交 ...

  6. MRCTF 2020 WP

    MRCTF 2020 WP 引言 周末趁上课之余,做了一下北邮的CTF,这里记录一下做出来的几题的WP ez_bypass 知识点:MD5强类型比较,is_numeric()函数绕过 题目源码: I ...

  7. Condition线程通信_生产者消费者案例

    ①Condition 接口描述了可能会与锁有关联的条件变量. 这些变量在用 法上与使用 Object.wait 访问的隐式监视器类似,但提供了更强大的 功能. 需要特别指出的是,单个 Lock 可能与 ...

  8. 用Python做词云可视化带你分析海贼王、火影和死神三大经典动漫

    对于动漫爱好者来说,海贼王.火影.死神三大动漫神作你肯定肯定不陌生了.小编身边很多的同事仍然深爱着这些经典神作,可见"中毒"至深.今天小编利用Python大法带大家分析一下这些神作 ...

  9. parrot os 创建swap分区&swapon failed invalid argument解决

    parrot os(不仅仅是debian系统),分区提示,查看系统格式为btrfs,需要注意的是btrfs无法添加swap分区,但是可以在5.0内核以上添加 以下命令,完成创建8g的swap分区 to ...

  10. HTML转义字符&url编码表

    ISO Latin-1字符集:  — 制表符Horizontal tab  — 换行Line feed  — 回车Carriage Return  — Space ! ! — 惊叹号Exclamati ...