Remainder Reminder

Problem Statement

Takahashi had a pair of two positive integers not exceeding N(a,b), which he has forgotten. He remembers that the remainder of a divided by b was greater than or equal to K. Find the number of possible pairs that he may have had.

Constraints

  • 1N105
  • 0KN1
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the number of possible pairs that he may have had.


Sample Input 1

Copy
5 2

Sample Output 1

Copy
7

There are seven possible pairs: (2,3),(5,3),(2,4),(3,4),(2,5),(3,5) and (4,5).


Sample Input 2

Copy
10 0

Sample Output 2

Copy
100

Sample Input 3

Copy
31415 9265

Sample Output 3

Copy
287927211

一开始看到这个题目,想到的方法是暴力搜索,于是运行了一下,果不其然超时了,后来就一直在想着如何把公式推导出来,耽误了蛮长的时间,于是去问了一下别人,在别人的指点下逐渐豁然开朗,非常感谢!!!

代码:

if (k==0)
        ans=n*n;//排除0的干扰
    else
    {
        for (int i=1;i<=n-k;i++)
            //只有比k大的数做除数(a/b中的b)才能得到大于等于k的余数
        {
            b=i+k;
            //b为比k大,小于等于n的所有数
            ans+=(n/b)*i;
            //对于任意b,在0到n的范围内任意a,a%b的余数有(n/b)个完整的循环,每个循环内有i个大于等于k的余数
            if(n%b-k>=0)
                ans+=n%b-k+1;
            //如果还存在不完整的循环,计算在这个不完整循环内a%b的大于等于k的余数个数
        }
    }

猜你喜欢

转载自blog.csdn.net/tong_zhi/article/details/79533298