【数论】UVa 11526 - H(n)
2015-03-10 19:32
351 查看
What is the value this simple C++ function will return?
long long H(int n) { long long res = 0; for( int i = 1; i <= n; i=i+1 ) { 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 = 0; LL pre = n, next_pos = n, tmp; for(LL i = 1; 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 0; }View Code
相关文章推荐
- uva 11526 H(n) (数论)
- UVA-11526H(n)(数论)
- 【数论,找规律】Uva 11526 - H(n)
- UVa 11526 H(n) (数论)
- UVA 10090 - Marbles (数论)
- UVA 11105 - Semi-prime H-numbers(数论)
- uva 11027(数论)
- UVa 1645 Count (递推,数论)
- [数论]Irrelevant Elements, ACM/ICPC NEERC 2004, Uva1635
- UVa 128 Software CRC 数论(模运算)
- Choose and divide UVA - 10375 (数论+唯一分解定理)
- UVa 11526 H(n)
- [UVA10139]Factovisors(数论,质因数)
- UVa 106 - Fermat vs Pythagoras(数论题目)
- UVA 575 (暑假-数论 -A - Skew Binary)
- UVa 10791 Minimum Sum LCM (数论&素因子分解)
- UVA 11526 H(n)
- UVA 10622 - Perfect P-th Powers(数论)
- uva350(数论)
- poj 1606 Jugs and poj 3414 Pots(数组模拟BFS) uva 571 (数论)