题目

  点这里看题目。

分析

  设\(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. PG 更新统计信息

    http://blog.chinaunix.net/uid-24774106-id-3802225.html 一.vacuum的效果: 1.1释放,再利用 更新/删除的行所占据的磁盘空间. 第一点的原 ...

  2. C语言基础知识(三)——指针

    指针定义 1.指针的值表示的是它所指向对象的地址,指针+1表示的是下一元素的地址,按**字节**编址,而不是下一字节的地址. 2.依照数据类型而定,short占用两字节.int占用4字节.double ...

  3. OpenStack的Swift组件详解

    一:简介     一.背景 1. Swift 最初是由 Rackspace 公司开发的高可用分布式对象存储服务(Object  Storage Service),并于 2010 年贡献给 OpenSt ...

  4. 要小心 JavaScript 的事件代理

    我们知道,如果给 form 里面的 button 元素绑定事件,需要考虑它是否会触发 form 的 submit 行为.除此之外,其它场合给 button 元素绑定事件,你几乎不用担心这个事件会有什么 ...

  5. [jQuery插件]手写一个图片懒加载实现

    教你做图片懒加载插件 那一年 那一年,我还年轻 刚接手一个ASP.NET MVC 的 web 项目, (C#/jQuery/Bootstrap) 并没有做 web 的经验,没有预留学习时间, (作为项 ...

  6. 【JavaScript数据结构系列】04-优先队列PriorityQueue

    [JavaScript数据结构系列]04-优先队列PriorityQueue 码路工人 CoderMonkey 转载请注明作者与出处 ## 1. 认识优先级队列 经典的案例场景: 登机时经济舱的普通队 ...

  7. [优文翻译]002.陪伴我作为程序员的9句名言(9 Quotes that stayed with me as a developer)

    导读:本文是从<9 Quotes that stayed with me as a developer>这篇文章翻译而来 下面的锦句均来自于<9 Quotes that stayed ...

  8. CentOS安装Python3.5

    1.  安装python3.5可能使用的依赖 yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel s ...

  9. 看不见远程新建git分支

    再网页上新建了一个git分支.然后在本地跑git branch -r(查看远程分支)/ git branch -a(查看所有分支)两个命令,都没有看到新建的那个分支.这是为啥呢??? 原因是因为:gi ...

  10. Java实现 LeetCode 787 K 站中转内最便宜的航班(两种DP)

    787. K 站中转内最便宜的航班 有 n 个城市通过 m 个航班连接.每个航班都从城市 u 开始,以价格 w 抵达 v. 现在给定所有的城市和航班,以及出发城市 src 和目的地 dst,你的任务是 ...