Number HDU

来源:互联网 发布:产品质量数据分析报告 编辑:程序博客网 时间:2024/06/09 16:57
/*(转)http://blog.sina.com.cn/s/blog_691ce2b701019vyl.html题意:如果A不是B的因子,且A B不互质 则称A是B的特殊数 定义一函数f(x) 为x的特殊数的数量若f(x)为奇数 则 x为real number 给x 和y 求x和y间的real number。思路:只要算出[sqrt(x),sqrt(y)]间的偶数n1和奇数的个数n2,和[x,y]间偶数的个数n3则ans = n3 - n1 + n2;证明:任一个数n 有n = phi(n)+g(n)+f(n)例如:n = 12则phi(n) = 4 ,(这些数是:1 5 7 11);g(x) = 5 ,(2 3 4 6 12);f(x) = 3 ,(8 9 10);所以 f(n) = n - phi(n) - g(n),又有当n>2时,phi(n)总为偶数所以要判断f(n)是奇是偶 只要判断 n - g(n)就行了有四种情况,n为奇,g(n)为奇or偶,n为偶,g(n)为奇or偶有一规律 n可开方则g(n)为偶(这个总能想明白吧) 然后没了。。。。注:phi(x)是欧拉函数,g(n)是n的因子数的数量,f(n)就是题目说的special number的数量 */#include<stdio.h>#include<stdio.h>#include<math.h>typedef __int64 lld;lld Gao(lld x,lld y,int flag){lld ret=(y-x+1)/2;if(x>y)return 0;if((y-x+1)%2!=0 && (x&1)==flag)ret++;return ret;}int main(){int T;lld x,y;scanf("%d",&T);while(T--){scanf("%I64d%I64d",&x,&y);lld ans=0;if(x<=2)x+=2;lld tx=(lld)ceil(sqrt(x*1.0));lld ty=(lld)sqrt(y*1.0);ans=Gao(tx,ty,1)-Gao(tx,ty,0)+Gao(x,y,0);printf("%I64d\n",ans);}return 0;}

原创粉丝点击