FFT质数打表程序

时间:2021-09-19 07:57:34
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
void sol(ll x){
int y=0;
for(ll i=2;i*i<=x;++i)
if(x%i==0){
int j=0;
for(;x%i==0;x/=i)++j;
if(y++)cout<<"*";
cout<<i;
if(j!=1)cout<<"^"<<j;
}
if(x!=1){
if(y)cout<<"*";
cout<<x;
}
cout<<endl;
}
bool jud(ll x){
for(ll i=2;i*i<=x;++i)
if(x%i==0)return 0;
return 1;
}
ll wop(ll s,ll n,ll p){
ll t=1;
for(;n;n>>=1){
if(n&1)t=t*s%p;
if(n>1)s=s*s%p;
}
return t;
}
ll phi(ll x){
ll s=x;
for(ll i=2;i*i<=x;++i)
if(x%i==0){
while(x%i==0)x/=i;
s=s/i*(i-1);
}
if(x!=1)s=s/x*(x-1);
return s;
}
ll gen(ll p){
static ll c[16];
ll s=phi(p),x=s;
int k=0;
for(ll i=2;i*i<=x;++i)
if(x%i==0){
while(x%i==0)x/=i;
c[k++]=i;
}
if(x!=1)c[k++]=x;
for(ll i=2;;++i){
int j=0;
while(j!=k&&wop(i,s/c[j],p)!=1)
++j;
if(j==k)return i;
}
}
void out(ll p){
int j=__builtin_ctzll(p-1);
cout<<setw(8)<<p;
cout<<setw(4)<<gen(p);
cout<<" 2^"<<j;
cout<<" * "<<setw(5)<<(p-1>>j);
cout<<endl;
}
int main(){
for(ll i=1;;++i){
ll j=(i<<20)+1;
if(j>2e9)break;
if(jud(j))out(j);
}
}