codeforces 616E 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 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.

Example
Input
3 4
Output
4
Input
4 4
Output
1
Input
1 1
Output
0

题意

给你两个数n,m
问你n % 1 + n % 2 + … + n% m为几

公式化简 n%i=n-n/i*i 那么就要求 n * m - sum(n/i * i) (i从1到m)

由于 当到达i的时候 从i到 n/(n/i) 的 n/i 的值是一样的 因为如果说 n/i 得到了一个因子的下界,那么n/这个因子。则是这个因子的上界。那么就可以那个求和公式求每一段了。

求得过程有些坑点需要注意,首先是注意m的大小,m的大小可能位于n的两个因子之间
然后是,求和公式那里也需要先求下余,再算乘法,这样就需要注意因为有个除2,所以要判断用哪一项能整除2,除完后再求余

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
int main()
{
    long long n,m;
    cin>>n>>m;
    ll all = (n%mod)*(m%mod);
    all%=mod;
    long long res=0;
    ll t=min(n,m);
    for(ll i=1;i<=t;i++)
    {
        ll l=i;
        ll r=min((n/(n/i)),m);
        long long num=r-l+1;
        long long sw=(l+r);
        if(sw%2==0) sw/=2;
        else num/=2;
        num%=mod,sw%=mod;
        ll sum=(num%mod)*(sw%mod);//这里不取模居然不行。。好奇怪
        sum%=mod;
        sum*=(n/i);
        sum%=mod;
        res+=sum;
        res%=mod;
        i=r;
    }
    res%=mod;
   cout<<(all-res+mod)%mod<<endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值