跳到正文

题解

CF1060H Sophisticated Device 题解

MatrixGroup约 12 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 12 分钟
获赞
2
题目
CF1060H

版本

  1. v1当前

链接

KaTeX\KaTeX 版本

视频题解

前言

第一个 *3300,还是独立做出来的,很感动啊。于是现在补了这个一个题解。

题目大意

有长度为 50005000 的数组 aa,a1=sa_1=s,a2=ta_2=t,a3a_3 至 a5000a_{5000} 均为 11。

给定质数 pp 和正整数 dd,你需要做不超过 50005000 次形如以下的操作:

  1. + x y z\boxed{\texttt{+ x y z}} 表示 az←(ax+ay) mod pa_z\gets (a_x+a_y)\bmod p。
  2.  ˆ x y\boxed{{\texttt{\^ }\texttt{ x y}}} 表示 ay←axd mod pa_y\gets a_x^d\bmod p。
  3. f x\boxed{\texttt{f x}} 表示报告 ax=st mod pa_x=st\bmod p。

你不需要保证 x≠y,x≠zx\neq y,x\neq z 或是 y≠zy\neq z。

你需要保证恰好有一次 33 操作,并且它之后没有任何操作。

你不知道 s,ts,t 的值,你需要保证对于所有 0≤s,t<p0\le s,t< p 都正确。

2≤d≤10,d<p≤109+92\le d\le 10,d<p\le10^9+9。

题目解法

我们使用自底而上、从特殊到一般的做题策略。

自底而上

我们考虑如何实现一些基本操作。我们如何实现 ay←ax×ca_y\gets a_x\times c?

事实上,我们可以使用快速幂的思想用 log⁡\log 次操作来完成这一目标。注意到没有 ay←axa_y\gets a_x 的操作,所以要预留几个数做加法。

参考实现:

C++
void multiplied_by(int x,int y,int c)//啥也没有时期的乘法
{
	c=(c%p+p)%p;
	if(c==0) c=p;
	if(c==1) c=p+1;
	if(c==2) c=p+2;
	c-=3;//没有赋值操作,所以要预留 3 个
	int temp_1=G(-1);
	int temp_2=G(-2);
	add_to(x,x,temp_2);
	add_to(x,x,temp_1);
	if(c&1) add_to(x,temp_1,temp_1);
	c>>=1;
	while(1)//快速幂
	{
		if(c&1) add_to(temp_2,temp_1,temp_1);
		c>>=1;
		if(c)add_to(temp_2,temp_2,temp_2);
		else break;
	}
	add_to(x,temp_1,y);
}

但是这样实在是有一点繁琐,因为它十分常用。我们可以用它先进行一些预处理:

C++
void init()//预处理常数
{
	multiplied_by(const_0,const_0,0);
	for(int i=2;i<=25;++i)add_to(const_1,const_v[i-1],const_v[i]);
	multiply_by(const_v[1],neg_v[1],p-1);
	for(int i=2;i<=25;++i)add_to(neg_v[1],neg_v[i-1],neg_v[i]);
}

这样就可以对常用的乘法减少一些操作次数:

C++
void multiply_by(int x,int y,int c)//预处理之后的乘法
{
	c=(c%p+p)%p;
	if(c==0){add_to(const_0,const_0,y);return;}
	if(c==1){add_to(const_0,x,y);return;}
	if(c==2){add_to(x,x,y);return;}
	if(c==3){int temp=G(-1);add_to(x,x,temp);add_to(x,temp,y);return;}
	if(c==4){add_to(x,x,y);add_to(y,y,y);return;}
	if(c==5){int temp=G(-1);add_to(x,x,temp);add_to(x,temp,y);add_to(y,temp,y);return;}
	//小数据特判
	int temp_1=G(-1);
	int temp_2=G(-2);
	add_to(x,const_0,temp_2);
	add_to(const_0,const_0,temp_1);
	if(c&1) add_to(x,temp_1,temp_1);
	c>>=1;
	while(c)
	{
		add_to(temp_2,temp_2,temp_2);
		if(c&1) add_to(temp_2,temp_1,temp_1);
		c>>=1;
	}
	add_to(temp_1,const_0,y);
}

还有一个常用操作是 ay←ax+ca_y\gets a_x+c。我们考虑 ay←ax+1×ca_y\gets a_x+1\times c 即可。

C++
void add_by(int x,int y,int c)
{
	int temp_1=G(-114);c=(c%p+p)%p;
	if(c<=25){add_to(x,const_v[c],y);return;}
	if(p-c<=25){add_to(x,neg_v[p-c],y);return;}
	//小数据特判
	multiply_by(const_1,temp_1,c);
	add_to(x,temp_1,y);
}

从特殊到一般

我们考虑 d=2d=2 的情形。显然地,xy=(x+y)2−x2−y22xy=\dfrac{(x+y)^2-x^2-y^2}{2},减法可以使用乘 p−1p-1 实现,除以二可以使用乘 22 的乘法逆元实现。

我们考虑扩展这个想法,但是 xy=(x+y)3−x3−y33(x+y)xy=\dfrac{(x+y)^3-x^3-y^3}{3(x+y)} 是不行的,因为你无法求一个非常数的逆元。

于是考虑是否可以在 d>2d>2 的情况下求出一个数的平方。根据二项式定理,

(x+a)d=∑i=0d(di)xian−i(x+a)^d=\sum\limits_{i=0}^d\binom{d}{i}x^ia^{n-i}

这里面存在 x2x^2 项。

