LightOj 1245 --- Harmonic Number (II)找规律
题目链接:http://lightoj.com/volume_showproblem.php?problem=1245
题意就是求 n/i (1<=i<=n) 的取整的和这就是到找规律的题,
i 1 2 3 4 5 6 7 8
a 8 4 2 2 1 1 1 1
你可以多写几组你会发现
有8-4个1;4-2个2;。。。其他例子也是这样;
当n = 10时
n/1 = 10, n/2 = 5说明(5, 10]这个前开后闭的区间中对应的结果都是 1 ,共有10-5 = 5个 ans += 5*1;
n/2 = 5, n/3 = 3说明(3, 5]这个前开后闭的区间中对应的结果都是 2 ,共有5-3 = 2个 ans += 2*2;
n/3 = 3, n/4 = 2说明(2, 3]这个前开后闭的区间中对应的结果都是 3 ,共有3-2 = 1个 ans += 1*3;
n/4 = 2, n/5 = 2说明(2, 2]这个前开后闭的区间中对应的结果都是 4 ,共有2-2 = 0个 ans += 0*4;
n/5 = 2, n/6 = 1说明(1, 2]这个前开后闭的区间中对应的结果都是 5 ,共有2-1 = 1个 ans += 1*5;
对于一个比较大的数n,直接循环一定会错,所以要优化,对于 1 - (int)sqrt(n)所对应的另一半即[n/(int)sqrt(n), n/1]这些数一定只出现了一次,所以加上,剩下的那些就是出现多次的了,按照规律进行处理,注意会加多的那个部分;
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
#define N 20100 int main()
{
int T, n,t=;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
int i=, cnt=;
long long ans=;
if(n==)
{
printf("Case %d: 1\n", t++);
continue;
}
while()
{
int a=n/i;
int b=n/(i+);
ans=ans+a+(a-b)*i;///结果;
cnt=cnt+(a-b)+;///表示已经加了几个数了;
i++;
if(cnt==n-)///当还剩一个数时就结束就行了,并且还要把这个数加上;
{
ans+=n/i;
break;
}
if(b<=i)///当后面那个数<=i事就说明已经加完了;
break;
}
printf("Case %d: %lld\n", t++, ans);
}
return ;
}
简单的写法
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
typedef long long LL;
const int oo = 0xfffffff;
const int N = 1e7+;
const double eps = 1e-; int main()
{
int T, t = ; scanf("%d", &T); while(T--)
{
LL n; scanf("%lld", &n); LL ans = , a = , b = , k = (int)sqrt(n); for(int i=; i<=k; i++)
{
a = n/i, b = n/(i+);
ans += a + (a-b)*i;
}
if(k == n/k)
ans -= k;
printf("Case %d: %lld\n", t++, ans);
}
return ;
}
LightOj 1245 --- Harmonic Number (II)找规律的更多相关文章
- 1245 - Harmonic Number (II)(规律题)
1245 - Harmonic Number (II) PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 3 ...
- LightOJ 1245 Harmonic Number (II)(找规律)
http://lightoj.com/volume_showproblem.php?problem=1245 G - Harmonic Number (II) Time Limit:3000MS ...
- G - Harmonic Number (II) 找规律--> 给定一个数n,求n除以1~n这n个数的和。n达到2^31 - 1;
/** 题目:G - Harmonic Number (II) 链接:https://vjudge.net/contest/154246#problem/G 题意:给定一个数n,求n除以1~n这n个数 ...
- LightOJ - 1245 - Harmonic Number (II)(数学)
链接: https://vjudge.net/problem/LightOJ-1245 题意: I was trying to solve problem '1234 - Harmonic Numbe ...
- lightoj 1245 Harmonic Number (II)(简单数论)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1245 题意:求f(n)=n/1+n/2.....n/n,其中n/i保留整数 显 ...
- LightOJ 1245 - Harmonic Number (II)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1245 题意:仿照上面那题他想求这么个公式的数.但是递归太慢啦.让你找公式咯. ...
- LightOJ 1245 Harmonic Number (II) 水题
分析:一段区间的整数除法得到的结果肯定是相等的,然后找就行了,每次是循环一段区间,暴力 #include <cstdio> #include <iostream> #inclu ...
- LightOJ - 1245 Harmonic Number (II) 求同值区间的和
题目大意:对下列代码进行优化 long long H( int n ) { long long res = 0; for( int i = 1; i <= n; i++ ) ...
- LightOJ - 1234 LightOJ - 1245 Harmonic Number(欧拉系数+调和级数)
Harmonic Number In mathematics, the nth harmonic number is the sum of the reciprocals of the first n ...
随机推荐
- Python中的计数(词频)
1,对于list列表来说 a.用自定义函数来统计技术 def get_count(sequence): counts={} for x in sequence: if x in sequence: c ...
- Spring MVC mapping[From Spring MVC Beginner's Guide]
In a Spring MVC application, the URL can logically be divided into five parts (see the following fig ...
- CSS样式补充代码
CSS符号属性: list-style-type:none; /*不编号*/ list-style-type:decimal; /*阿拉伯数字*/ list-style-type:lower-roma ...
- boost之signal
boost里的signal是一个模板类,不区分信号种类,产生信号统一用()调用操作符. 1.回调普通函数代码示例: #include <iostream> #include <str ...
- 【BZOJ】【1552】【Cerc2007】robotic sort / 【3506】【CQOI2014】排序机械臂
Splay 离散化+Splay维护序列…… 好吧主要说一下我做这道题遇到的几个错误点: 1.离散化 2.由于找到的这个数的位置一定是大于等于 i 的,所以其实在把它splay到根以后,i 结点只能sp ...
- 【BZOJ】【2002】【HNOI2010】弹飞绵羊
呃这题的Hint写着splay启发式合并……但是蒟蒻不懂T_T只好写个简单的LCT来蒙混过关,就是时间效率上差劲的很…… 不过能够一次AC心情也是蛮愉悦的~ /******************** ...
- Java/Java Web中乱码解决汇总
在开发Java/Java Web Application过程中,往往会出现乱码问题,而且有的时候真会弄得人很烦,浪费太多的时间. 记得之前看过一篇帖子,详细解释了Encoding/Decoding过程 ...
- jquery的show/hide性能测试
这篇文章是jQuery各种 show/hide方式的性能测试.作者之所以测试这个源于Robert Duffy在SanFrancisco举行的jQuery大会上的一句话:“.hide()和.show() ...
- vuforia 结合 unity3d 开发 AR 的 androidAPP 总结
原地址:https://software.intel.com/zh-cn/blogs/2014/07/09/vuforia-unity3d-ar-androidapp/?utm_campaign=CS ...
- 《暗黑世界GM管理后台系统》部署+功能说明文档
http://www.9miao.com/product-10-1073.html <暗黑世界GM管理后台系统>部署+功能说明文档 <暗黑世界GM管理后台系统>部署+功能说明文 ...