Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 867 Accepted Submission(s): 280
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 4HintFor the second case, the real numbers are 6,8,9,10.
Source
Recommend
liuyiding
题解:
规律,自己在纸上写写,就能找到规律 ,
很容易发现 所谓的 real number 就是大于4的偶数 且 不是 偶数的 平方的 数,或者是奇数的平方。
所以如果 k*k<=n<(k+1)*(k+1).假如k是偶数,那么就是 (n-4)/2 ,因为偶数的平方和奇数的平方个数相等。
假如k是奇数,那么就是 (n-4)/2+1了,因为奇数的平方多一个。
其余就简单了。注意数据范围,用 long long
1 #include<cstdio>
2 #include<cstring>
3 #include<cmath>
4 #include<iostream>
5 #include<algorithm>
6 #include<set>
7 #include<map>
8 #include<queue>
9 #include<vector>
10 #include<string>
11 #define Min(a,b) a<b?a:b
12 #define Max(a,b) a>b?a:b
13 #define CL(a,num) memset(a,num,sizeof(a));
14 #define eps 1e-12
15 #define inf 100000000
16 #define mx 10
17
18 const double pi = acos(-1.0);
19 const int maxn = 105;
20 typedef __int64 ll;
21 using namespace std;
22 ll cal(ll n)
23 {
24 if(n <= 4) return 0;
25 ll tp = sqrt(n);
26 if(tp&1)
27 {
28 return (n - 4)/2 + 1;
29
30 }
31 else return (n - 4)/2 ;
32 }
33 int main()
34 {
35 int n;
36 int t;
37 ll a,b;
38 scanf("%d",&t);
39 while(t--)
40 {
41 scanf("%I64d%I64d",&a,&b);
42
43 printf("%I64d\n",cal(b) - cal(a - 1));
44 }
45 }
2 #include<cstring>
3 #include<cmath>
4 #include<iostream>
5 #include<algorithm>
6 #include<set>
7 #include<map>
8 #include<queue>
9 #include<vector>
10 #include<string>
11 #define Min(a,b) a<b?a:b
12 #define Max(a,b) a>b?a:b
13 #define CL(a,num) memset(a,num,sizeof(a));
14 #define eps 1e-12
15 #define inf 100000000
16 #define mx 10
17
18 const double pi = acos(-1.0);
19 const int maxn = 105;
20 typedef __int64 ll;
21 using namespace std;
22 ll cal(ll n)
23 {
24 if(n <= 4) return 0;
25 ll tp = sqrt(n);
26 if(tp&1)
27 {
28 return (n - 4)/2 + 1;
29
30 }
31 else return (n - 4)/2 ;
32 }
33 int main()
34 {
35 int n;
36 int t;
37 ll a,b;
38 scanf("%d",&t);
39 while(t--)
40 {
41 scanf("%I64d%I64d",&a,&b);
42
43 printf("%I64d\n",cal(b) - cal(a - 1));
44 }
45 }