您的位置:首页 > 大数据 > 人工智能

Codeforces 616E Sum of Remainders 【数学分块】

2016-04-05 16:58 531 查看
题目链接:Codeforces 616E Sum of Remainders

E. Sum of Remainders

time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard 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

题意:求解∑mi=1nmodi。

思路:我们化简nmodi=n−n/i∗i。

这样原式 =n∗m−∑mi=1(n/i∗i)

首先m<=n√时,直接暴力,对于剩下的,我们可以分块来搞,总会存在一段连续的值使得n/i为定值,那么我们找到这个区间就可以了。不过貌似在除以2的时候,会出问题,我直接上逆元就过了。

AC代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#define INF 0x7fffffff
using namespace std;
typedef long long LL;
const int MAXN = 1e6 + 10;
const int MOD = 1e9 + 7;
void add(LL &x, LL y) {x += y; x %= MOD;}
LL pow_mod(LL a, LL n) {
LL ans = 1LL;
while(n) {
if(n & 1)
ans = ans * a % MOD;
a = a * a % MOD;
n >>= 1;
}
return ans;
}
int main()
{
LL n, m; scanf("%lld%lld", &n, &m);
//    LL s = 0;
//    for(LL i = 1; i <= m; i++) {
//        add(s, n % i % MOD);
//    }
//    cout << s << endl;
LL ans = n % MOD * (m % MOD) % MOD;
LL nn = sqrt(n);
LL ans1 = 0;
for(LL i = 1; i <= min(m, nn); i++) {
add(ans1, n / i % MOD * i % MOD);
}
//cout << ans1 << endl;
if(m > nn) {
if(nn * nn == n) nn--;
for(LL i = 1; i <= nn; i++) {
LL r = min(m, n / i); LL l = n / (i + 1) + 1;
if(l > r || r <= nn) continue;
//cout << l << ' ' << r << endl;
add(ans1, (l + r) % MOD * ((r - l + 1) % MOD) % MOD * pow_mod(2, MOD-2) % MOD * i % MOD);
}
}
printf("%lld\n", ((ans - ans1) % MOD + MOD) % MOD);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: