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代码:
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; }
相关文章推荐
- 【转】Adnroid4.0 签名混淆打包(conversion to dalvik format failed with error 1)
- mybaits批量插入
- lightoj 1223 - Testing Mailboxes 区间DP
- Gradle sync failed: Out of memory: PermGen space
- AFNetworking的报错Error Domain=NSURLErrorDomain Code=-1012 解决问题
- 56 RAII实现智能指针
- gdb在运行maintenance info program-spaces命令时coredump
- 移动诊疗和人工智能序言
- intellij创建安卓项目时Crunching cruncher ***.png failed的解决
- [z] 人工智能和图形学、图像处理方面的各种会议的评级
- Light OJ 1252 Maintaining Communities (树形DP)
- Daily Sentence
- VirtualBox installation failure on Windows
- raid0
- 数据一致性(consistency)、服务可用性(availability)、分区容错性(partition-tolerance)
- USACO 1.3-Barn Repair
- How Browsers Work<二>渲染引擎--painting
- 根据慕课网Daisy老师教程写的 爱心鱼 网页小游戏
- UltraISO制作U盘启动盘安装Win7系统攻略
- uva 10245 The Closest Pair Problem