51Nod 1225 余数之和 —— 分区枚举
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1225
输入1个数N(2 <= N <= 10^12)。
输出F(n) Mod 1000000007的结果。
6
3
题解:
这题(LightOJ1245)的加强版,本来是求sigma(n/i) 1<=i<=n,而此题求的是:sigma(n%i) 1<=i<=n。
分两个区间枚举:
第一个区间 [1,sqrt(n)]:设i为除数,从1到sqrt(n)枚举i, 直接 ans += n%i;
第二个区间 [sqrt(n), n]:设i为商,从1到sqrt(n)枚举i,由于用后面区间的数去除n所得到的商,在很大一片区域是相同的,在这片区域,他们的余数成等差数列,且公差即为商,所以就可根据等差数列的求和公式,求出这段区域的余数和了。
需要特判在sqrt(n)处是否重复计算了。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXM = 1e5+;
const int MAXN = 1e6+; //inv(2,MOD-2) = 500000004
int main()
{
LL n;
while(scanf("%lld", &n)!=EOF)
{
LL m = (LL)sqrt(n);
LL ans = ;
for(LL i = ; i<=m; i++)
ans = (ans+n%i)%MOD;
for(LL i = ; i<=m; i++)
{
LL cnt = ((n/i)%MOD-(n/(i+))%MOD+MOD)%MOD; //求个数也要求模
LL a1 = n%i;
LL s = (a1*cnt%MOD + ((cnt*(cnt-)%MOD)*500000004LL%MOD)*i%MOD)%MOD;
ans = (ans+s)%MOD;
} if(n/m==m) ans = (ans-(n%m)+MOD)%MOD;
printf("%lld\n",ans);
}
}
51Nod 1225 余数之和 —— 分区枚举的更多相关文章
- 51nod 1225 余数之和 数论
1225 余数之和 题目连接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1225 Description F(n) ...
- 51Nod 1225 余数之和 [整除分块]
1225 余数之和 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 收藏 关注 F(n) = (n % 1) + (n % 2) + (n % 3) + ... ...
- 51nod 1225:余数之和
传送门 题意 略 分析 \(\sum_i^n(n\%i)=\sum_i^n(n-i*n/i)=n^2-\sum_i^ni*n/i\) \(=\sum r\sum_i^ni[n/i==r]\) 可以证明 ...
- 51nod 1225 余数的和 数学
1225 余数之和 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 收藏 关注 F(n) = (n % 1) + (n % 2) + (n % 3) + ... ...
- 51nod1225 余数之和
打表可以看出规律.分块求就可以了. #include<cstdio> #include<cstring> #include<cctype> #include< ...
- bzoj 1257: [CQOI2007]余数之和sum 数学 && 枚举
1257: [CQOI2007]余数之和sum Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 1779 Solved: 823[Submit][Sta ...
- 【BZOJ1257】【CQOI2007】余数之和sum
Description 给出正整数n和k,计算j(n, k)=k mod 1 + k mod 2 + k mod 3 + … + k mod n的值,其中k mod i表示k除以i的余数.例如j(5, ...
- [原博客] BZOJ 1257 [CQOI2007] 余数之和
题目链接题意: 给定n,k,求 ∑(k mod i) {1<=i<=n} 其中 n,k<=10^9. 即 k mod 1 + k mod 2 + k mod 3 + … + k mo ...
- 1257: [CQOI2007]余数之和sum
1257: [CQOI2007]余数之和sum Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 2001 Solved: 928[Submit][Sta ...
随机推荐
- POJ-1190-生日蛋糕-DFS(深搜)-枚举-多重剪枝
题目链接: 这个题目非常好,有难度:能够好好的多做做: #include<iostream> #include<string> #include<cstdio> # ...
- java -jar xxx.jar
之前用MyEclipse做了一个可执行jar,点击就可运行的. 今天突然不好用了,错误是: could not find the main class C:\123\abc.jar.Program w ...
- 上传项目至svn服务器,从svn上获取项目
1.在桌面右键->TortoiseSVn->Repo_brower->输入地址,进入 ,ok 2.在地址目录上右键==>>add folder==>>选择你要 ...
- HDU 3466 01背包变形
给出物品数量N和总钱数M 对于N个物品.每一个物品有其花费p[i], 特殊值q[i],价值v[i] q[i] 表示当手中剩余的钱数大于q[i]时,才干够买这个物品 首先对N个物品进行 q-p的排序,表 ...
- Action window Flags
Action window 主要字段使用 含义 target 值 作用 current 当前窗口 new 新窗口 inline 内联编辑 fullscreen 全屏 main 当前窗口的主动作 ...
- P13在O(1)时间内删除链表结点
package offer; //在 O(1)时间删除链表结点 public class Problem13 { public static void main(String[] args) { Li ...
- js 中的 prototype 和 constructor
var a=function(){ this.msg="aa"; } a.prototype.say=function(){ alert('this is say');} 1.只有 ...
- Maven上传本地jar
1. 将Jar包安装到本地仓库 -- DgroupId和DartifactId构成了该jar包在pom.xml的坐标, 对应依赖的DgroupId和DartifactId -- Dfile表示需 ...
- 允许局域网内其他主机访问本地MySql数据库
mysql的root账户,我在连接时通常用的是localhost或127.0.0.1,公司的测试服务器上的mysql也是localhost所以我想访问无法访问,测试暂停. 解决方法如下: 1,修改表, ...
- SQLite 数据库安装与创建数据库
嵌入式关系数据库 Ubuntu $ sudo apt-get install sqlite3 sqlite3-dev CentOS, or Fedora $ yum install SQLite3 s ...