跳到正文

题解

题解:P12072 [THOI 2014] 超立方体

_determination_约 23 分钟
原文
文章信息与版本

文章信息

发布
2026年6月6日
收录
2026年11月4日
更新
2026年11月9日
篇幅
约 23 分钟
获赞
4
题目
P12072

版本

  1. v1当前

你们咋都写这么魔怔的玩意,邪恶 @Miss_SGT 把这题加强到了 t=2105t=2^{10^5},场上整出来了一个及其魔怔的过不去加强版(但是过原题绰绰有余)的做法。

下面认为 nn 是 20(也就是题面中的 nn 的 log⁡\log),T=log⁡tT=\log t。

首先一个很自然的想法就是看 i→ji\to j 的方案数,然后你就注意到若 popcount⁡(ixor⁡j)\operatorname{popcount}(i\operatorname{xor}j) 相等则这个方案数也一样。大概幻想一下就是你改变某些位的方法肯定和改变同样多位的方法能够一一对应。

那么很自然的问题被拆分为了两个部分,第一是计算 fxf_x 表示变化 xx 位的方案数。第二是计算 gx,yg_{x,y} 表示变化 xx 位变成 yy 的起点的 ∑ap\sum a_p(也就是 ∑popcount⁡(pxor⁡y)=xap\sum_{\operatorname{popcount}(p\operatorname{xor}y)=x}a_p)。

我们来看第一部分。根据 ff 的定义,可以直接写出方程 fx→fx+1f_x\to f_{x+1},转移系数 x+1x+1。想象一下对于一个固定的有 x+1x+1 位是 1 的数,有 x+1x+1 个可以转移到他的有 xx 位是 1 的数。同理有 fx→fx−1f_x\to f_{x-1},转移系数 n−x+1n-x+1。

发现这个转移非常好看,写一个矩乘就好了。O(n3T)O(n^3T)。

然后看第二部分。一个想法就是你钦定变换顺序必须先变第零位,再变第一位……如此。直接转移就能做到 O(2nn2)O(2^nn^2)。

最后 xx 的答案就是 ∑figi,x\sum f_ig_{i,x},总复杂度 O(2nn2+n3T)O(2^nn^2+n^3T)。

Link.

代码中的卡常是试图让这个玩意跑过加强版,但是显然失败了。这题不需要快读和快速取模应该也能过。

C++
#include<bits/stdc++.h>
#define int unsigned long long
#define endl '\n'
using namespace std;

namespace fastio
{
    const int bufl=1<<20;
    const double base1[16]={1,1e-1,1e-2,1e-3,1e-4,1e-5,1e-6,1e-7,1e-8,1e-9,1e-10,1e-11,1e-12,1e-13,1e-14,1e-15};
    const double base2[16]={1,1e1,1e2,1e3,1e4,1e5,1e6,1e7,1e8,1e9,1e10,1e11,1e12,1e13,1e14,1e15};
    struct IN{
        FILE *IT;char ibuf[bufl],*is=ibuf,*it=ibuf;
        IN(){IT=stdin;}IN(char *a){IT=fopen(a,"r");}
        inline char getChar(){if(is==it){it=(is=ibuf)+fread(ibuf,1,bufl,IT);if(is==it)return EOF;}return *is++;}
        template<typename Temp>inline void getInt(Temp &a){a=0;int b=0,c=getChar();while(c<48||c>57)b^=(c==45),c=getChar();while(c>=48&&c<=57)a=(a<<1)+(a<<3)+c-48,c=getChar();if(b)a=-a;}
        template<typename Temp>inline void getDouble(Temp &a){a=0;int b=0,c=getChar(),d=0;__int128 e=0,f=0;while(c<48||c>57)b^=(c==45),c=getChar();while(c>=48&&c<=57)e=(e<<1)+(e<<3)+c-48,c=getChar();if(c==46){c=getChar();while(c>=48&&c<=57)d++,f=(f<<1)+(f<<3)+c-48,c=getChar();}a=e+base1[d]*f;if(b)a=-a;}
        IN& operator>>(char &a){a=getChar();return *this;}
        IN& operator>>(char *a){do{*a=getChar();}while(*a<=32);while(*a>32)*++a=getChar();*a=0;return *this;}
        IN& operator>>(string &a){char b=getChar();while(b<=32)b=getChar();while(b>32)a+=b,b=getChar();return *this;}
        IN& operator>>(int &a){getInt(a);return *this;}
//        IN& operator>>(long long &a){getInt(a);return *this;}
        IN& operator>>(__int128 &a){getInt(a);return *this;}
        IN& operator>>(float &a){getDouble(a);return *this;}
        IN& operator>>(double &a){getDouble(a);return *this;}
        IN& operator>>(long double &a){getDouble(a);return *this;}
    };
    struct OUT{
        FILE *IT;char obuf[bufl],*os=obuf,*ot=obuf+bufl;int Eps;long double Acc;
        OUT(){IT=stdout,Eps=6,Acc=1e-6;}OUT(char *a){IT=fopen(a,"w"),Eps=6,Acc=1e-6;}
        inline void ChangEps(int x=6){Eps=x;}
        inline void flush(){fwrite(obuf,1,os-obuf,IT);os=obuf;}
        inline void putChar(int a){*os++=a;if(os==ot)flush();}
        template<typename Temp>inline void putInt(Temp a){if(a<0){putChar(45);a=-a;}if(a<10){putChar(a+48);return;}putInt(a/10);putChar(a%10+48);}
        template<typename Temp>inline void putLeading(Temp a,int b){if(!b)return;putLeading(a/10,b-1);putChar(a%10+48);}
        template<typename Temp>inline void putDouble(Temp a){if(a<0){putChar(45);a=-a;}__int128 b=a;putInt(b);a-=b;a*=base2[Eps];b=a+Acc;putChar(46);putLeading(b,Eps);}
        OUT& operator<<(char a){putChar(a);return *this;}
        OUT& operator<<(char *a){while(*a>32)putChar(*a++);return *this;}
        OUT& operator<<(string a){for(auto c:a)putChar(c);return *this;}
        OUT& operator<<(int a){putInt(a);return *this;}
//        OUT& operator<<(long long a){putInt(a);return *this;}
        OUT& operator<<(__int128 a){putInt(a);return *this;}
        OUT& operator<<(float a){putDouble(a);return *this;}
        OUT& operator<<(double a){putDouble(a);return *this;}
        OUT& operator<<(long double a){putDouble(a);return *this;}
        ~OUT(){flush();}
    };
}
using fastio::IN;
using fastio::OUT;
IN fin;
OUT fout;
#define cin fin
#define cout fout

const int inf=0x3f3f3f3f3f3f3f3f;
const int N=30,M=(1ll<<20)+10;
//const int mod=1e9+7;

int mod;
struct MOD {
    int m, p;
    void init(long long pp) { 
        m = ((__int128)1 << 64) / pp; 
        p = pp; 
    }
    int operator()(int x) {
        int res = x - (int)(((__int128)x * m) >> 64) * p;
        
        if (res < 0) res += p;
        if (res >= p) res -= p;
        
        return res;
    }
} Mod;

mt19937 rnd(time(0));
int n,T;
int a[M],f[N];
int g[N][M];

//void add(int &x,int y){if((x+=y)>=mod)x-=mod;}

namespace MATRIX{
	struct Matrix{
		int a[21][21];
		void clear(){memset(a,0,sizeof(a));}
	}x;
	struct Vector{
		int a[21];
		void clear(){memset(a,0,sizeof(a));}
	}ans;
	Matrix operator *(Matrix x,Matrix y)
	{
		Matrix ans;ans.clear();
		for ( int i = 0 ; i <= n ; i++ )
			for ( int k = 0 ; k <= n ; k++ )
			{
				if(!x.a[i][k])continue;
				for ( int j = 0 ; j <= n ; j++ )
					ans.a[i][j]+=Mod(x.a[i][k]*y.a[k][j]);
			}
		for ( int i = 0 ; i <= n ; i++ )
			for ( int j = 0 ; j <= n ; j++ )
				ans.a[i][j]=Mod(ans.a[i][j]);
//				ans.a[i][j]%=mod;
		return ans;
	}
	Vector operator *(Vector x,Matrix y)
	{
		Vector ans;ans.clear();
		for ( int k = 0 ; k <= n ; k++ )
//		{
			for ( int j = 0 ; j <= n ; j++ )
				ans.a[j]+=Mod(x.a[k]*y.a[k][j]);
//				ans.a[j]+=x.a[k]*y.a[k][j]%mod;
//		}
		for ( int j = 0 ; j <= n ; j++ )ans.a[j]=Mod(ans.a[j]);//ans.a[j]%=mod;
		return ans;
	}
	void solve()
	{
		ans.a[0]=1;
		for ( int i = 0 ; i <= n ; i++ )
		{
			if(i)x.a[i][i-1]=n-i+1;
			if(i!=n)x.a[i][i+1]=i+1;
		}
		while(T)
		{
			if(T&1)ans=ans*x;
			x=x*x;T>>=1;
		}
		for ( int i = 0 ; i <= n ; i++ )f[i]=ans.a[i];
	}
}


signed main()
{
	ios::sync_with_stdio(false);
//	cin.tie(0),cout.tie(0);
//	freopen("peter.in","r",stdin);
//	freopen("peter.out","w",stdout);
	cin >> n >> T >> mod;n=log2(n);
	Mod.init(mod);
	
	for ( int i = 0 ; i < (1ll<<n) ; i++ )cin >> a[i];
	int cl=clock();
	MATRIX::solve();
	cerr << clock()-cl << endl;cl=clock();
	for ( int i = 0 ; i < (1ll<<n) ; i++ )g[0][i]=a[i];
	for ( int i = 0 ; i < n ; i++ )
	{
		for ( signed j = i ; j >= 0 ; j-- )
			for ( int k = 0 ; k < (1ll<<n) ; k++ )
				g[j+1][k^(1ll<<i)]+=g[j][k];
		for ( int j = 0 ; j <= i+1 ; j++ )
			for ( int k = 0 ; k < (1ll<<n) ; k++ )
				g[j][k]=Mod(g[j][k]);
	}
	cerr << clock()-cl << endl;
	for ( int i = 0 ; i < (1ll<<n) ; i++ )
	{
		int ans=0;
		for ( int j = 0 ; j <= n ; j++ )
			ans+=Mod(g[j][i]*f[j]);
		cout << Mod(ans);cout << '\n';
	}
	return 0;
}

上面是极其魔怔的考场做法,看个乐子就好了。给大家一个复杂度更优秀的做法。

考虑你每过一个时刻本质是这样的:

fx′=fygxxor⁡yf'_{x}=f_yg_{x\operatorname{xor}y},其中 gx=[popcount⁡(x)=1]g_x=[\operatorname{popcount}(x)=1]。发现这个本质是 H=FGtH=FG^t,你直接把 f,gf,g fwt 一遍,然后对着 GG 的每个元素做快速幂就能做到 O(2nT)O(2^nT)。由于我们刚刚的观察发现在 xx 的 popcount 一样的时候 GxG_x 相同然后就能只做 O(n)O(n) 次快速幂。最终复杂度 O(nT+n2n)O(nT+n2^n)。

AI 和我说这个做法本质把前面做法的转移矩阵对角化了,我也不知道他在说什么。

Link.

C++
#include<bits/stdc++.h>
#define int __int128
#define endl '\n'
using namespace std;

