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.
InputGiven two integers n, k(1 <= n, k <= 10
9).OutputFor each n and k, print Ice(n, k) in a single line.Sample Input
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
Unluckily, yifenfei was bored with Number Theory problem, now you, with intelligent, please help him to find yifenfei’s First Love.
5 4 5 3Sample Output
5 7
题意:求和k%i (1<=i<=n)
对于每一个i ,k%i = k- (k/i)*i;
则有: sum = [k-(k/1)*1]+[k-(k/2)*2]+······[k-(k/n)*n] = n*k - [(k/1)*1+(k/2)*2+······+(k/n)*n];
n*K 可直接求得
对于 [(k/1)*1+(k/2)*2+······+(k/n)*n],因为 k / i 是分段的,且,每一段都是规则的等差数列,我们可以分段累加。
#include<bits/stdc++.h>
#define LL long long
using namespace std;
int main(){
LL n,k;
while(scanf("%lld%lld",&n,&k)!=EOF){
LL ans = n*k;
if(n > k) n = k;
for(int i=1;i<=n;){
LL d = k/i;
LL j = k/d;
if(j > n) j = n;
LL temp = (i+j)*(j-i+1)/2*d;
ans -= temp;
i = j+1;
}
cout<<ans<<endl;
}
return 0;
}