题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1225

基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题
 收藏
 关注
F(n) = (n % 1) + (n % 2) + (n % 3) + ...... (n % n)。其中%表示Mod,也就是余数。

例如F(6) = 6 % 1 + 6 % 2 + 6 % 3 + 6 % 4 + 6 % 5 + 6 % 6 = 0 + 0 + 0 + 2 + 1 + 0 = 3。
给出n,计算F(n), 由于结果很大,输出Mod 1000000007的结果即可。
 
Input
输入1个数N(2 <= N <= 10^12)。
Output
输出F(n) Mod 1000000007的结果。
Input示例
6
Output示例
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 余数之和 —— 分区枚举的更多相关文章

  1. 51nod 1225 余数之和 数论

    1225 余数之和 题目连接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1225 Description F(n) ...

  2. 51Nod 1225 余数之和 [整除分块]

    1225 余数之和 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题  收藏  关注 F(n) = (n % 1) + (n % 2) + (n % 3) + ... ...

  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]\) 可以证明 ...

  4. 51nod 1225 余数的和 数学

    1225 余数之和 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题  收藏  关注 F(n) = (n % 1) + (n % 2) + (n % 3) + ... ...

  5. 51nod1225 余数之和

    打表可以看出规律.分块求就可以了. #include<cstdio> #include<cstring> #include<cctype> #include< ...

  6. bzoj 1257: [CQOI2007]余数之和sum 数学 && 枚举

    1257: [CQOI2007]余数之和sum Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 1779  Solved: 823[Submit][Sta ...

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

  8. [原博客] 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 ...

  9. 1257: [CQOI2007]余数之和sum

    1257: [CQOI2007]余数之和sum Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 2001  Solved: 928[Submit][Sta ...

随机推荐

  1. Usage of API documented as @since1.6+

    Usage of API documented as @since1.6+ File ->Project Structure->Project Settings -> Modules ...

  2. 【SQL】SQL Server中存储过程的调试方法

    1.以管理员用户登录DB服务器,把域用户追加到「Administrators」组. 2.在本机上以域用户登录,启动VS. 3.追加DB连接 4.右击要debug的存储过程,选择「ストアドプロシージャに ...

  3. hibernate映射排序

    @OneToMany(mappedBy="member") @OrderBy(value = "TousuID desc")

  4. HDU 1875 畅通project再续 (最小生成树 水)

    Problem Description 相信大家都听说一个"百岛湖"的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其它的小岛时都要通过划小船来实现. 如今政府决定大力发展百岛 ...

  5. Spket安装

    1.http://www.spket.com/下载1.6.23放到Eclipse或者myEclipse的dropins文件夹下 2. 3. 4. 我的是jquery-1.8.2.js,2.1.1不会提 ...

  6. Deploying Docker images via SSH

    Original URL:https://advancedweb.hu/2015/04/14/deploying-docker-images-via-ssh/ Background When we b ...

  7. oracle 表压缩技术

    压缩表是我们维护管理中常常会用到的.以下我们看都oracle给我们提供了哪些压缩方式. 文章摘自"Oracle® Database Administrator's Guide11g Rele ...

  8. Android UI开源框架

    1.Side-Menu.Android 分类側滑菜单,Yalantis 出品. 项目地址:https://github.com/Yalantis/Side-Menu.Android 2.Context ...

  9. 效率提升最重要的原则 - Doing one thing at a time

    前段时间流行的时间管理方法 - url=NotLrz-4f4eCgENFAECrXNw88mSLoJ2Rc2MrkP4aes1yQvPjNQRlmdYcbz9oP9U8JoBzJeY-DSUhhInx ...

  10. jquery元素分组插件,用于把一连串元素分成多组,如把多个a标签分成多组放入<li>元素中,可以用于简化多图滚动为一个元素滚动,兼容ie6

    三个参数 <script type="text/javascript"> /* *sclass:设置包裹元素的类 * packages:设置包裹的元素 * row:设置 ...