Codeforces - Educational Codeforces Round 5 - E. Sum of Remainder
题目链接:http://codeforces.com/contest/616/problem/E
题目大意:给定整数n,m(1≤n,m≤1013), 求(n mod 1 + n mod 2 + ... + n mod m)的值(mod Pt = 1e9 + 7)。
思路:这题一看是看觉得题意简洁,通过人数不多一定是一道用到各种定理的碉堡数论题。后来仔细想了一下发现是乱搞…
首先通过观察数据范围,结合数论题的复杂度传统考虑O(√n)算法。
把n拆解,可以任意写成很多种n = px + r 的形式,而p, x中必有一个≤√n,首先对于[1,√n]的x暴力处理n % x,而当x > √n的时候,就出现了一个[1, √n]的p,对应一段连续的x的情况,同样r也是对应的。如果能够快速批处理出这些r的和,那么就能通过1 - √n枚举p来搞定x∈[√n, +∞]的情况。通过观察,对于任何一个p对应的连续的x区间,r都成首项为n - (n / p) * p, 公差为p,项数为n / p - n / (p + 1)的等差数列前n项和,因此带入公式可O(1)得解。 先枚举p去做代码比较好些,直到n / p < √n,把成块的x处理结束,剩下谁就直接从1到那个值暴力枚举计算比较容易写。
当m > n的时候把m变成n,答案初始为n * (m - n) 即可。值得注意的是m < n的时候,要注意从p等于多少开始能覆盖到m,那个边界的x区间中,r的首项为n % m。
题目要求% Pt, 这一点卡的很严,注意n % m之后还要再 % Pt,而且中间计算的过程中每一步运算都要%,防止爆long long。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL; const LL Pt = 1e9 + ;
LL n, m, d1, d2, a, k, ans, pos, ans1; int main()
{
scanf("%I64d%I64d", &n, &m);
if (m > n) ans = ((n % Pt) * ((m - n) % Pt)) % Pt, m = n;
bool flag = false;
for (LL i = ; i <= n; i++)
{
d1 = n / i; d2 = n / (i - );
if (d1 < m && !flag)
{
flag = true;
a = (n % m) % Pt;
k = (m - d1) % Pt;
a = ((a * k) % Pt + (((k * (k - ) / ) % Pt) * (i - )) % Pt) % Pt;
ans = (ans + a) % Pt;
continue;
}
if (flag)
{
a = (n - d2 * (i - )) % Pt;
k = (d2 - d1) % Pt;
a = ((a * k) % Pt + (((k * (k - ) / ) % Pt) * (i - )) % Pt) % Pt;
ans = (ans + a) % Pt;
}
if (d1 <= sqrt(n)) {pos = d1; break;}
}
for (LL i = ; i <= min(pos, m); i++) ans = (ans + n % i) % Pt;
printf("%I64d\n", ans);
}
Codeforces - Educational Codeforces Round 5 - E. Sum of Remainder的更多相关文章
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...
- Codeforces Educational Codeforces Round 5 E. Sum of Remainders 数学
E. Sum of Remainders 题目连接: http://www.codeforces.com/contest/616/problem/E Description The only line ...
- Codeforces Educational Codeforces Round 15 E - Analysis of Pathes in Functional Graph
E. Analysis of Pathes in Functional Graph time limit per test 2 seconds memory limit per test 512 me ...
- Codeforces Educational Codeforces Round 3 E. Minimum spanning tree for each edge LCA链上最大值
E. Minimum spanning tree for each edge 题目连接: http://www.codeforces.com/contest/609/problem/E Descrip ...
- Codeforces Educational Codeforces Round 3 C. Load Balancing 贪心
C. Load Balancing 题目连接: http://www.codeforces.com/contest/609/problem/C Description In the school co ...
- Codeforces Educational Codeforces Round 3 A. USB Flash Drives 水题
A. USB Flash Drives 题目连接: http://www.codeforces.com/contest/609/problem/A Description Sean is trying ...
- codeforces Educational Codeforces Round 24 (A~F)
题目链接:http://codeforces.com/contest/818 A. Diplomas and Certificates 题解:水题 #include <iostream> ...
- Codeforces Educational Codeforces Round 3 E. Minimum spanning tree for each edge 树上倍增
E. Minimum spanning tree for each edge 题目连接: http://www.codeforces.com/contest/609/problem/E Descrip ...
随机推荐
- linux学习四x系统指令
一.任务调度 任务调度:系统在某个时间执行特定的命令或者程序 如: 1.对于一些需要周期性执行的一些系统指令 2.定期的病毒扫描 3.定期数据库备份等 命令:crontab 设置任务调度文件: / ...
- my28_mysql内存占用过高降低的方法
对mysql做压力测试,测试完之后,mysql的内存一直不下降 $ free -m total used free shared buff/cache available Mem: Swap: # t ...
- js遇到的奇怪问题和坑1
if(window.confirm('你确定要删除此条信息么')){ // window.open('delete.php'); location.href='delete.php'; return ...
- 转 Python 操作 MySQL 数据库
#########http://www.runoob.com/python/python-mysql.html Python 标准数据库接口为 Python DB-API,Python DB-API为 ...
- socket阻塞与非阻塞情况下的recv、send、read、write返回值 以及epool_wait返回值学习
https://www.cnblogs.com/longingforlife/p/3289976.html
- Spark机器学习库(MLlib)官方指南手册中文版
中文https://blog.csdn.net/liulingyuan6/article/details/53582300 https://yq.aliyun.com/articles/608083 ...
- Selenium2(WebDriver)中执行JavaScript代码 (转)
在用selenium编写web页面的自动化测试代码时,可能需要执行一些JavaScript代码,selenium本身就支持执行js,我们在代码中可以使用executeScript.executeAsy ...
- C# list.toArray list与数组的转换
void Start () { List<int> list = new List<int>(); list.Add(); list.Add(); list.Add(); li ...
- [转]jQuery为控件添加水印文字
本文转自:http://www.cnblogs.com/gzh4455/archive/2011/09/29/2195418.html jQuery扩展: jquery.tinywatermark-3 ...
- JS arguments转array
JS arguments转array? Array.prototype.slice.call(arguments)