题目链接: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. WIN10安装SW2012完整破解文件和问题解决Crack

    Win10在安装solidword后,激活时有可能报错,本文介绍怎么解决问题 安装后激活报错信息: 一.按下面步骤进行安装 1.相关序列号,记住要断网进行安装 Install SolidWorks 2 ...

  2. SElinux学习记录

    1.SELinux:是一种基于域类型模型的强制访问控制安全系统,由NSA编写设计成内核模块包含到内核中,相应的某些安全相关的应用也被打了SE Linux补丁 查看Selinux: ps -Z #查看S ...

  3. 结合element-ui表格自动生成sku规格列表

    最近在写一个根据输入的规格,属性值动态生成sku表格,实现的效果大致如图,这是在vue项目里,结合element-UI表格写的,写好了就整理了一下,把代码贴上来,方便以后使用,不过代码里还是有一些重复 ...

  4. my.助战选择

    ZC:我的选项:铁扇.小白龙.北海龙女.北海龙子(涌浪).老鼠精(避敌锋芒).惠岸.银角 ZC:我的选择(日常速推不是我急需的,慢一点无所谓,我需要过难度):老鼠精(避敌锋芒).银角(首回合隐身,每回 ...

  5. TypeScript -- JavaScript的救赎

    TypeScript的设计目的应该是解决JavaScript的"痛点":弱类型和没有命名空间,导致很难模块化,不适合开发大型程序.另外它还提供了一些语法糖来帮助大家更方便地实践面向 ...

  6. pthread_cond_wait函数的学习以及其他

    pthread_cond_wait() 前使用 while 讲解2009-10-27 9:33LINUX环境下多线程编程肯定会遇到需要条件变量的情况,此时必然要使用pthread_cond_wait( ...

  7. 树形dp学习

    学习博客:https://www.cnblogs.com/qq936584671/p/10274268.html 树的性质:n个点,n-1条边,任意两个点之间只存在一条路径,可以人为设置根节点,对于任 ...

  8. Unity 平台宏定义

    官方文档: https://docs.unity3d.com/Manual/PlatformDependentCompilation.html

  9. pat05-图3. 六度空间 (30)

    05-图3. 六度空间 (30) 时间限制 1500 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard “六度空间”理论又称作“六度分隔(Six Degrees ...

  10. 介绍几个关于C/C++程序调试的函数

    最近调试程序学到的几个挺有用的函数,分享一下,希望对用C/C++的朋友有所帮助! 1. 调用栈系列下面是函数原型: 1 2 3 4 #include "execinfo .h" i ...