跳到正文

剪贴板

剪贴板 y8cm501c

原文
剪贴板信息

剪贴板信息

收录
更新
CF1060H Sophisticated Device 题解题目大意有长度为 5000 的数组 a,a1=s,a2=t,a3 至 a5000 均为 1。给定质数 p 和正整数 d,你需要做不超过 5000 次操作,形如:1. + x y z 表示 az←(ax+ay) mod p2.  ˆ x y 表示 ay←axd mod p3. f x 表示报告 ax=st mod p你不需要保证 x≠y,x≠z 或是 y≠z。你需要保证恰好有一次 3 操作,并且它之后没有任何操作。你不知道 s,t 的值,你需要保证对于所有 0≤s,t<p 都正确。2≤d≤10,d<p≤109+9.题目解法首先搭建一些简单操作,ay←ax×c 可以怎样实现?我们可以考虑使用快速幂的思想来实现,运用 log⁡ 次操作。ay←ax+c 可以使用 ay←ax+1×c 来实现。我们考虑先考虑简单情形,当 d=2 的时候,我们可以考虑xy=(x+y)2−x2−y22 ,减法就是乘 p−1,显然有 2 的逆元。能不能用更高次幂来计算平方?我们考虑二项式定理(x+a)d=∑i=0d(di)xian−i,以此使用高斯消元凑出系数 ci 使得x2=∑i=0dci(x+i)d,于是就可以使用 Θ(d×log⁡) 次操作解决。代码实现我们知道 x1 和 x0 的值,可以少列两个方程。\begin{array}{l} \LARGE\textsf{CF1060H Sophisticated Device 题解}\\ \Large\textsf{题目大意}\\ \textsf{有长度为 }5000\textsf{ 的数组 }a\textsf{,}a_1=s\textsf{,}a_2=t\textsf{,}a_3\textsf{ 至 }a_{5000}\textsf{ 均为 }1\textsf{。}\\ \textsf{给定质数}\text{ }p\textsf{ 和正整数 }d\textsf{,你需要做不超过 }5000\textsf{ 次操作,形如:}\\ \textrm{1. }\colorbox{d0d0d0}{\texttt{+ x y z}}\textsf{ 表示 }a_z\gets (a_x+a_y)\bmod p\\ \textrm{2. }\colorbox{d0d0d0}{\texttt{\^ }\texttt{ x y}}\textsf{ 表示 }a_y\gets a_x^d\bmod p\\ \textrm{3. }\colorbox{d0d0d0}{\texttt{f x}}\textsf{ 表示报告 }a_x=st\bmod p\\ \textsf{你不需要保证 }x\neq y,x\neq z\textsf{ 或是 }y\neq z\textsf{。}\\ \textsf{你需要保证恰好有一次 }3\textsf{ 操作,并且它之后没有任何操作。}\\ \textsf{你不知道 }s,t\textsf{ 的值,你需要保证对于所有 }0\le s,t< p\textsf{ 都正确。}\\ 2\le d\le 10,d<p\le10^9+9.\\ \Large\textsf{题目解法}\\ \textsf{首先搭建一些简单操作,}a_y\gets a_x\times c\textsf{ 可以怎样实现?}\\ \textsf{我们可以考虑使用快速幂}\textsf{的思想来实现,运用 }\log \textsf{ 次操作。}\\ a_y\gets a_x+c\textsf{ 可以使用 }a_y\gets a_x+1\times c\textsf{ 来实现。}\\ \textsf{我们考虑先考虑简单情形,当 }d=2\textsf{ 的时候,我们可以考虑}\\ xy=\frac{(x+y)^2-x^2-y^2}{2}\textsf{ ,减法就是乘 }p-1\textsf{,显然有 }2\textsf{ 的逆元。}\\ \textsf{能不能用更高次幂来计算平方?我们考虑二项式}\textsf{定理}\\ (x+a)^d=\sum\limits_{i=0}^d\binom{d}{i}x^ia^{n-i}\textsf{,以此使用高斯消元}\textsf{凑出系数 }c_i\textsf{ 使得}\\ x^2=\sum\limits_{i=0}^d c_i(x+i)^d\textsf{,于是就可以使用 }\Theta(d\times \log)\textsf{ 次操作解决。}\\ \Large\textsf{代码实现}\\ \textsf{我们知道 }x^1\textsf{ 和 }x^0\textsf{ 的值,可以少列两个方程。} \end{array}

