hdu 4279 Number
http://acm.hdu.edu.cn/showproblem.php?pid=4279
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
Hint
For the second case, the real numbers are 6,8,9,10.这题实在是规律难找,参考了几位大神的代码
1 #include <cstdio> 2 #include <cmath> 3 #include <cstring> 4 #include <algorithm> 5 #include <iostream> 6 7 using namespace std; 8 9 long long a, b, t; 10 long long s1, s2; 11 12 int main() 13 { 14 int T; 15 scanf("%d", &T); 16 while(T--) { 17 cin>>a>>b; 18 --a; 19 s1 = a >> 1; 20 s2 = b >> 1; 21 22 t = sqrt(a); 23 s1 -= t >> 1; 24 s1 += t-(t>>1); 25 26 t = sqrt(b); 27 s2 -= t >> 1; 28 s2 += t-(t>>1); 29 30 if (a >= 1) s1 -= 1; 31 if (a >= 2) s1 -= 1; 32 if (b >= 1) s2 -= 1; 33 if (b >= 2) s2 -= 1; 34 s2 -= s1; 35 cout<<s2<<endl; 36 } 37 return 0; 38 }
代码(2):参考自http://blog.csdn.net/kk303/article/details/7960986
1 #include<iostream> 2 #include<cmath> 3 using namespace std; 4 int t,n,s[20]={0,0,0,0,0,0,1,1,2,3,4,4,5}; 5 __int64 a,b,p1,p2; 6 __int64 getsum(__int64 x) 7 { 8 if (x<=12) return s[x]; 9 __int64 m,k; 10 m=x/2; 11 k=(__int64)sqrt(x); 12 if (k%2==0) m-=2; 13 else m-=1; 14 return m; 15 } 16 int main() 17 { 18 // freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); 19 scanf("%d",&t); 20 while (t--) 21 { 22 scanf("%I64d%I64d",&a,&b); 23 a--; 24 p1=getsum(a); 25 p2=getsum(b); 26 printf("%I64d\n",p2-p1); 27 } 28 return 0; 29 }