当前位置:网站首页>FFT [template]

FFT [template]

2022-06-25 08:05:00 Mfy's little brother 1

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const int maxn=(1<<20)+5;
const double PI=acos(-1);
struct Complex
{
    
    double x,y;
    Complex operator+(const Complex &o) const{
    return{
    x+o.x,y+o.y};}  
    Complex operator-(const Complex &o) const{
    return{
    x-o.x,y-o.y};}  
    Complex operator*(const Complex &o) const{
    return{
    x*o.x-y*o.y,x*o.y+y*o.x};} 
}A[maxn],B[maxn];
int rev[maxn];
int limit=1;
void FFT(Complex *a,int inv){
    
    for(int i=0;i<limit;i++)
	    if(i<rev[i])swap(a[i],a[rev[i]]);

    for(int mid=1;mid<limit;mid<<=1){
    
        Complex Wn=Complex({
    cos(PI/mid),inv*sin(PI/mid)});

        for(int i=0;i<limit;i+=mid*2){
    
            Complex w=Complex({
    1,0});
            for(int j=0;j<mid;j++,w=w*Wn){
    
                Complex x=a[i+j],y=w*a[i+j+mid];
                a[i+j]=x+y,a[i+j+mid]=x-y;
            }
        }
    }
    if(inv==-1) for(int i=0;i<limit;i++) A[i].x/=limit;
}
// --------FFT
int n;
const int Bs=500000;
int vis[maxn];
int main(){
    
    cin>>n;
    for(int i=1;i<=n;i++)
    {
    
        int b;
        cin>>b;
        A[b].x=1;//ax^b,  Input b Is power ,1 Is the coefficient a 
        B[Bs-b].x=1;// use Bs-b A negative number  
    }
    int bit=20;
    limit=1<<20;//limit Must be greater than the maximum number of times  
    for(int i=1;i<limit;i++) rev[i]=rev[i>>1]>>1|(i&1)<<(bit-1); 
    FFT(A,1),FFT(B,1);
    for(int i=0;i<limit;i++) A[i]=A[i]*B[i];
    FFT(A,-1);
    for(int i=0;i<=Bs;i++) vis[i]=int(A[i+Bs].x+0.5);// The coefficient , Because it's subtraction , Added Bs, Greater than Bs And meet the requirements , Determine whether there is a modified power  
    
    for(int i=n;;i++)
    {
    
        bool ok=1;
        for(int j=i;j<=Bs;j+=i)
            if(vis[j]) {
    ok=0;break;}
        if(ok) return cout<<i<<'\n',0;
    }
    return 0;
}


原网站

版权声明
本文为[Mfy's little brother 1]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206250624539599.html