hdu 2204 Eddy's爱好 (容斥原理)

来源:互联网 发布:java 字符串pack方法 编辑:程序博客网 时间:2024/06/07 22:44

这题看起来简单,做的时候才发现不是那么简单的。。。

M^K 在K为合数的时候都可以表示为X^Y (Y为质数),但X^Y这个数可能出现多次,例如8^2= 4^3

这是因为该数为2^6, 6等于质数2乘质数3  所以我们在计数时 ans= 指数为一个质数 - 指数为两个质数 + 指数为三个质数;

因为N最大为10^18  < 2^60 ,所以指数为 三个质数的数为:2^(2*3*5)= 2^30 , 3^(2*3*5)= 3^30, 2^(2*3*7)= 2^42

代码:

#include <stdio.h>#include <string.h>#include <math.h>#define LL __int64int main(){int a[]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59};// 一个质因子的指数 int b[]={6,10,14,15,21,22,26,33,34,35,38,39,46,51,55,57,58};//两个质因子的指数 LL n;while(scanf("%I64d",&n)!=EOF){int ans=  0;for(int i= 0; i<= 16; i++)  //指数为一个质因子 {int num= pow(1.0*n, 1.0/a[i]);while(pow(num+1.0, 1.0*a[i])<= n)num++;num--;if(num> 0)ans+= num;}for(int i= 0; i<= 16; i++)  //指数为两个质因子 {int num= pow(1.0*n, 1.0/b[i]);while(pow(num+1.0, 1.0*b[i])<= n)num++;num--;if(num> 0)ans-= num;}ans++;if(n>= pow(2.0, 30.0)) ans++;//指数为三个质因子的只有三个数 2^30,3^30,2^42 if(n>= pow(3.0, 30.0)) ans++;if(n>= pow(2.0, 42.0)) ans++;printf("%d\n",ans);}return 0;}


 

0 0
原创粉丝点击