HDU 3187 HP Problem(欧拉函数)
来源:互联网 发布:淘宝医保卡套现方法 编辑:程序博客网 时间:2024/06/06 00:47
之前对欧拉函数的理解太过浅显,这次学习到它的另一种写法:
phi(k) = k(1-1/p1)(1-1/p2)(1-1/p3)
= (p1-1)p1^x*(p2-1)p2^y*(p3-1)p3^z
这题要求phi(k)=n的k,可以枚举n的所有因子i,在判断i+1是否为素数,是就加入集合。
然后对集合进行dfs,保证每次不超过3个素数.(代码参考)
拓展(下面与此题无关):
这里学习了一种dfs构造找出所有满足phi(n)的k的方法。
枚举n的所有因子p,判断p+1是否为素数,如果是,则说明p可能是构成k的素因数。
加入集合。枚举完之后dfs该集合.
假设对当前dfs选出来的数p1,p2,p3...pi
用n去除(pi+1),如果不满足整除则跳出。
再用pi不断去整除n,xi保存pi整除n的次数
当尝试结束时n=1,则说明当前除去的p1,p2,pi所构成的k的满足phi(k)=n
k=p1^(x1+1) * p2^(x2+1) * p3^(x3+1)....
#include <cstdio>#include <vector>#include <cstdlib>#include <iostream>#include <cstring>using namespace std;#define S 5typedef long long LL;LL modular_multi(LL x,LL y,LL mo){LL t;x%=mo;for(t=0;y;x=(x<<1)%mo,y>>=1)if (y&1)t=(t+x)%mo;return t;} LL modular_exp(LL num,LL t,LL mo){LL ret=1,temp=num%mo;for(;t;t>>=1,temp=modular_multi(temp,temp,mo))if (t&1)ret=modular_multi(ret,temp,mo);return ret;} bool miller_rabin(LL n){if (n==2)return true;if (n<2||!(n&1))return false;int t=0;LL a,x,y,u=n-1;while((u&1)==0) t++,u>>=1;for(int i=0;i<S;i++){a=rand()%(n-1)+1;x=modular_exp(a,u,n);for(int j=0;j<t;j++){y=modular_multi(x,x,n);if (y==1&&x!=1&&x!=n-1)return false;x=y;}if (x!=1)return false;}return true;}vector<int>prim;int ans;LL power(LL a,LL b){ LL ret=1; while(b) { if(b&1) ret=ret*a; a=a*a; b>>=1; } return ret;}void dfs(int cur,vector<int>with,int n){ int nn=n; bool can=1; int num[50]; for(int i=0;i<with.size();i++) if(n%(with[i]-1)==0) n/=(with[i]-1); else { can=0; break; } if(!can) return ; memset(num,0,sizeof(num)); for(int i=0;i<with.size();i++) while(n%with[i]==0) n/=with[i],num[i]++; if(n==1) { LL k=1; for(int i=0;i<with.size();i++) k*=power(with[i],num[i]+1);///k*=with[i]^(num[i]+1) printf("k==%I64d\n",k); ans++; } for(int i=cur;i<prim.size();i++) { with.push_back(prim[i]); dfs(i+1,with,nn); with.pop_back(); }}int main(){ int n; while(~scanf("%d",&n)) { prim.clear(); for(int i=1;i*i<=n;i++) if(n%i==0){ if(miller_rabin(i+1)) prim.push_back(i+1); if(i*i!=n&&miller_rabin(n/i+1)) prim.push_back(n/i+1); } ans=0; dfs(0,vector<int>(),n); printf("%d\n",ans); } return 0;}
- HDU 3187 HP Problem(欧拉函数)
- 【HDU】5152 A Strange Problem 【线段树+欧拉函数】
- hdu 3187 (欧拉函数+dfs)
- POJ2480Longge's problem 欧拉函数
- POJ2480_Longge's problem(欧拉函数)
- hdu-1286欧拉函数
- hdu 2588 欧拉函数
- hdu 2824 欧拉函数
- hdu 1787 欧拉函数
- 欧拉函数 hdu 2824
- hdu~3501~欧拉函数
- HDU 1787 欧拉函数
- hdu 1286 欧拉函数
- HDU 4002 欧拉函数
- hdu 4983 欧拉函数
- hdu 4893 欧拉函数
- HDU 1286 欧拉函数。
- HDU 1286 欧拉函数
- 向Windows窗口发送Alt组合键的问题
- Sprite Kit编程指南(9)-Sprite Kit最佳实践
- Android应用嵌入广告相关经验收集整理
- 20130822C语言编程实践课第四天
- C++字符串输入流的问题
- HDU 3187 HP Problem(欧拉函数)
- 恢复Cygwin快捷方式
- Linux命令总结
- 编译报错
- softIRQ tasklet work_queue
- USACO 3.2.1 Factorials
- 从零开始学GO语言(1)——hellow world
- BOOST 宏定义标记
- 简单01背包裸题——饭卡