Treasure Hunt IV 找规律题

Description

Alice is exploring the wonderland, suddenly she fell into a hole, when she woke up, she found there are b - a + 1 treasures labled a from bin front of her.
Alice was very excited but unfortunately not all of the treasures are real, some are fake.
Now we know a treasure labled n is real if and only if [n/1] + [n/2] + ... + [n/k] + ... is even.
Now given 2 integers a and b, your job is to calculate how many real treasures are there.

Input

The input contains multiple cases, each case contains two integers a and b (0 <= a <= b <= 263-1) seperated by a single space. Proceed to the end of file.

Output

Output the total number of real treasure.

Sample Input

0 2
0 10

Sample Output

1
6

***********************************************************************************************************************************************************
通过计算我们会发现,只有在区间[0,1)、[4,9)、[16,25)……实属满足题意
**********************************************************************************************************************************************************
 1 #include<iostream>
 2 #include<string>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<queue>
 6 #include<cmath>
 7 using namespace std;
 8 typedef long long ll;
 9 const double eps=1e-6;
10 ll solve(ll n)
11 {
12      if (n == -1) return 0;
13     ll r = (ll)sqrt((double) n + 1 + eps);
14     if (r & 1){
15         r = (r + 1) >> 1;
16         return ((r << 1) - 1) * r;
17     }
18     else{
19         return n - r * r + (r >> 1) * (r - 1) + 1;//减去两区间之间的无用数
20     }
21 }
22 int main()
23 {
24     ll a,b;
25     while(~scanf("%lld %lld",&a,&b))
26     {
27         ll ans=solve(b)-solve(a-1);
28         printf("%lld\n",ans);
29     }
30     return 0;
31 }
View Code

转载于:https://www.cnblogs.com/sdau--codeants/p/3440129.html

猜你喜欢

转载自blog.csdn.net/weixin_33835690/article/details/93432858