4803: 逆欧拉函数
Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 232 Solved: 49
[Submit][Status][Discuss]
Description
已知phi(N),求N。
Input
两个正整数,分别表示phi(N)和K。
phi(N)<=10^14,K<=1000
Output
按升序输出满足条件的最小的K个N。
Sample Input
8 4
Sample Output
15 16 20 24
这题和BZOJ 3643: Phi的反函数 搜索基本一样,就是数据范围巨了点
Z基先A掉过,就去讨论了一番
他这样讲到(就是这意思。。。):因为数据范围大了,所以判素数要用Miller_Rabin,然后优化一下搜索就好了
具体就是酱:
预处理素数时,有很多的素数可能是没用的,但在搜索里会耗时
所以枚举n的因子,当素数p满足p-1是n的因子时,就把它推进vector
然后素数测试判素数,之后只管爆搜就好了 还有K个答案就推priority_queue就好了
get技能 素数测试
#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef double db;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch<='9'&&ch>='0'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
void print(ll x)
{if(x<0)putchar('-'),x=-x;if(x>=10)print(x/10);putchar(x%10+'0');}
const int N=1010;
priority_queue<ll>q;
ll ans[N];
inline ll qmul(ll x,ll y,ll mod)
{
ll res=0;
while(y)
{
if(y&1)res=(res+x)%mod;
x=(x+x)%mod;
y>>=1;
}
return res;
}
inline ll qpow(ll x,ll y,ll mod)
{
ll res=1;
while(y)
{
if(y&1)res=qmul(res,x,mod);
x=qmul(x,x,mod);
y>>=1;
}
return res;
}
bool check(ll bas,ll r,ll n,int s)
{
ll res=qpow(bas,r,n),p=res;
for(int i=1;i<=s;++i)
{
res=qmul(res,res,n);
if(res==1&&p!=1&&p!=n-1)return 0;
p=res;
}
if(res^1)return 0;
return 1;
}
bool M_R(ll n)
{
if(n<=1)return 0;if(n==2)return 1;
if(!(n&1))return 0;
ll r=n-1,s=0;
while(!(r&1))r>>=1,s++;
for(int i=0;i<10;++i)
if(!check(rand()%(n-1)+1,r,n,s))return 0;
return 1;
}
vector<ll>prime;
void dfs(int step,ll aim,ll now)
{
if(aim==1){if(now<q.top())q.pop(),q.push(now);return ;}
if(step==prime.size())return ;
dfs(step+1,aim,now);
for(int i=1;aim>1;++i)
if(aim%(prime[step]-(i==1))==0)
{
now*=prime[step];
aim/=(prime[step]-(i==1));
dfs(step+1,aim,now);
}
else break;
}
int main()
{
srand(2029);
ll n;scanf("%lld",&n);int K=read();
register int i,top=0;
for(i=1;i<=K;++i)q.push(1ll<<60);
for(ll i=1;1ll*i*i<=n;++i)
if(n%i==0)
{
if(M_R(i+1))prime.push_back(i+1);
if(1ll*i*i<n&&M_R(n/i+1))prime.push_back(n/i+1);
}
sort(prime.begin(),prime.end());
dfs(0,n,1);
while(!q.empty()){ans[++top]=q.top();q.pop();}
sort(ans+1,ans+K+1);
for(i=1;i<K;++i)print(ans[i]),putchar(' ');print(ans[K]);puts("");
return 0;
}
/*
8 4
15 16 20 24
*/