hdu 2620 Ice Rain(数论)

Ice Rain

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 181    Accepted Submission(s): 76


Problem Description
Ice Rain------I was waiting for a girl, or waiting for been addicted to the bitter sea. Love for irrigation in silence. No one considered whether the flowers came out or wither. Love which I am not sure swing left and right. I had no choice but to put my sadness into my heart deeply.

   Yifenfei was waiting for a girl come out, but never. 
His love is caught by Lemon Demon. So yifenfei ’s heart is “Da Xue Fen Fei” like his name.
The weather is cold. Ice as quickly as rain dropped. Lemon said to yifenfei, if he can solve his problem which is to calculate the value of   , he will release his love.
Unluckily, yifenfei was bored with Number Theory problem, now you, with intelligent, please help him to find yifenfei’s First Love.
 

Input
Given two integers n, k(1 <= n, k <= 10 9).
 

Output
For each n and k, print Ice(n, k) in a single line.
 

Sample Input
 
   
5 4 5 3
 

Sample Output
 
   
5 7
题意:给你n和k,求

思路:kmodi=k-i*[k/i] ,所以=nk-(1*[k/1]+2*[k/2]+...+n*[k/n])

那么问题就在于怎么求解(1*[k/1]+2*[k/2]+...+n*[k/n]),直接枚举肯定会炸掉

我们知道[k/i]到[k/n]肯定是分成很多段值相等的区间的。(k固定,n越小k/n越大,但是中间会有几个数k/n值是相同的,比如4/3=1 4/4=1)

对于某些区间来说,数量是很庞大的,所以如果我们可以已知这个区间的第一个编号,求得区间最后一个编号,那么将大大加快枚举时间。

设d=k/i  j=k/d则j就是我们要求的最后一个编号,每次到i后直接跳到j+1即可。

证明:

已知d=k/i,我们要求一个j使得[i,j]之间的数l的[k/l]=d

那么令j=k/d,[i,j]内的数满足性质是显而易见的。但是j是不是区间的最后一个数呢?

我们假设k/(j+1)=d,那么有d(j+1)<=k  而根据k/d=j可以推出k-(d*j)

这就冲突了吧。 

代码:

#include 
#include 
#include 
#include 
#include 
using namespace std;
#define LL long long
int main()
{
    LL n,k;
    while(~scanf("%lld %lld",&n,&k))
    {
        LL ans=n*k;
        if(n>k) n=k;
        for(LL i=1;i<=n;)
        {
            LL d=k/i;
            LL j=k/d;
            if(j>n) j=n;
            ans=ans-d*((i+j)*(j-i+1)/2);
            i=j+1;
        }
        printf("%lld\n",ans);
    }
    return 0;
}


你可能感兴趣的:(数学-数论)