设为首页 收藏本站
查看: 914|回复: 0

[经验分享] hdu3187 HP Problem

[复制链接]

尚未签到

发表于 2015-10-6 11:01:02 | 显示全部楼层 |阅读模式
Problem Description

In mathematics, the greatest common divisor (gcd), of two non-zero integers, is the largest positive integer that divides both two numbers without a remainder. For example, gcd( 10, 15 ) = 5, gcd( 5, 4 ) = 1. If gcd( k, n ) == 1 , then we say k is co-prime to n ( also , n is co-prime to k ), the totient function H(n) of a positive integer n is defined to be the number of positive integers not greater than n that are co-prime to n. In particular H(1) = 1 since 1 is co-prime to itself (1 being the only natural number with this property). For example, H (9) = 6 since the six numbers 1, 2, 4, 5, 7 and 8 are co-prime to 9. Also, we define the number of different prime of n is P (n). For example, P (4) = 1 (4 = 2*2), P (10) = 2(10 = 2*5), P (60) = 3(2*2*3*5). Now, your task is, give you a positive integer n not greater than 2^31-1, please calculate the number of k (0 < k < 2^31) satisfied that H (k) = n and P (k) <= 3(So we called HP Problem).



Input

Each line will contain only one integer n. Process to end of file.



Output

For each case, output the answer in one line.



Sample Input



6



Sample Output



4

  思路:给定n为在[1,2^31-1]范围内有多少个数满足 H(k)=n&& P(k)<=3;
  H(k)为k的欧拉函数;P(K)为k中所含质因子的种类个数;
  欧拉公式:如果k=p1^q1 *P2^q2*....*pm^qm;
  则H(k)=k*(p1-1)*(p2-1)...(pm-1)/(p1*p2*p3*p4...pm)
  如果已知H(k)以及k中质因子(p1, p2, p3, p4,..pm)则可以唯一确定一个k=H(k)*(p1*p2*...*pm)/((p1-1)*(p2-1)...*(pm-1))
  于是对于n,如果找到了质因子集合{p1,p2,...,pm}( m<=3)则等于找到了一个k;
  同时这样的质因子集合满足
  n%(pi-1)==0&&n 不含集合外的其他质因子;同时求得的K应小于2^31;
  由于m<=3可以枚举具有上述性质的质因子集合来得到答案;
  n为1时,有2个,H(1)=1这种特殊情况要考虑。
  (真心看不懂啊……)


DSC0000.gif DSC0001.gif View Code


#include<stdio.h>
#include<queue>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<memory>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<malloc.h>
#include<iostream>
using namespace std;
typedef __int64 ll;
#define N 100000
#define M_F 1000
const ll base=2147483648;
int tag[N];
ll prime[N/10],cnt;
ll p[M_F],q[M_F],num;
ll a[M_F],m;
ll ans,n,pr[10],t;
void get_prime()
{
int i,j;
memset(tag,0,sizeof(tag));
for(i=2;i*i<N;i++)
if(!tag)
{
for(j=2;j*i<N;j++)
tag[j*i]=1;
}
for(i=2,cnt=0;i<N;i++)
if(tag==0)prime[cnt++]=i;
}
void factor()
{
int i;
ll tmp=n;
for(num=0,i=0;i<cnt&&prime*prime<=n;i++)
if(n%prime==0)
{
p[num]=prime;
q[num]=0;
while(n%prime==0)
{
q[num]++;
n/=prime;
}
num++;
}
if(n>1)
{
p[num]=n;
q[num++]=1;
}
n=tmp;
}
int Is_Ok(ll cur)
{
int i;
cur++;
for(i=0;i<cnt&&prime*prime<=cur;i++)
if(cur%prime==0)return 0;
return 1;
}
void dfs(ll cur,int idx)
{
ll tmp=1;
if(idx>=num)
{
if(Is_Ok(cur))a[m++]=cur;
return ;
}
for(int i=0;i<=q[idx];i++)
{
dfs(cur*tmp,idx+1);
tmp=tmp*p[idx];
}
}
int judge()
{
ll ret,tmp;
ret=n;
int i;
for(i=0;i<t;i++)
{
if(ret%(pr-1))return 0;
else ret/=(pr-1);
}
for(i=0;i<t;i++)
{
ret=ret*pr;
if(ret>=base||ret<0)return 0;
}
//tmp=ret;
ret=n;
for(i=0;i<t;i++)
{
ret/=(pr-1);
}
for(i=0;i<t;i++)
while(ret%pr==0)ret/=pr;
//if(ret==1)cout<<tmp<<endl;
return (ret==1);
}
void dfs1(int idx,int k)
{
if(k>3)return ;
else
{
if(k)
{
t=k;
ans+=judge();
}
int i;
for(i=idx;i<m;i++)
{
pr[k]=a+1;
dfs1(i+1,k+1);
}
}
}
int main()
{
get_prime();
while(scanf("%I64d",&n)!=EOF)
{
factor();
m=0;
dfs(1,0);
ans=0;
dfs1(0,0);
if(n==1)ans++;
printf("%I64d\n",ans);
}
return 0;
}
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-123281-1-1.html 上篇帖子: 重点推荐:HP大中华区总裁孙振耀退休感言 下篇帖子: [急聘]HP正式及外包职位
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表