题目链接: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. [Django]中建立数据库视图

    Django中建立数据库视图 Django中没有建立视图的接口.假设要建立一个视图须要一些手动的改变. 这里使用的Django 版本号>1.5, 使用的数据库为mysql 第一步 建立视图,比如 ...

  2. java8新特性学习笔记(二) 流的相关思想

    流是什么 流是Java API的新成员,他允许你以声明的方式处理数据集合,就现在来说,可以把他们看成遍历数据集合的高级迭代器.此外,流还可以透明地并行处理,你无须写任何多线程代码. 下面例子是新老AP ...

  3. 微信小程序 - 答题进度条

    关于进度条的话,我是使用官方原生的progress的. 关于进度progress接受保留2位小数(从后端获取到平均值,再item循环出来) js wxml

  4. el表达式注意

    如果action那边是String类型,el表达式进行判断的时候必须加引号,即使是数字也要加. 否则可能导致windows正常,linux出错.

  5. Java Transaction Management

    Just a few weeks ago, I had a discussion with one of my colleagues about how to manage the transacti ...

  6. React15.6.0实现Modal弹层组件

    代码地址如下:http://www.demodashi.com/demo/12315.html 注:本文Demo环境使用的是我平时开发用的配置:这里是地址. 本文适合对象 了解React. 使用过we ...

  7. 基于Repository模式设计项目架构—你可以参考的项目架构设计

    关于Repository模式,直接百度查就可以了,其来源是<企业应用架构模式>.我们新建一个Infrastructure文件夹,这里就是基础设施部分,EF Core的上下文类以及Repos ...

  8. Spring中的scope配置和@scope注解

    Scope,也称作用域,在 Spring IoC 容器是指其创建的 Bean 对象相对于其他 Bean 对象的请求可见范围.在 Spring IoC 容器中具有以下几种作用域:基本作用域(single ...

  9. Android之Intent和Activity

    Intent能够说是Android的灵魂,程序跳转和传递数据的时候基本上就是靠Intent了.Intent在Android应用中是相当重要的,理解Intent相应用编程非常有帮助.在Android的官 ...

  10. Mysql 5.7.18 加密连接mysql_ssl_rsa_setup

    MySQL 5.7.18 下载地址: https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.18-linux-glibc2.5-x86_64. ...