本文共 1565 字,大约阅读时间需要 5 分钟。
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 <= 109).
Output
For each n and k, print Ice(n, k) in a single line.
Sample Input
5 4
5 3
Sample Output
5
7
代码及注释如下:
/*我们知道k=i*x+r;其中x表示商即k/i,r表示余数即k%i;所以k=i*(k/i)+r;所以r=k-i*(k/i);所以就有sumr=nk-[1*(k/1)+2*(k/2)+....+n*(k/n)];而有一些连续区间的k/i一定相同,所以可以进行简化合并...并利用等差数列的公式进行求解令d=k/i; 则连续区间的最后一个数为j=k/d;所以可以结合等差数列求解....*/#include#include #include #include using namespace std;typedef long long ll;ll n,k;int main(){ while (scanf("%lld%lld",&n,&k)!=EOF) { ll sum=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; sum=sum-d*((j-i+1)*(i+j)/2); i=j+1; } printf("%lld\n",sum); } return 0;}
转载地址:http://faaen.baihongyu.com/