我们使用高斯消元可以求出 cic_i 使得:

x2=∑i=0dci(x+i)dx^2=\sum\limits_{i=0}^d c_i(x+i)^d

于是我们就可以使用 d+1d+1 次 log⁡\log 次数的乘法来求出一个数的平方,然后调用三次即可。

实现精细的话,显然不会超过 50005000 次操作。

关于为什么一定可以凑出来,你可以把表打出来,发现分母上的数都不会大于 dd,这也是 d<pd<p 这个条件的目的。有非打表证明方法欢迎私信作者。

代码实现

注意到我们知道 x1x^1 和 x0x^0 的值,所以可以少列两个方程。

C++
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int d,p;
int G(int v){if(v>=0) return v+1;return v+5001;}//随便拿一个没用的变量
const int const_1=133;//常数 1
const int const_0=139;//常数 0
const int const_v[26]={const_0,const_1,114,115,116,117,118,119,25,26,27,28,29,30,31,32,33,34,35,36,101,102,103,104,105,106};//常数i
const int neg_v[26]={const_0,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,256,257};//常数-i
ll qkpw(ll a,ll b,ll m){ll ret=1;while(b){if(b&1)ret=ret*a%m;a=a*a%m;b>>=1;}return ret;}//快速幂
int inv(ll x){return int(qkpw(x%p,p-2ll,p-0ll));}//计算逆元
void add_to(int x,int y,int z){cout<<"+ "<<x<<" "<<y<<" "<<z<<endl;}//操作 1
void pow_to(int x,int y){cout<<"^ "<<x<<" "<<y<<endl;}//操作 2
void multiplied_by(int x,int y,int c)//啥也没有时期的乘法
{
	//见上文
}
void multiply_by(int x,int y,int c)//预处理之后的乘法
{
	//见上文
}
void add_by(int x,int y,int c)
{
	//见上文
}
int cof[14][14],temp[14],value[14];
int binom[144][144];
void row_multiply(int v,int c)
{
	if(c==1) return;
	for(int i=0;i<=d;++i)
	cof[v][i]=(cof[v][i]*1ll*c)%p;
	value[v]=1ll*value[v]*c%p;
}
void row_add(int u,int v,int c)
{
	if(c==0) return;
	for(int i=0;i<=d;++i)
	cof[v][i]=(cof[v][i]+cof[u][i]*1ll*c)%p;
	value[v]=(value[v]+value[u]*1ll*c)%p;
}
//初等行变换
void get_squared(int x,int y)// 计算平方
{
	int temp_1=G(-333),temp_2=G(-64);multiply_by(temp_2,temp_2,0);for(int i=0;i<=d-2;++i) temp[i]=G(-4-i);//拿一些临时变量
	binom[0][0]=1;for(int i=1;i<=142;++i){binom[i][0]=binom[i][i]=1;for(int j=1;j<i;++j){binom[i][j]=(binom[i-1][j]+binom[i-1][j-1])%p;}}//组合数
	for(int i=0;i<=d-2;++i)//预处理系数和值
	{
		add_by(x,temp_1,i);pow_to(temp_1,temp[i]);
		int cur=1;
		for(int j=d;j>=0;--j)
		{
			if(j>=2)
			{
				cof[i][j-2]=1ll*cur*binom[d][j]%p;
			}
			else
			{
				int v=1ll*cur*binom[d][j]%p;
				if(j==1)
				{
					multiply_by(x,temp_1,(p-v)%p);
					add_to(temp[i],temp_1,temp[i]);
				}
				else
				{
					add_by(temp[i],temp[i],(p-v)%p);
				}
			}
			cur=1ll*cur*i%p;
		}
		value[i]=(i==0);//平方要消到 1,其它 0
	}
	for(int i=0;i<=d-2;++i) for(int j=0;j<i;++j) swap(cof[i][j],cof[j][i]);//自己推一下式子,就能理解了
	for(int i=0;i<=d-2;++i)//消元
	{
		row_multiply(i,inv(cof[i][d-2-i]));
		for(int j=i+1;j<=d-2;++j)
		{
			row_add(i,j,(p-cof[j][d-2-i])%p);
		}
	}
	for(int i=d-2;i>=0;--i)//消元
	{
		for(int j=0;j<i;++j)
		{
			row_add(i,j,(p-cof[j][d-2-i])%p);
		}
	}
	for(int i=d-2;i>=0;--i)//得出结果
	{
		multiply_by(temp[i],temp[i],value[d-2-i]);
		add_to(temp_2,temp[i],temp_2);
	}
	add_by(temp_2,y,0);
}
void init()//预处理常数
{
	multiplied_by(const_0,const_0,0);
	for(int i=2;i<=25;++i)add_to(const_1,const_v[i-1],const_v[i]);
	multiply_by(const_v[1],neg_v[1],p-1);
	for(int i=2;i<=25;++i)add_to(neg_v[1],neg_v[i-1],neg_v[i]);
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin>>d>>p;
	init();
	get_squared(G(0),G(2));
	get_squared(G(1),G(3));
	add_to(G(0),G(1),G(4));
	get_squared(G(4),G(5));
	multiply_by(G(2),G(2),p-1);
	multiply_by(G(3),G(3),p-1);
	add_to(G(2),G(3),G(6));
	add_to(G(5),G(6),G(7));
	multiply_by(G(7),G(7),(p+1)/2);
	cout<<"f "<<G(7)<<endl;
    //d=2 的解法
	return 0;
}
原文