UVa 11526 H(n) (数论)

11526 - H(n)

Time limit: 5.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2521

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                      Output for Sample Input

2
5
10
10
27

怎么计算sum{ [n/i] }?(1<=i<=n)(n<=2147483647)

n太大,硬算肯定不行,我们先观察一个例子,看能否得出一些结论。

当n=20时,和式展开为

20+10+6+5+4+3+2+2+2+2+1+1+1+1+1+1+1+1+1+1

注意到后面相同的数太多,不妨化简下:

20+10+6+5+1*(20-10)+2*(10-6)+3*(6-5)+4*(5-4)

=(20+10+6+5)+(20+10+6+5)-4*4

=2(20+10+6+5)-4*4

也许,我们可以

本文URL地址:http://www.bianceng.cn/Programming/sjjg/201410/45351.htm

这样,复杂度就从O(n)降为O(√n)了。

完整代码:

/*0.206s*/

#include<cstdio>
#include<cmath>
typedef long long ll;  

inline ll ans(ll n)
{
    ll r = 0, m = sqrt(n), i;
    for (i = 1; i <= m; ++i) r += n / i;
    return (r << 1) - m * m;
}  

int main()
{
    int t;
    ll n;
    scanf("%d", &t);
    while (t--)
    {
        scanf("%lld", &n);
        printf("%lld\n", ans(n));
    }
    return 0;
}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索c++
, int
, function
, long
, 数论
category
uva11572、11518 uva、h.zhang uva.nl、http 218.26.115.219、218.26.115..219,以便于您获取更多的相关知识。

时间: 2024-10-28 10:40:23

UVa 11526 H(n) (数论)的相关文章

UVa 136 Ugly Numbers (数论)

136 - Ugly Numbers Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=72 Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2,

UVa 11121 Base -2 (数论 &amp;amp; -2进制 &amp;amp; 补足思想)

11121 - Base -2 Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=2062 The creator of the universe works in mysterious ways. But he uses a base ten countin

UVa 10892 LCM Cardinality (数论&amp;amp;素因子分解)

10892 - LCM Cardinality Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&category=467&page=show_problem&problem=18 33 A pair of numbers has a unique LCM but a single number can be the LCM of m

算法题:UVA 10105 Polynomial Coefficients(数论)

Polynomial coefficients The Problem The problem is to calculate the coefficients in expansion of polynomial (x1+x2+...+xk)n. The Input The input will consist of a set of pairs of lines. The first line of the pair consists of two integers n andk separ

算法题:UVA 128 Software CRC(数论 进制转化)

Software CRC You work for a company which uses lots of personal computers. Your boss, Dr Penny Pincher, has wanted to link the computers together for some time but has been unwilling to spend any money on the Ethernet boards you have recommended. You

UVa 160 Factors and Factorials:数论

160 - Factors and Factorials Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=96 题目点这:http://uva.onlinejudge.org/external/1/160.pdf 思路:遍历1~n计算质因子个数即可. 你若还想再快点的话,就用[n/p]+[n

UVa 402 M*A*S*H (STL&amp;amp;list)

402 - M*A*S*H Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=343 Corporal Klinger is a member of the 4077th Mobile Army Surgical Hospital in the Korean W

UVa 524 Prime Ring Problem:数论&amp;amp;DFS

524 - Prime Ring Problem Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=465 A ring is composed of n (even number) circles as shown in diagram. Put natural numbers into e

UVa 11889 Benefit (数论)

11889 - Benefit Time limit: 5.000 seconds http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&category=467&page=show_problem&problem=29 89 Recently Yaghoub is playing a new trick to sell some more. When somebody gives him