Sum of Remainders

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
*/
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值