zoukankan      html  css  js  c++  java
  • 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
    ",ans);
    29     }
    30     return 0;
    31 }
    View Code
  • 相关阅读:
    Bugku web web基础$_GET
    Bugku web 计算器
    Bugku web web2
    Bugku 杂项 宽带信息泄露
    Bugku 杂项 猜
    Bugku 杂项 又一张图片,还单纯吗
    Bugku 杂项 啊哒
    Bugku 杂项 眼见非实(ISCCCTF)
    Bugku 杂项 telnet
    Bugku 杂项 隐写
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3440129.html
Copyright © 2011-2022 走看看