zoj 3629 Treasure Hunt IV 打表找规律

H - Treasure Hunt IV

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

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 b in 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

题意:给你一个Long long范围的区间,然后问里面有多少个数,满足n/1+n/2+n/k……加起来等于一个偶数
题解:首先打表,然后我们发现这种数是扎堆存在的,[0,1),[4,9),[16,25),……
然后我们就这样子找规律,然后一个等差数列求和就好啦!

LL solve(LL n)

{

    LL m = (LL)sqrt(n*1.0);

    LL sum=0;

    if(m%2==0) sum = n-m*m;

    if(m%2==1) m++;

    LL j=m/2;

    sum=sum-j+2*j*j;

   // sum=sum+2*j*j-j;

    return sum;

}

int main()

{

    LL n,m;

    while(scanf("%llu%llu",&n,&m)>0)

    {

        n++,m++;

        LL ans=solve(n-1);

        LL cur =solve(m);

        printf("%llu\n",cur-ans);

    }

    return 0;

}

 

你可能感兴趣的:(ZOJ)