P4721 【模板】分治 FFT

时间:2023-12-22 14:37:50

其实是分治ntt,因为fft会爆精度,真*裸题

分治过程和fft的一模一样,主要就是ntt精度高,用原根来代替fft中的\(w_n^k\)

1.定义:设m>1,(a,m)==1,满足\(a^r=1(modm)\)的最小r是\(\phi(r)\),那么a就是m的原根

2.性质:如果g是p原根,那么\(g^1,g^2...g^(p-1)\)是1到p-1的排列,各不相同

对于\(g^k=x(mod p)\),我们记I(x)=k,

有\(I(a*b)=I(a)*I(b)(mod p-1),I(a^k)=kI(a)(mod p-1)\)

3.求法:求原根的话就是找到一个g,对于\(p-1=p_1^{k_1}*p_2^{k_2}***p_x^{k_x}\),都有\(g^{\frac{p-1}{p_i}}!=1(1<=i<=k)\)

直接遍历即可,因为原根不是很大

//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 998244353
#define ld long double
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
//#define cd complex<double>
#define ull unsigned long long
#define base 1000000000000000000
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
template<typename T>
inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>
inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;} using namespace std; const double eps=1e-8;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=200000+10,maxn=400000+10,inf=0x3f3f3f3f; ll x[N<<3],y[N<<3];
int rev[N<<3];
void getrev(int bit)
{
for(int i=0;i<(1<<bit);i++)
rev[i]=(rev[i>>1]>>1) | ((i&1)<<(bit-1));
}
void ntt(ll *a,int n,int dft)
{
for(int i=0;i<n;i++)
if(i<rev[i])
swap(a[i],a[rev[i]]);
for(int step=1;step<n;step<<=1)
{
ll wn=qp(3,(mod-1)/(step*2));
if(dft==-1)wn=qp(wn,mod-2);
for(int j=0;j<n;j+=step<<1)
{
ll wnk=1;
for(int k=j;k<j+step;k++)
{
ll x=a[k];
ll y=wnk*a[k+step]%mod;
a[k]=(x+y)%mod;a[k+step]=(x-y+mod)%mod;
wnk=wnk*wn%mod;
}
}
}
if(dft==-1)
{
ll inv=qp(n,mod-2);
for(int i=0;i<n;i++)a[i]=a[i]*inv%mod;
}
}
ll dp[N],a[N];
void cdq(int l,int r)
{
if(l==r)return ;
int m=(l+r)>>1;
cdq(l,m);
int sz=0;
while((1<<sz)<=(r-l+1))sz++;sz++;
getrev(sz);int len=(1<<sz);
for(int i=0;i<=len;i++)x[i]=y[i]=0;
for(int i=l;i<=m;i++)x[i-l]=dp[i];
for(int i=1;i<=r-l;i++)y[i-1]=a[i];
ntt(x,len,1),ntt(y,len,1);
for(int i=0;i<=len;i++)x[i]=x[i]*y[i]%mod;
ntt(x,len,-1);
for(int i=m+1;i<=r;i++)
{
dp[i]+=x[i-l-1]%mod;
dp[i]%=mod;
}
cdq(m+1,r);
}
int main()
{
int n;scanf("%d",&n);
dp[0]=1;
for(int i=1;i<n;i++)
{
scanf("%lld",&a[i]);
a[i]%=mod;dp[i]=0;
}
cdq(0,n-1);
for(int i=0;i<n;i++)printf("%lld ",dp[i]);puts("");
return 0;
}
/******************** ********************/

求原根

//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000007
#define ld long double
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
//#define cd complex<double>
#define ull unsigned long long
#define base 1000000000000000000
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
template<typename T>
inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>
inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;} using namespace std; const double eps=1e-8;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=200000+10,maxn=400000+10,inf=0x3f3f3f3f; int main()
{
ll n,p;scanf("%lld",&n);
vi v;p=n-1;
for(ll i=2;i*i<=p;i++)
{
if(p%i==0)
{
v.pb(i);
while(p%i==0)p/=i;
}
}
if(p!=1)v.pb(p);
for(int i=1;i<=n;i++)
{
bool ok=1;
for(int j=0;j<v.size();j++)
if(qp(i,(n-1)/v[j],n)==1)
ok=0;
if(ok)return 0*printf("%d\n",i);
}
return 0;
}
/******************** ********************/