1. Train of Thought: Mathematics + law finding
2 analysis:
1. The data range of this question is very large (1 --- z ^ 63-1), and the given time must be 1 s. Then it is clearly the question of finding a rule. But how can we find this rule? We usually use a table to output the first 100 or about 50 of the data to find the rule.
2 because the number of values in the closed interval [x, y] is required, we think of subtraction. Take [1, y]-[1, x-1] Here why not [1, x], because here is a closed range so here x is to consider, so can not directly subtract.
3. The rule is as follows:
1-> x: ans
1-1:0
1-2: 0
1-3: 0
1-4: 0
1-// before the value of x is less than or equal to 5, all values are 0. 5/2-2 = 0
1-6// x is the sum of squares of a number, and k is an even number. 6/2-2 = 1;
1-7// x is the sum of squares of a number, and k is an even number. 7/2-2 = 1;
1-8:2
1-// x is the sum of squares of a certain number of k, and k is an odd number. Add 1 9/3-2 + 1 = 3;
1-10: 4
1-11: 4
1-12: 5
1-
1-// x is the sum of squares of a certain number of k, and k is an odd number. Add 1 14/2-2 + 1 = 6;
1-
1-// x is the sum of squares of a certain number of k, and k is an even number. 16/2-2 = 6;
1-17: 6
1-18:6
1-19: 7
1-20:8
1-
1-
1-23:9
1-24:10
1-// x is the sum of squares of a certain number of k, and k is an odd number. Add 1 25/2-2 + 1 = 11;
1-26:12
The rule we found is as follows: after analyzing the above data, we know that if the number is initialized to ans = x/2-2, we only need to consider whether the number is an odd or even number after the square is opened, if it is an even number, 1 is not added. If it is an odd number, 1 is added.
4 here, the odd and even numbers can be judged by the "&" operation, as long as x & 1 is done. If x is an even number, then the last digit of x's binary is 0, if it is an odd number, the last digit of the binary number is 1. A division of 2 can be expressed by "> 1", which is much faster.
5. The data volume is large. The data type must be long or _ int64. sometimes, an error occurs when using long. You can replace it with _ int64.
Code:
[Cpp]
# Include <iostream>
# Include <algorithm>
# Include <cstdio>
# Include <cstring>
# Include <cmath>
Using namespace std;
Typedef _ int64 int64;/* type redefinition */
# Define maxn1010
Int t;
Int64 ans;
Int64 Left, Right;
Int64 solve (int64 x ){
Int64 tmp = x;
If (x <6)
Return 0;
Ans = (x> 1)-2;/* initialize to this value */
X = sqrt (tmp);/* obtain the number of square open x */
If (x & 1)/* if it is an odd number, add 1 */
Ans ++;
Return ans;
}
Int main (){
Scanf ("% d", & t );
While (t --){
Scanf ("% I64d % I64d", & Left, & Right );
Printf ("% I64d \ n", solve (Right)-solve (Left-1);/* Here is solve (left-1 )*/
}
Return 0;
}