原题:http://acm.hdu.edu.cn/showproblem.php?pid=4279
Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2819 Accepted Submission(s): 774
Problem Description
Here are two numbers A and B (0 < A <= B). If B cannot be divisible by A, and A and B are not co-prime numbers, we define A as a special number of B.
For each x, f(x) equals to the amount of x’s special numbers.
For example, f(6)=1, because 6 only have one special number which is 4. And f(12)=3, its special numbers are 8,9,10.
When f(x) is odd, we consider x as a real number.
Now given 2 integers x and y, your job is to calculate how many real numbers are between them.
For each x, f(x) equals to the amount of x’s special numbers.
For example, f(6)=1, because 6 only have one special number which is 4. And f(12)=3, its special numbers are 8,9,10.
When f(x) is odd, we consider x as a real number.
Now given 2 integers x and y, your job is to calculate how many real numbers are between them.
Input
In the first line there is an integer T (T <= 2000), indicates the number of test cases. Then T line follows, each line contains two integers x and y (1 <= x <= y <= 2^63-1) separated by a single space.
Output
Output the total number of real numbers.
Sample Input
2 1 1 1 10
Sample Output
0 4
打表:
#include <stdio.h>
int gcd(int a,int b)
{
if(b==0)
return a;
return gcd(b,a%b);
}
int main()//打表找规律
{
y=1000;
for(i=1;i<=y;i++)
{
num=0;
for(j=1;j<i-1;j++)
{
m=gcd(i,j);
if(m!=j&&m>1)
num++;
}
if(num%2)
printf("%d\n",i);
}
return 0;
}
AC代码:
#include <stdio.h>
#include <math.h>
#define IN __int64
/*
author:YangSir
time:2014/5/8
*/
IN f(IN a)
{
IN m,b;
m=(a-4)/2;//除去2,4的偶数个数
b=sqrt(a);
if(b%2)//比较偶数开方后的偶数和奇数平方后的奇数个数是否相等
return m+1;//不等
else
return m;//相等
}
int main()
{
IN x,y;
int t;
scanf("%d",&t);
while(t--)
{
scanf("%I64d%I64d",&x,&y);
if(y<6)
{
printf("0\n");
continue;//因为y比x大,所以y没有x也就没有
}
if(x<6)
{
printf("%I64d\n",f(y));
}
else
{
printf("%I64d\n",f(y)-f(x-1));//因为是闭区间,取得到x
}
}
return 0;
}