题目链接: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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. Codeforces Educational Codeforces Round 3 C. Load Balancing 贪心

    C. Load Balancing 题目连接: http://www.codeforces.com/contest/609/problem/C Description In the school co ...

  7. 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 ...

  8. codeforces Educational Codeforces Round 24 (A~F)

    题目链接:http://codeforces.com/contest/818 A. Diplomas and Certificates 题解:水题 #include <iostream> ...

  9. 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 ...

随机推荐

  1. chafen

    //f(u)>=f(v)+w //求最大值 跑最短路 v->u -w //求最小值 跑最长路 u->v w

  2. cf Permute Digits(dfs)

    C. Permute Digits You are given two positive integer numbers a and b. Permute (change order) of the ...

  3. SQL Server外部链接时报错:Error locating serverInstance specified

    SQL Server外部链接时报错:Error locating server/Instance specified 连接时报错信息: 08001 sql server network interfa ...

  4. gitignore 忽略某文件夹下 非某后缀名的文件

    忽略指定文件夹下的除某一文件之外的其他文件 parent_dir/!spec_file_name.html 忽略指定文件夹下的除某一类后缀名的文件 parent_dir/*[!*.html] 注意:若 ...

  5. Linux虚拟机无法通过宿主机上网

    解决方法 1.Windows: 确保相关服务已经启动 2.Linux: 确保相关服务已经启动 1) 确认Linux的IP地址和Windows在同一个网段: 若Windows给虚拟机分配的IP地址如下: ...

  6. python编程零碎积累

    例行程序 def routine():     lastday = ''     while True:         day = datetime.datetime.now().strftime( ...

  7. Unity Scene Screen.resolutions 分辨率列表

    Screen.resolutions 分辨率列表(安卓平台试了不能用此方法,最好用宏定义判断一下平台) C# => public static Resolution[] resolutions; ...

  8. [转]Using the HTML5 and jQuery UI Datepicker Popup Calendar with ASP.NET MVC - Part 4

    本文转自:http://www.asp.net/mvc/overview/older-versions/using-the-html5-and-jquery-ui-datepicker-popup-c ...

  9. android shape.xml 属性详解

    转载源:http://blog.csdn.net/harvic880925/article/details/41850723 一.简单使用 刚开始,就先不讲一堆标签的意义及用法,先简单看看shape标 ...

  10. XtraReport三动态数据绑定

    代码还用上一节的,把Report的Datasource去掉.各个绑定的字段也去掉,有了第二节的基础,现在看这个就不难了,无非就是传到report一个数据集,在把这个数据集绑定到各控件里 清空detai ...