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/p^2]+[n/p^3]+...+1在O(log n/log p)的时间内统计出n!的质因子p的个数。(不过此题n太小,两种方法在时间上相差不大)

完整代码:

/*0.016s*/

#include<cstdio>
#include<cmath>
#include<cstring>  

int prime[100], c, ans[100];
bool vis[110];  

inline void create_prime()
{
    int i, j;
    for (i = 2; i <= 11; ++i)
        if (!vis[i])
        {
            prime[c++] = i;
            for (j = i * i; j < 110; j += i)
                vis[j] = true;
        }
    for (; i < 110; ++i)
        if (!vis[i])
            prime[c++] = i;
}  

int main(void)
{
    create_prime();
    int n, temp;
    while (scanf("%d", &n), n)
    {
        memset(ans, 0, sizeof(ans));
        printf("%3d! =", n);
        for (int i = 2; i <= n; i++)
        {
            temp = i;
            for (int j = 0; j < c && prime[j] <= i; j++)
            {
                while (temp % prime[j] == 0)
                {
                    ++ans[prime[j]];
                    temp /= prime[j];
                }
            }
        }
        for (int i = 0, j = 0; prime[i] <= n; i++, j++)
        {
            if (j % 15 == 0 && j) printf("\n      ");
            printf("%3d", ans[prime[i]]);
        }
        putchar('\n');
    }
    return 0;
}

查看本栏目更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索http
, include
, uva
, 时间
, 数论
, 个数
debugc++uva
successfactors、human factors、sap successfactors、factors、prime factors,以便于您获取更多的相关知识。

时间: 2024-08-22 14:42:01

UVa 160 Factors and Factorials:数论的相关文章

UVa 10791 Minimum Sum LCM (数论&amp;amp;素因子分解)

10791 - Minimum Sum LCM Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&category=467&page=show_problem&problem=17 32 LCM (Least Common Multiple) of a set of integers is defined as the minimum

UVa 10110 Light, more light:数论

10110 - Light, more light Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_problem&problem=1051 The Problem There is man named "mabu" for switching on-off light

算法题:UVA 106 Fermat vs. Pythagoras(数论)

Fermat vs. Pythagoras Background Computer generated and assisted proofs and verification occupy a small niche in the realm of Computer Science. The first proof of the four-color problem was completed with the assistance of a computer program and curr

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 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 lo

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

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 568 Just the Facts:数论&amp;amp;打表&amp;amp;不打表

568 - Just the Facts Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_problem&problem=509 The expression N!, read as ``N factorial," denotes the product of the first