P5277 【模板】多项式开根(加强版)
这题好像没有a0=0的数据。
以下二次剩余使用mod以内较小的那个解。
(一)利用B2=A 得到 B=A1/2 利用多项式的幂计算。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#define ll long long
#define llu unsigned ll
using namespace std;
const int maxn=8e5+100;
const int p=998244353;
const int mod=998244353;
const int g=3;
struct erCi
{
struct node
{
ll x,y;
node(){
}
node(ll a,ll b)
{
x=a,y=b;
}
};
node mul(const node &a,const node &b,const ll &w,const ll &p)
{
node ans;
ans.x=((a.x*b.x%p+a.y*b.y%p*w%p)%p+p)%p;
ans.y=((a.x*b.y%p+a.y*b.x%p)+p)%p;
return ans;
}
ll mypow(ll a,ll b,ll p)
{
ll ans=1;
while(b)
{
if(b&1) ans=ans*a%p;
a=a*a%p;
b>>=1;
}
return ans;
}
ll mypow(node a,ll b,ll w,ll p)
{
node ans=node(1,0);
while(b)
{
if(b&1) ans=mul(ans,a,w,p);
a=mul(a,a,w,p);
b>>=1;
}
return ans.x%p;
}
ll fi(ll n,ll p=mod)
{
n%=p;
if(p==2) return n;
if(mypow(n,(p-1)/2,p)==p-1) return -1;
ll a,w;
while(1)
{
a=rand()%p;
w=((a*a%p-n)%p+p)%p;
if(mypow(w,(p-1)/2,p)==p-1) break;
}
node ans=node(a,1);
ll res=mypow(ans,(p+1)/2,w,p);
return min(res,p-res);
}
}er;
int fi[maxn];
int a[maxn],inva[maxn],da[maxn],lna[maxn],c[maxn],iinv[maxn];
int f[maxn];
int n;
void get_inv(int n)
{
iinv[1]=1;
for(int i=2;i<=n;i++)
iinv[i]=1ll*(mod-mod/i)*iinv[mod%i]%mod;
}
int mypow(int a,int b)
{
if(b<0) return mypow(mypow(a,p-2),-b);
int ans=1;
while(b)
{
if(b&1) ans=1ll*ans*a%p;
a=1ll*a*a%p;
b>>=1;
}
return ans%p;
}
int getlen(int n,int m)//n,m是次数
{
int len=1,cnt=0;
while(len<=n+m) len<<=1,cnt