【数论】UVa 11526 - H(n)
What is the value this simple C++ function will return?
long long H(int n)
{
long long res = ;
for( int i = ; i <= n; i=i+ )
{
res = (res + n/i);
}
return res;
}
Input
The first line of input is an integer T (T ≤ 1000) that indicates the number of test cases. Each of the next T line will contain a single signed 32 bit integer n.
Output
For each test case, output will be a single line containing H(n).
Sample Input
2
5
10
Sample Output
10
27
题意:求上述函数的返回值;
分析:简单讲就是求出一个tmp=[n/i]后计算一下这个数会出现多少次,方法就是n/tmp,求得的数是满足n/i==tmp的最大i值,然后继续更新i值即可。算法复杂度会由O(n)降为O(sqrt(n));
代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;
LL H(LL n)
{
LL res = ;
LL pre = n, next_pos = n, tmp;
for(LL i = ; i <= n; i++)
{
res += n/i;
tmp = n/i;
if(tmp != pre)
{
next_pos = n/tmp;
res += (next_pos-i)*tmp;
pre = tmp;
i = next_pos;
}
}
return res;
}
int main()
{
int T; scanf("%d", &T);
while(T--)
{
LL n; scanf("%lld", &n);
printf("%lld\n", H(n));
}
return ;
}
【数论】UVa 11526 - H(n)的更多相关文章
- UVa 11526 H(n)
题意: long long H(int n){ long long res = 0; for( int i = 1; i <= n; i=i+1 ){ res = (res + n/i); } ...
- 【数论,找规律】Uva 11526 - H(n)
原来做过的题再看还是没想出来,看来当时必然没有真正理解.这次回顾感觉理解更透彻了. 网上的题解差不多都是一个版本,而且感觉有点扯.根据n=20猜出来的? 好吧哪能根据一个就猜到那么变态的公式.其实这题 ...
- uva 11526 H(n) (数论)
转载自 http://blog.csdn.net/synapse7/article/details/12873437 这道题我自己做的时候没有想到好的优化方法,提交的时候借鉴这篇文章的内容,转载如下: ...
- 数论 UVA 10780
数论题目.有关内容:整数质因数分解,N的阶乘质因数分解,整除的判断. 这道题的题意是给你两个数n.m,要求你求出n!所能整除的m^k的最大值的k是多少. 由于数据范围:1<m<5000,1 ...
- 数论 UVA 10943
这是一道关于组合数和隔板法的数论题目.题目说的是选出k个不同且不大于N的数字进行相加,要求这些数字之和等于N,结果要求输出这样的数有多少组.这里可以将问题利用隔板法来转换,那么题目的叙述可以转换成:这 ...
- 数论 UVA 11889
有关数论的题目,题目大意是给你两个数a和c,c为a和另一个数b的最小公倍数,要求你求出b的最小值.由最大公约数gcd(a,b)和最小公倍数lcm(a,b)之间的关系可知,lcm(a,b)*gcd(a, ...
- 数论 UVA 11388
这道题是关于两个数的最大公约数和最小公倍数的题目.给你两个数字g,l,分别表示最大公约数和最小公倍数.要求你找到两个数a,b,要求这两个数的最大公约数和最小公倍数为所给的两个数.如果存在多组数字符合这 ...
- 紫书 习题 10-13 UVa 11526(打表找规律+分步枚举)
首先看这道题目,我预感商数肯定是有规律的排列的,于是我打表找一下规律 100 / 1 = 100 100 / 2 = 50 100 / 3 = 33 100 / 4 = 25 100 / 5 = ...
- 数论 UVA 10791
这道题目是关于满足同意最小公倍数的所有数对中两数之和的最小值. 题目大意是给你一个数n,要求你求出在所有以n为最小公倍数的数对中两数之和的最小值. 方法:将n进行质因数分解,再将所有分解出的质因子加起 ...
随机推荐
- Java Thread.interrupt 害人! 中断JAVA线程(zz)
http://www.blogjava.net/jinfeng_wang/archive/2012/04/22/196477.html#376322 ————————————————————————— ...
- Serializable 序列化使用限制
序列化不能跨语言 如果单纯为了传数据,完全可以被json代替.
- POJ 1573 (13.10.11)
Description A robot has been programmed to follow the instructions in its path. Instructions for the ...
- cocos2dx搭建开发环境
windows7 64位 搭建cocos2dx 版本开发环境 目前cocos2dx分为2.x版本和3.x版本,搭建环境稍有不同 先搭建3.1版本win32开发环境 相关准备: 注意:安装路径尽可能不要 ...
- [前端JS学习笔记]JavaScript function
一.函数的声明 1.1 function 命令 function methodName(params) { // code } 如下声明: function test_function(params) ...
- C# 解压zip压缩文件
此方法需要在程序内引用ICSharpCode.SharpZipLib.dll 类库 /// <summary> /// 功能:解压zip格式的文件. /// </summary> ...
- PC/UVa 题号: 110104/706 LC-Display (液晶显示屏)题解
#include <string> #include <iostream> #include <cstring> #include <algorithm> ...
- VMware虚拟机扩容
作者:金良(golden1314521@gmail.com) csdn博客:http://blog.csdn.net/u012176591 用了一段Linux虚拟机.发现安装虚拟机时分配的空间不够用, ...
- sqoop的安装与使用
1.什么是Sqoop Sqoop即 SQL to Hadoop ,是一款方便的在传统型数据库与Hadoop之间进行数据迁移的工具.充分利用MapReduce并行特点以批处理的方式加快传输数据.发展至今 ...
- Android-WizardPager
https://github.com/HeinrichReimer/Android-WizardPager