BZOJ 3643 Phi的反函数
2017-08-08 15:44
295 查看
3643: Phi的反函数
Time Limit: 10 Sec Memory Limit: 64 MB
Sample Input
4
Sample Output
5
设 x = p1^a1 * p2^a2 * …* pm^am (m<=10,pi为素数)
则 n == phi(x) == x * (p1-1)p1 (p2-1)p2 …* (pm-1)*pm
== p1^(a1-1) * (p1-1) * p2^(a2-1) * (p2-1) * …* pm^(am-1) * (pm-1)
可以发现:(p1-1)(p2-1) …*(pm-1) 为n的因数
又因为m不会超过10,因此在[2,sqrt(n)]内搜索pi即可,若存在px>sqrt(n),那么这样的x一定只有一个,且ax==1,需要特殊判断
资料来自:http://blog.csdn.net/cjk_cjk/article/details/45858111
Time Limit: 10 Sec Memory Limit: 64 MB
Sample Input
4
Sample Output
5
#include<iostream> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> using namespace std; #define MAXN 100005 #define LL long long int n,m,prime[MAXN],tot; bool is_prime[MAXN]; LL ans; void Get_Prime(){ memset(is_prime,false,sizeof is_prime ); for(int i=2;i<=50000;i++){ if(!is_prime[i]) prime[++tot]=i; for(int j=1;j<=tot&&i*prime[j]<=50000;j++){ is_prime[i*prime[j]]=true; if(i%prime[j]==0) break; } } return; } bool Judge_Prime(int x){ for(int i=2;i<=sqrt(x);i++) if(x%i==0) return false; return true; } void DFS(int k,int now,LL sum){ if(sum>=ans) return; if(now==1) { ans=sum;return; } if(now>m&&Judge_Prime(now+1)) ans=min(ans,sum*(now+1)); for(int i=k+1;prime[i]-1<=m;i++){ if(prime[i]-1 > now) break; if((now%(prime[i]-1))==0){ int x=now/(prime[i]-1);LL y=sum*prime[i]; DFS(i,x,y); while(x%prime[i]==0){ x/=prime[i];y*=prime[i]; DFS(i,x,y); } } } } int main(){ scanf("%d",&n);m=(int)sqrt(n); Get_Prime(); ans=(LL)1073741824*2;DFS(1,n,1); if(ans<=((1<<31)-1)) printf("%lld\n",ans); else printf("-1\n"); return 0; }
设 x = p1^a1 * p2^a2 * …* pm^am (m<=10,pi为素数)
则 n == phi(x) == x * (p1-1)p1 (p2-1)p2 …* (pm-1)*pm
== p1^(a1-1) * (p1-1) * p2^(a2-1) * (p2-1) * …* pm^(am-1) * (pm-1)
可以发现:(p1-1)(p2-1) …*(pm-1) 为n的因数
又因为m不会超过10,因此在[2,sqrt(n)]内搜索pi即可,若存在px>sqrt(n),那么这样的x一定只有一个,且ax==1,需要特殊判断
资料来自:http://blog.csdn.net/cjk_cjk/article/details/45858111
相关文章推荐
- 【BZOJ 3643】Phi的反函数 数搜索
- BZOJ3643 Phi的反函数(数论+搜索)
- BZOJ 3643: Phi的反函数 搜索
- bzoj 3643:Phi的反函数 (数论+搜索)
- BZOJ 3643 Phi的反函数
- [BZOJ3643]Phi的反函数(数论+dfs)
- 【NOIP 模拟题】【bzoj 3643】Phi的反函数(数论+搜索)
- 【BZOJ-3643】Phi的反函数 数论 + 搜索
- [BZOJ3643]Phi的反函数(数学相关+搜索)
- bzoj3643 Phi的反函数
- BZOJ 3643|Phi的反函数|搜索|线性筛法
- [BZOJ3643] phi的反函数 - 欧拉函数 - dfs
- 【BZOJ3643】phi的反函数,暴搜
- 【BZOJ 3642】Phi的反函数
- BZOJ4584 & 洛谷3643 & UOJ204:[APIO2016]划艇——题解
- bzoj3643 Phi的反函数 搜索
- 2017.10.7 phi的反函数 失败总结
- 【BZOJ 2818】Gcd - 筛法求素数&phi()
- bzoj 4173: 数学 (数论+sqrt求phi)
- BZOJ3643-欧拉函数,搜索