E. Sum of Remainders
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Calculate the value of the sum: n mod 1 + n mod 2 + n mod 3 + ... + n mod m. As the result can be very large, you should print the value modulo 109 + 7 (the remainder when divided by 109 + 7).
The modulo operator a mod b stands for the remainder after dividing a by b. For example 10 mod 3 = 1.
Input
The only line contains two integers n, m (1 ≤ n, m ≤ 1013) — the parameters of the sum.
Output
Print integer s — the value of the required sum modulo 109 + 7.
Examples
input
3 4
output
4
input
4 4
output
1
input
1 1
output
0
ans=n*m-([n/1]*1+[n/2]*2+...+[n/m]*m)
发现很多的n/i的值是相等的,所以可以根据这个规律,整合求
n/i等于x的范围是[ n/(x+1)+1 , n/x ]
#include<bits/stdc++.h>
using namespace std;
const long long mo=1e9+7;
long long summ(long long a,long long b)
{
if(a&1)
{
b/=2;
}
else
{
a/=2;
}
return ((a%mo)*(b%mo))%mo;
}
long long mu(long long a,long long b)
{
return ((a%mo)*(b%mo))%mo;
}
int main()
{
long long n,m;
cin>>n>>m;
long long ans,a,b,need=m+1;
ans=(mu(n,m))%mo;
for(long long i=1;i*i<=n;i++)
{
a=n/i;
b=n/(i+1)+1;
if(a>m)
a=m;
if(a>=b)
{
need=min(need,b);
ans=(ans-mu(summ(a+b,a-b+1),i)+mo)%mo;
}
}
for(long long i=1;i<need;i++)
{
ans=(ans-mu(n/i,i)+mo)%mo;
}
cout<<ans<<endl;
return 0;
}