namespace fastio
{
    const int bufl=1<<20;
    const double base1[16]={1,1e-1,1e-2,1e-3,1e-4,1e-5,1e-6,1e-7,1e-8,1e-9,1e-10,1e-11,1e-12,1e-13,1e-14,1e-15};
    const double base2[16]={1,1e1,1e2,1e3,1e4,1e5,1e6,1e7,1e8,1e9,1e10,1e11,1e12,1e13,1e14,1e15};
    struct IN{
        FILE *IT;char ibuf[bufl],*is=ibuf,*it=ibuf;
        IN(){IT=stdin;}IN(char *a){IT=fopen(a,"r");}
        inline char getChar(){if(is==it){it=(is=ibuf)+fread(ibuf,1,bufl,IT);if(is==it)return EOF;}return *is++;}
        template<typename Temp>inline void getInt(Temp &a){a=0;int b=0,c=getChar();while(c<48||c>57)b^=(c==45),c=getChar();while(c>=48&&c<=57)a=(a<<1)+(a<<3)+c-48,c=getChar();if(b)a=-a;}
        template<typename Temp>inline void getDouble(Temp &a){a=0;int b=0,c=getChar(),d=0;__int128 e=0,f=0;while(c<48||c>57)b^=(c==45),c=getChar();while(c>=48&&c<=57)e=(e<<1)+(e<<3)+c-48,c=getChar();if(c==46){c=getChar();while(c>=48&&c<=57)d++,f=(f<<1)+(f<<3)+c-48,c=getChar();}a=e+base1[d]*f;if(b)a=-a;}
        IN& operator>>(char &a){a=getChar();return *this;}
        IN& operator>>(char *a){do{*a=getChar();}while(*a<=32);while(*a>32)*++a=getChar();*a=0;return *this;}
        IN& operator>>(string &a){char b=getChar();while(b<=32)b=getChar();while(b>32)a+=b,b=getChar();return *this;}
        IN& operator>>(int &a){getInt(a);return *this;}
//        IN& operator>>(long long &a){getInt(a);return *this;}
//        IN& operator>>(__int128 &a){getInt(a);return *this;}
        IN& operator>>(float &a){getDouble(a);return *this;}
        IN& operator>>(double &a){getDouble(a);return *this;}
        IN& operator>>(long double &a){getDouble(a);return *this;}
    };
    struct OUT{
        FILE *IT;char obuf[bufl],*os=obuf,*ot=obuf+bufl;int Eps;long double Acc;
        OUT(){IT=stdout,Eps=6,Acc=1e-6;}OUT(char *a){IT=fopen(a,"w"),Eps=6,Acc=1e-6;}
        inline void ChangEps(int x=6){Eps=x;}
        inline void flush(){fwrite(obuf,1,os-obuf,IT);os=obuf;}
        inline void putChar(int a){*os++=a;if(os==ot)flush();}
        template<typename Temp>inline void putInt(Temp a){if(a<0){putChar(45);a=-a;}if(a<10){putChar(a+48);return;}putInt(a/10);putChar(a%10+48);}
        template<typename Temp>inline void putLeading(Temp a,int b){if(!b)return;putLeading(a/10,b-1);putChar(a%10+48);}
        template<typename Temp>inline void putDouble(Temp a){if(a<0){putChar(45);a=-a;}__int128 b=a;putInt(b);a-=b;a*=base2[Eps];b=a+Acc;putChar(46);putLeading(b,Eps);}
        OUT& operator<<(char a){putChar(a);return *this;}
        OUT& operator<<(char *a){while(*a>32)putChar(*a++);return *this;}
        OUT& operator<<(string a){for(auto c:a)putChar(c);return *this;}
        OUT& operator<<(int a){putInt(a);return *this;}
//        OUT& operator<<(long long a){putInt(a);return *this;}
//        OUT& operator<<(__int128 a){putInt(a);return *this;}
        OUT& operator<<(float a){putDouble(a);return *this;}
        OUT& operator<<(double a){putDouble(a);return *this;}
        OUT& operator<<(long double a){putDouble(a);return *this;}
        ~OUT(){flush();}
    };
}
using fastio::IN;
using fastio::OUT;
IN fin;
OUT fout;
#define cin fin
#define cout fout

const int inf=0x3f3f3f3f3f3f3f3f;
const int N=30,M=(1ll<<20)+10;
//const int mod=1e9+7;
int n,mod,T;
int f[M],g[M];
void fwt(int f[])
{
	for ( int i = 0 ; i < n ; i++ )
		for ( int j = 0 ; j < (1ll<<n) ; j+=(1ll<<i+1) )
			for ( int k = 0 ; k < (1ll<<i) ; k++ )
			{
				int x=f[j+k]+f[j+k+(1ll<<i)],y=f[j+k]-f[j+k+(1ll<<i)];
				f[j+k]=x,f[j+k+(1ll<<i)]=y;
			}
	for ( int i = 0 ; i < (1ll<<n) ; i++ )f[i]=(f[i]%mod+mod)%mod;
}

int w[N];
int pw(int x)
{
	int ans=1;
	int p=T;
	while(p)
	{
		if(p&1)ans=ans*x%mod;
		x=x*x%mod;p>>=1;
	}
	return ans;
}
int getlg(int n)
{
	int ans=0;
	while(n!=1)n>>=1,ans++;
	return ans;
}

signed main()
{
	ios::sync_with_stdio(false);
//	cin.tie(0),cout.tie(0);
//	freopen("peter.in","r",stdin);
//	freopen("peter.out","w",stdout);
	cin >> n >> T >> mod;n=getlg(n);mod<<=n;
	
	for ( int i = 0 ; i < (1ll<<n) ; i++ )cin >> f[i],g[i]=(__builtin_popcount(i)==1);
	fwt(f);fwt(g);
	for ( int i = 0 ; i < (1ll<<n) ; i++ )w[__builtin_popcount(i)]=g[i];
	for ( int i = 0 ; i <= n ; i++ )w[i]=pw(w[i]);
	for ( int i = 0 ; i < (1ll<<n) ; i++ )f[i]=f[i]*w[__builtin_popcount(i)]%mod;
	fwt(f);
	for ( int i = 0 ; i < (1ll<<n) ; i++ )cout << (f[i]>>n) << endl;
	return 0;
}
原文
存档评论1 条抓取于