#include <bits/stdc++.h>typedef long long ll;using namespace std;int G(int v){if(v>=0)return v+1;return v+5001;}//随便拿一个没用的变量const int const_1=133;//常数 1const int const_0=139;//常数 0const 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};//常数 iconst 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};//常数 -ill 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;}//操作 1void pow_to(int x,int y){cout<<" ˆ "<<x<<" "<<y<<endl;}//操作 2void multiplied_by(int x,int y,int c)//啥也没有时期的乘法{c=(c%p+p)%p;if(c<=2)c+=p;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);}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,x);add_to(x,x,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_2);c>>=1;}add_to(temp_1,const_0,y);}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);}int cof[14][14],temp[14],value[14],binom[144][144];void row_multiply(int v,int c){for(int i=0;i<=d-2;++i)cof[v][i]=(cof[v][i]*1ll*c)%p;value[v]=1ll*value[v]*c%p;}void row_add(int v,int c){for(int i=0;i<=d-2;++i)cof[v][i]=(cof[v][i]+cof[u][i]*1ll*c)%p;value[v]=(value[v]+1ll*value[u]*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\newcommand\ky[1]{{\tiny\color{0C8E5D}\texttt{#1}}} \newcommand\wf[1]{{\tiny\color{770088}\texttt{#1}}} \newcommand\nm[1]{{\tiny\texttt{#1}}} \newcommand\fc[1]{{\tiny\color{0C0CFF}\texttt{#1}}} \newcommand\no[1]{{\tiny\color{116644}\texttt{#1}}} \newcommand\co[1]{{\tiny\color{AB5602}\texttt{#1}}} \newcommand\st[1]{{\tiny\color{BA3D3D}\texttt{#1}}} \newcommand\fs[1]{{\tiny\color{555555}\texttt{#1}}} \newcommand\ar[2]{\nm{binom[}#1\nm{][}#2\nm{]}} \newcommand\ii{\ky{int}} \newcommand\lf{\nm{(}} \newcommand\rt{\nm{)}} \newcommand\Lf{\nm{\{}} \newcommand\Rt{\nm{\}}} \newcommand\sp{\nm{ }} \newcommand\lo{\nm{ll}} \newcommand\vd{\ky{void}} \newcommand\tb{\quad} \begin{array}{l} \fs{\#include <bits/stdc++.h>}\\ \wf{typedef }\ky{long long }\nm{ll;}\\ \wf{using namespace }\fc{std}\nm{;}\\ \ii\fc{ G}\lf\ii\nm{ v)}\Lf\wf{if}\lf\nm{v>=}\no{0}\rt\wf{return }\nm{v+}\no{1}\nm{;}\wf{return }\nm{v+}\no{5001}\nm{;}\Rt\co{//随便拿一个没用的变量}\\ \wf{const }\ii\nm{ const\_1=}\no{133}\nm{;}\co{//常数 1}\\ \wf{const }\ii\nm{ const\_0=}\no{139}\nm{;}\co{//常数 0}\\ \wf{const }\ii\nm{ const\_v[}\no{26}\nm{]=}\Lf\nm{const\_0,const\_1}\nm{,}\no{114}\nm{,}\no{115}\nm{,}\no{116}\nm{,}\no{117}\nm{,}\no{118}\nm{,}\no{119}\nm{,}\no{25}\nm{,}\no{26}\nm{,}\no{27}\nm{,}\no{28}\nm{,}\no{29}\nm{,}\no{30}\nm{,}\no{31}\nm{,}\no{32}\nm{,}\no{33}\nm{,}\no{34}\nm{,}\no{35}\nm{,}\no{36}\nm{,}\no{101}\nm{,}\no{102}\nm{,}\no{103}\nm{,}\no{104}\nm{,}\no{105}\nm{,}\no{106}\nm{\};}\co{//常数 i}\\ \wf{const }\ii\nm{ neg\_v[}\no{26}\nm{]=}\Lf\nm{const\_0}\nm{,}\no{233}\nm{,}\no{234}\nm{,}\no{235}\nm{,}\no{236}\nm{,}\no{237}\nm{,}\no{238}\nm{,}\no{239}\nm{,}\no{240}\nm{,}\no{241}\nm{,}\no{242}\nm{,}\no{243}\nm{,}\no{244}\nm{,}\no{245}\nm{,}\no{246}\nm{,}\no{247}\nm{,}\no{248}\nm{,}\no{249}\nm{,}\no{250}\nm{,}\no{251}\nm{,}\no{252}\nm{,}\no{253}\nm{,}\no{254}\nm{,}\no{255}\nm{,}\no{256}\nm{,}\no{257}\nm{\};}\co{//常数 -i}\\ \lo\fc{ qkpw}\nm{(ll a,ll b,ll m)\{ll ret=}\no{1}\nm{;}\wf{while}\nm{(b)}\wf{if}\nm{(b\&}\no{1}\rt\nm{ret=ret*a\%m;a=a*a\%m;b>>=}\no{1}\nm{;\}}\wf{return }\nm{ret;\}}\co{//快速幂}\\ \ii\fc{ inv}\nm{(ll x)\{}\wf{return }\ii\lf\nm{qkpw(x\%p,p-}\no{2ll}\nm{,p-}\no{0ll}\nm{));\}}\co{//计算逆元}\\ \vd\fc{ add\_to}\lf\ii\nm{ x,}\ii\nm{ y,}\ii\nm{ z)\{cout<<}\st{"+ "}\nm{<<x<<}\st{" "}\nm{<<y<<}\st{" "}\nm{<<z<<endl;\}}\co{//操作 1}\\ \vd\fc{ pow\_to}\lf\ii\nm{ x,}\ii\nm{ y)\{cout<<}\st{"\^ \ "}\nm{<<x<<}\st{" "}\nm{<<y<<endl;\}}\co{//操作 2}\\ \vd\fc{ multiplied\_by}\lf\ii\nm{ x,}\ii\nm{ y,}\ii\nm{ c)}\co{//啥也没有时期的乘法}\\ \Lf\\ \tb\nm{c=(c\%p+p)\%p;}\wf{if}\nm{(c<=}\no{2}\rt\nm{c+=p;c-=}\no{3}\nm{;}\co{//没有赋值操作,所以要预留 3 个}\\ \tb\ii\nm{ temp\_1=G(-}\no{1}\nm{);}\ii\nm{ temp\_2=G(-}\no{2}\nm{);}\nm{add\_to(x,x,temp\_2);add\_to(x,x,temp\_1);}\\ \tb\wf{if}\nm{(c\&}\no{1}\rt\nm{add\_to(x,temp\_1,temp\_1);}\nm{c>>=}\no{1}\nm{;}\\ \tb\wf{while}\lf\no{1}\rt\\ \tb\Lf\\ \tb\tb\wf{if}\nm{(c\&}\no{1}\nm{)add\_to(temp\_2,temp\_1,temp\_1);}\nm{c>>=}\no{1}\nm{;}\wf{if}\nm{(c)add\_to(temp\_2,temp\_2,temp\_2);}\wf{else break}\nm{;}\\ \tb\Rt\\ \tb\nm{add\_to(x,temp\_1,y);}\\ \Rt\\ \vd\fc{ multiply\_by}\lf\ii\nm{ x,}\ii\nm{ y,}\ii\nm{ c)}\co{//预处理之后的乘法}\\ \Lf\\ \tb\nm{c=(c\%p+p)\%p;}\\ \tb\wf{if}\nm{(c==}\no{0}\nm{)\{add\_to(const\_0,const\_0,y);}\wf{return}\nm{;\}}\\ \tb\wf{if}\nm{(c==}\no{1}\nm{)\{add\_to(const\_0,x,y);}\wf{return}\nm{;\}}\\ \tb\wf{if}\nm{(c==}\no{2}\nm{)\{add\_to(x,x,y);}\wf{return}\nm{;\}}\\ \tb\wf{if}\nm{(c==}\no{3}\nm{)}\Lf\ii \nm{ temp=G(-}\no{1}\nm{);}\nm{add\_to(x,x,temp);}\nm{add\_to(x,temp,y);}\wf{return}\nm{;\}}\\ \tb\wf{if}\nm{(c==}\no{4}\nm{)}\Lf\nm{add\_to(x,x,x);}\nm{add\_to(x,x,y);}\wf{return}\nm{;\}}\\ \tb\wf{if}\nm{(c==}\no{5}\nm{)}\Lf\ii \nm{ temp=G(-}\no{1}\nm{);}\nm{add\_to(x,x,temp);}\nm{add\_to(x,temp,y);}\nm{add\_to(y,temp,y);}\wf{return}\nm{;\}}\\ \tb\co{//小数据特判}\\ \tb\ii\nm{ temp\_1=G(-}\no{1}\nm{);}\ii\nm{ temp\_2=G(-}\no{2}\nm{);add\_to(x,const\_0,temp\_2);add\_to(const\_0,const\_0,temp\_1);}\\ \tb\wf{if}\nm{(c\&}\no{1}\nm{)add\_to(x,temp\_1,temp\_1);c>>=}\no{1}\nm{;}\\ \tb\wf{while}\nm{(c)}\\ \tb\Lf\\ \tb\tb\nm{add\_to(temp\_2,temp\_2,temp\_2);}\wf{if}\nm{(c\&}\no{1}\nm{)add\_to(temp\_2,temp\_1,temp\_2);c>>=}\no{1}\nm{;}\\ \tb\Rt\\ \tb\nm{add\_to(temp\_1,const\_0,y);}\\ \Rt\\ \vd\fc{ add\_by}\lf\ii\nm{ x,}\ii\nm{ y,}\ii\nm{ c)}\\ \Lf\\ \tb\ii\nm{ temp\_1=G(-}\no{114}\nm{);c=(c\%p+p)\%p};\wf{if}\nm{(c<=}\no{25}\nm{)\{add\_to(x,const\_v[c],y);}\wf{return}\nm{;)}\wf{if}\nm{(p-c<=}\no{25}\nm{)\{add\_to(x,neg\_v[p-c],y);}\wf{return}\nm{;\}}\\ \tb\co{//小数据特判}\\ \tb\nm{multiply\_by(const\_1,temp\_1,c);add\_to(x,temp\_1,y);}\\ \Rt\\ \ii\nm{ cof[}\no{14}\nm{][}\no{14}\nm{],temp[}\no{14}\nm{],value[}\no{14}\nm{],binom[}\no{144}\nm{][}\no{144}\nm{];}\\ \vd\fc{ row\_multiply}\lf\ii\nm{ v,}\ii\nm{ c)}\\ \Lf\\ \tb\wf{for}\lf\ii\nm{ i=}\no{0}\nm{;i<=d-}\no{2}\nm{;++i)}\\ \tb\nm{cof[v][i]=(cof[v][i]*}\no{1ll}\nm{*c)\%p;}\\ \tb\nm{value[v]=}\no{1ll}\nm{*value[v]*c\%p;}\\ \Rt\\ \vd\fc{ row\_add}\lf\ii\nm{ v,}\ii\nm{ c)}\\ \Lf\\ \tb\wf{for}\lf\ii\nm{ i=}\no{0}\nm{;i<=d-}\no{2}\nm{;++i)}\\ \tb\nm{cof[v][i]=(cof[v][i]+cof[u][i]*}\no{1ll}\nm{*c)\%p;}\\ \tb\nm{value[v]=(value[v]+}\no{1ll}\nm{*value[u]*c)\%p;}\\ \Rt\\ \co{//初等行变换}\\ \vd\fc{ get\_squared}\lf\ii\nm{ x,}\ii\nm{ y)}\co{// 计算平方}\\ \Lf\\ \tb\ii\nm{ temp\_1=G(-}\no{333}\nm{),temp\_2=G(-}\no{64}\nm{);multiply\_by(temp\_2,temp\_2,}\no{0}\nm{);}\wf{for}\lf\ii\nm{ i=}\no{0}\nm{;i<=d-}\no{2}\nm{;++i)}\nm{temp[i]=G(-}\no{4}\nm{-i);}\co{//拿一些临时变量}\\ \tb\ar{\no{0}}{\no{0}}\nm{=}\no{1}\nm{;}\wf{for}\lf\ii\nm{ i=}\no{1}\nm{;i<=}\no{142}\nm{;++i)\{}\ar{\nm{i}}{\no{0}}\nm{=}\ar{\nm{i}}{\nm{i}}\nm{=}\no{1}\nm{;}\wf{for}\lf\ii\nm{ j=}\no{1}\nm{;j<i;++j)}\Lf\ar{\nm{i}}{\nm{j}}\nm{=}\lf\ar{\nm{i-}\no{1}}{\nm{j}}+\ar{\nm{i-}\no{1}}{\nm{j-}\no{1}}\rt\nm{\%p;\}\}}\co{//组合数}\\ \tb\wf{for}\lf\ii\nm{ i=}\no{0}\nm{;i<=d-}\no{2}\nm{;++i)}\\ \tb\Lf\\ \tb\tb\nm{add\_by(x,temp\_1,i);pow\_to(temp\_1,temp[i]);}\ii \nm{ cur=}\no{1}\nm{;}\\ \tb\tb\wf{for}\lf\ii\nm{ j=d}\nm{;j>=}\no{0}\nm{;--j)}\\ \tb\tb\Lf\\ \tb\tb\tb\wf{if}\nm{(j>=}\no{2}\nm{)}\\ \tb\tb\tb\Lf\\ \tb\tb\tb\tb\nm{cof[i][j-}\no{2}\nm{]=}\no{1ll}\nm{*cur*binom[d][j]\%p;}\\ \tb\tb\tb\Rt\\ \tb\tb\tb\wf{else}\\ \tb\tb\tb\Lf\\ \tb\tb\tb\tb\ii\nm{ v=}\no{1ll}\nm{*cur*binom[d][j]\%p;}\\ \tb\tb\tb\tb\wf{if}\nm{(j==}\no{1}\nm{)}\\ \tb\tb\tb\tb\Lf\\ \tb\tb\tb\tb\tb\nm{multiply\_by(x,temp\_1,(p-v)\%p);}\\ \tb\tb\tb\tb\tb\nm{add\_to(temp[i],temp\_1,temp[i]);}\\ \tb\tb\tb\tb\Rt\\ \tb\tb\tb\tb\wf{else}\\ \end{array} {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_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(0);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;}\newcommand\ky[1]{{\tiny\color{0C8E5D}\texttt{#1}}} \newcommand\wf[1]{{\tiny\color{770088}\texttt{#1}}} \newcommand\nm[1]{{\tiny\texttt{#1}}} \newcommand\fc[1]{{\tiny\color{0C0CFF}\texttt{#1}}} \newcommand\no[1]{{\tiny\color{116644}\texttt{#1}}} \newcommand\co[1]{{\tiny\color{AB5602}\texttt{#1}}} \newcommand\st[1]{{\tiny\color{BA3D3D}\texttt{#1}}} \newcommand\fs[1]{{\tiny\color{555555}\texttt{#1}}} \newcommand\ar[2]{\nm{binom[}#1\nm{][}#2\nm{]}} \newcommand\ii{\ky{int}} \newcommand\lf{\nm{(}} \newcommand\rt{\nm{)}} \newcommand\Lf{\nm{\{}} \newcommand\Rt{\nm{\}}} \newcommand\sp{\nm{ }} \newcommand\lo{\nm{ll}} \newcommand\vd{\ky{void}} \newcommand\tb{\quad} \begin{array}{l} \tb\tb\tb\tb\Lf\\ \tb\tb\tb\tb\tb\nm{add\_by(temp[i],temp[i],(p-v)\%p);}\\ \tb\tb\tb\tb\Rt\\ \tb\tb\tb\Rt\\ \tb\tb\tb\nm{cur=}\no{1ll}\nm{*cur*i\%p;}\\ \tb\tb\Rt\\ \tb\tb\nm{value[i]=(i==}\no{0}\nm{);}\co{//平方要消到 1,其它 0}\\ \tb\Rt\\ \tb\wf{for}\lf\ii\nm{ i=}\no{0}\nm{;i<=d-}\no{2}\nm{;++i) }\wf{for}\lf\ii\nm{ j=}\no{0}\nm{;j<i}\nm{;++j) }\nm{swap(cof[i][j],cof[j][i]);}\co{自己推一下式子,就能理解了}\\ \tb\wf{for}\lf\ii\nm{ i=}\no{0}\nm{;i<=d-}\no{2}\nm{;++i)}\co{//消元}\\ \tb\Lf\\ \tb\tb\nm{row\_multiply(i,inv(cof[i][d-}\no{2}\nm{-i]));}\\ \tb\tb\wf{for}\lf\ii\nm{ j=i+}\no{1}\nm{;j<=d-}\no{2}\nm{;++j) }\\ \tb\tb\Lf\\ \tb\tb\tb\nm{row\_add(i,j,(p-cof[j][d-}\no{2}\nm{-i])\%p);}\\ \tb\tb\Rt\\ \tb\Rt\\ \tb\wf{for}\lf\ii\nm{ i=d-}\no{2}\nm{;i>=}\no{0}\nm{;--i)}\co{//消元}\\ \tb\Lf\\ \tb\tb\wf{for}\lf\ii\nm{ j=}\no{0}\nm{;j<i;++j)}\\ \tb\tb\Lf\\ \tb\tb\tb\nm{row\_add(i,j,(p-cof[j][d-2-i])\%p);}\\ \tb\tb\Rt\\ \tb\Rt\\ \tb\wf{for}\lf\ii\nm{ i=d-}\no{2}\nm{;i>=}\no{0}\nm{;--i)}\co{//得出结果}\\ \tb\Lf\\ \tb\tb\nm{multiply\_by(temp[i],temp[i],value[d-}\no{2}\nm{-i]);}\\ \tb\tb\nm{add\_to(temp\_2,temp[i],temp\_2);}\\ \tb\Rt\\ \tb\nm{add\_by(temp\_2,y,}\no{0}\nm{);}\\ \Rt\\ \vd\fc{ init}\nm{()}\co{//预处理常数}\\ \Lf\\ \tb\nm{multiplied\_by(const\_0,const\_0,}\no{0}\nm{);}\\ \tb\wf{for}\lf\ii\nm{ i=}\no{2}\nm{;i<=}\no{25}\nm{;++i)add\_to(const\_1,const\_v[i-}\no{1}\nm{],const\_v[i]);}\\ \tb\nm{multiply\_by(const\_1,neg\_v[}\no{1}\nm{],p-}\no{1}\nm{);}\\ \tb\wf{for}\lf\ii\nm{ i=}\no{2}\nm{;i<=}\no{25}\nm{;++i)}\nm{add\_to(neg\_v[}\no{1}\nm{],neg\_v[i-}\no{1}\nm{],neg\_v[i]);}\\ \Rt\\ \ii\fc{ main}\nm{()}\\ \Lf\\ \tb\nm{ios\_base::sync\_with\_stdio(}\no{0}\nm{);}\\ \tb\nm{cin>>d>>p;init();}\\ \tb\nm{get\_squared(G(}\no{0}\nm{),G(}\no{2}\nm{));}\\ \tb\nm{get\_squared(G(}\no{1}\nm{),G(}\no{3}\nm{));}\\ \tb\nm{add\_to(G(}\no{0}\nm{),G(}\no{1}\nm{),G(}\no{4}\nm{));}\\ \tb\nm{get\_squared(G(}\no{4}\nm{),G(}\no{5}\nm{));}\\ \tb\nm{multiply\_by(G(}\no{2}\nm{),G(}\no{2}\nm{),p-}\no{1}\nm{);}\\ \tb\nm{multiply\_by(G(}\no{3}\nm{),G(}\no{3}\nm{),p-}\no{1}\nm{);}\\ \tb\nm{add\_to(G(}\no{2}\nm{),G(}\no{3}\nm{),G(}\no{6}\nm{));}\\ \tb\nm{add\_to(G(}\no{5}\nm{),G(}\no{6}\nm{),G(}\no{7}\nm{));}\\ \tb\nm{multiply\_by(G(}\no{7}\nm{),G(}\no{7}\nm{),(p+}\no{1}\nm{)/}\no{2}\nm{);}\\ \tb\nm{cout<<}\st{"f "}\nm{<<G(}\no{7}\nm{)<<endl;}\co{//d=2 的解法}\\ \tb\wf{return }\no{0}\nm{;}\\ \Rt \end{array}