CF453D Little Pony and Elements of Harmony

本文探讨了一个特定的算法问题,涉及到数组通过特定操作进行更新的复杂过程。利用FFT(快速傅立叶变换)和快速幂运算,文章详细解析了如何高效地计算经过大量迭代后数组的状态。关键在于将问题转化为FFT形式,通过预先处理数组并应用快速幂,最终逆变换得到结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

机房的同学都把这题秒啦
我还是太菜啦
题目链接

题目大意

有一个数组\(f\)和一个数组\(b\),每次操作,\(f[i]\)会变成\(\sum_{j=0}^nb[popcount(i\oplus j)]*f[j]\)
\(t\)次操作之后的\(f\),对\(P\)取模.
\(n\leq 2^{20},t\leq 10^{18},b\leq 20\)


解析

\(c[i]=b[popcount(i)]\)
\(f[i]=\sum_{j=0}^nc[i\oplus j]*f[j]\)
考虑到\(i\oplus j\oplus j=i\),因此\(f[i]=\sum_{j\oplus k=i}c[j]*f[k]\).
我们很愉快的发现这就是一个\(FWT\)的形式.
因此只要把\(c\)先做一次\(FWT\),然后对每个数快速幂一下(\(t\)次方),然后再把\(f\)做一次\(FWT\),最后乘起来然后\(IFWT\)即可.

考虑到这个\(P\)可能没有模\(n\)的乘法逆元,因此把\(P\)乘上\(n\),然后\(IFWT\)的时候直接除以\(n\)即可.乘的时候注意\(O(1)\)快速乘.

代码如下

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#define N (1100010)
#define inf (0x7f7f7f7f)
#define rg register int
#define Label puts("NAIVE")
#define spa print(' ')
#define ent print('\n')
#define rand() (((rand())<<(15))^(rand()))
typedef long double ld;
typedef long long LL;
typedef unsigned long long ull;
using namespace std;
inline char read(){
    static const int IN_LEN=1000000;
    static char buf[IN_LEN],*s,*t;
    return (s==t?t=(s=buf)+fread(buf,1,IN_LEN,stdin),(s==t?-1:*s++):*s++);
}
template<class T>
inline void read(T &x){
    static bool iosig;
    static char c;
    for(iosig=false,c=read();!isdigit(c);c=read()){
        if(c=='-')iosig=true;
        if(c==-1)return;
    }
    for(x=0;isdigit(c);c=read())x=((x+(x<<2))<<1)+(c^'0');
    if(iosig)x=-x;
}
inline char readchar(){
    static char c;
    for(c=read();!isalpha(c);c=read())
    if(c==-1)return 0;
    return c;
}
const int OUT_LEN = 10000000;
char obuf[OUT_LEN],*ooh=obuf;
inline void print(char c) {
    if(ooh==obuf+OUT_LEN)fwrite(obuf,1,OUT_LEN,stdout),ooh=obuf;
    *ooh++=c;
}
template<class T>
inline void print(T x){
    static int buf[30],cnt;
    if(x==0)print('0');
    else{
        if(x<0)print('-'),x=-x;
        for(cnt=0;x;x/=10)buf[++cnt]=x%10+48;
        while(cnt)print((char)buf[cnt--]);
    }
}
inline void flush(){fwrite(obuf,1,ooh-obuf,stdout);}
LL e[N],b[N],c[N],P,t;
int n,m;
int popcount(int x){
    int ans=0;
    while(x)x&=(x-1),ans++;
    return ans; 
}
LL mult(LL x,LL y,LL mod){
    LL tmp=(x*y-(LL)((ld)x/mod*y+1.0e-8)*mod);
    return tmp<0?tmp+mod:tmp;
}
LL FWT(LL *a,int tp){
    for(int i=1;i<n;i<<=1)
    for(int R=i<<1,j=0;j<n;j+=R)
    for(int k=j;k<j+i;k++){
        LL x=a[k],y=a[k+i];
        a[k]=(x+y)%P,a[k+i]=(x-y+P)%P;
    }
    if(tp==-1)
    for(int i=0;i<n;i++)a[i]/=n;
}
LL ksm(LL a,LL p){
    LL res=1;
    while(p){
        if(p&1)res=mult(res,a,P);
        a=mult(a,a,P),p>>=1;
    }
    return res;
}
int main(){
    read(m),n=(1<<m),read(t),read(P),P*=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[popcount(i)];
    FWT(e,1),FWT(c,1);
    for(int i=0;i<n;i++)c[i]=ksm(c[i],t);
    for(int i=0;i<n;i++)e[i]=mult(e[i],c[i],P);
    FWT(e,-1);
    for(int i=0;i<n;i++)
    print(e[i]),ent;
    return flush(),0;
}

转载于:https://www.cnblogs.com/Romeolong/p/10064188.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值