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 1e9 + 7 (the remainder when divided by 1e9 + 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 ≤ 10^13) — the parameters of the sum.
Output
Print integer s — the value of the required sum modulo 1e9 + 7.
Examples
Input1
3 4
Output1
4
Input2
4 4
Output2
1
Input3
1 1
Output3
0
题目大意
给出n和m两个正整数(1<=n,m<=10^13),求:
ans=n%1+n%2+n%3+…….n%m
思路
由于n,m都是十分大的整数,则从1枚举到m,再一直 ans+=n%i的算法很明显过不了(TLE)。
由于涉及到mod运算且范围如此的大,我们可以尝试着去找规律。
上图中,n分别%每列数的余数在每列中都构成了一列等差数列。
这是为什么呢?我们设i为我们一直在枚举的数。
则有: n=k*i+p; p=n%i;
而所有的p之和即为我们要求出的东西。
当p>=k时,则有n=(i+1)*k+p-k;
此时,我们设 i2=(i+1),p2=(p-k);
可以发现,若p>=k,i的值就可以一直递下去。
k不变时,p的值就构成一个等差数列。
ans每次就加等于Sump,而i每次便可以加等于数列长度。
代码
#include<cstdio>
#include<algorithm>
using namespace std;
#define ll long long
const ll MOD=(1e9+7);
ll n,m,ans,i=1;
int main(){
scanf("%lld%lld",&n,&m);
if(m>n)ans=((m-n)%MOD*(n%MOD))%MOD,m=n;
while(i<m){
ll k=n/i,p=n%i,lp=p%k;
ll d=(p-lp+k)/k,x=(p+lp);
i+=d;
if(d%2==0)d/=2;
else if(x%2==0)x/=2;
ans=(ans+((x%MOD)*(d%MOD)%MOD))%MOD;
}
if(i>m+1){//多算了(m+1)至i
i--;
ll k=n/i,p=n%(m+1),lp=n%i;
ll d=(p-lp+k)/k,x=(p+lp);
if(d%2==0)d/=2;
else if(x%2==0)x/=2;
ans=(ans-((x%MOD)*(d%MOD))%MOD+MOD)%MOD;
}
if(i==m)//少算了m
ans=(ans+(n%m)%MOD)%MOD;
printf("%lld\n",ans);
}
/*
56598 56
*/