Sum of Remainders(数学题)
F - Sum of Remainders
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Description
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.
Sample Input
- 3 4
- 4
- 4 4
- 1
- 1 1
0
//给你两个数n,m,问你n % 1 + n % 2 + … + n% m为几
这个题目的思路是,和为 n * m - sum ( [ n / i ] * i ) ,[ ] 是向下取整,i 从(1--- m)
具体是:
前面的 n*m 肯定就这样了
主要是后面的 :将 [ n / i ] 相同的做一个区间,用求和公式去节省时间
i 从 1 --- sqrt (n) ;
l = n / ( i + 1 ) + 1 , r = n / i ; // 这就是一个个的区间
比如 n = 20 , m = 20
i=1 --> l=11, r=20 n / (11---20) 都是等于 1
i=2 --> l=7, r=10 n / (7---10) 都等于2
i=3 --> l=r=6
i=4 --> l=r=5
注意一些特殊情况,看注释
- #include <iostream>
- #include <cstdio>
- #include <cmath>
- using namespace std;
- #define ll __int64
- const ll MOD=1e9+;
- int main()
- {
- ll n,m;
- scanf("%I64d%I64d",&n,&m);
- ll ans=(n%MOD)*(m%MOD)%MOD;
- ll temp=,las=m+;//记录哪些数没被减
- m=min(n,m);//n 余大于 n 的都等于 n
- ll nn=(ll)sqrt(n*1.0);
- for (ll i=;i<=nn;i++)
- {
- ll l = n/(i+)+;
- ll r = n/i;
- r=min(r,m);//可能 r 比 m 大
- if (l>r) continue;
- las=min(las,l);
- ll s1=l+r , s2 =(r-l+);//这里高斯求和有个坑,要先判断哪个数可以除2,再乘
- if (s1%==) s1/=; //直接用公式也不对,会超出ll限制
- else s2/=;
- s1%=MOD;s2%=MOD;
- s1=(s1*s2)%MOD;
- s1=s1*i%MOD;
- temp=(temp+s1)%MOD;
- }
- ans=(ans+MOD-temp)%MOD;
- for (ll i=;i<las;i++) //剩下的没被减得数,将n余之为0的最大整数减去
- {
- temp=n/i%MOD*i%MOD;
- ans=(ans+MOD-temp)%MOD;
- }
- printf("%I64d\n",ans);
- return ;
- }
Sum of Remainders(数学题)的更多相关文章
- codeforces 616E Sum of Remainders (数论,找规律)
E. Sum of Remainders time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- 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 616E - Sum of Remainders
616E Sum of Remainders Calculate the value of the sum: n mod 1 + n mod 2 + n mod 3 + - + n mod m. As ...
- Codeforces 616 E Sum of Remainders
Discription Calculate the value of the sum: n mod 1 + n mod 2 + n mod 3 + ... + n mod m. As the resu ...
- Educational Codeforces Round 5 E. Sum of Remainders (思维题)
题目链接:http://codeforces.com/problemset/problem/616/E 题意很简单就不说了. 因为n % x = n - n / x * x 所以答案就等于 n * m ...
- codeforces 616E. Sum of Remainders 数学
题目链接 给两个数n, m. 求n%1+n%2+.......+n%m的值. 首先, n%i = n-n/i*i, 那么原式转化为n*m-sigma(i:1 to m)(n/i*i). 然后我们可以发 ...
- HDU-2058-The sum problem(数学题技巧型)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2058 思路: 这题的n,m都很大,很显然直接暴力,会超时,那就不能全部都找了,利用等差数列求和公式, ...
- hdu 2058 The sum problem(数学题)
一个数学问题:copy了别人的博客 #include<cstdio> #include<cstdlib> #include<cmath> int main() { ...
- 杭电ACM分类
杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...
随机推荐
- django 用model来简化form
django里面的model和form其实有很多地方有相同之处,django本身也支持用model来简化form 一般情况下,我们的form是这样的 from django import forms ...
- Nginx user_agent、if指令及全局变量
Nginx user_agent.if指令及全局变量 1.User_agent User Agent中文名为用户代理,简称 UA,它是一个特殊字符串头,使得服务器能够识别客户使用的操作系统及版本.CP ...
- 转: Java 应用一般架构
http://mp.weixin.qq.com/s?__biz=MzAwMzI3Njc1MA==&mid=2650192186&idx=1&sn=bd08fd3a89f9089 ...
- [Create_Cdi]
bbb 原理:游标就是把数据按照指定要求提取出相应的数据集,然后逐条进行数据处理.1.1游标的概念 游标(Cursor)它使用户可逐行访问由SQL Server返回的结果集. 使用游标(cursor ...
- 数据结构与算法——优先队列类的C++实现(二叉堆)
优先队列简单介绍: 操作系统表明上看着是支持多个应用程序同一时候执行.其实是每一个时刻仅仅能有一个进程执行,操作系统会调度不同的进程去执行. 每一个进程都仅仅能执行一个固定的时间,当超过了该时间.操作 ...
- 通过项目了解JAVA注解
java自定义注解实践 ² 背景 最近在为公司的技术改造做准备,我设计了一个提高Web开发效率的技术框架,为了增加框架的友好性和易用性,决定采用注解来代替配置文件,于是我查询了很多的资料,进行整理和学 ...
- SpringMVC文件上传的配置
记述一下步骤以备查. 准备工作: 需要把Jakarta Commons FileUpload及Jakarta Commons io的包放lib里. 我这边的包是: commons-fileupload ...
- 编程算法 - 食物链 并查集 代码(C)
食物链 并查集 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 有N仅仅动物, 分别编号为1,2,...,N. 全部动物都属于A,B,C中的一种 ...
- C++ 大规模数据排序(100G数据 使用 4G 内存 排序)
思路很简单,先分段排序,存储到临时文件中,然后合并. 使用10000个整数来模拟大数据,每次读取100个到内存中. #include <stdint.h> #include <std ...
- Java Applet 基础
Java Applet 基础 Applet 是一种 Java 程序.它一般运行在支持 Java 的 Web 浏览器内.因为它有完整的 Java API支持,所以Applet 是一个全功能的 Java ...