HDU 1124 Factorial (数论)

http://acm.hdu.edu.cn/showproblem.php?

pid=1124

題目好長好長,好可怕,看完腎都萎了,以後肯定活不長。我可不能死在這種小事上,小灰灰我勵志死在少女的超短裙下~~~哈哈,所以我就猥瑣的叫 旁邊的小師妹幫我翻譯了,我是不是非常禽獸,嘻嘻~~~

題目大意呢,就是給一個數,要你求出它的階乘的得到的結果後面有幾個0。

解析:

一看就是簡單數論啦。跟數因子有關。最小素因子并且相乘能得到10的(就是後面有0的)就是2*5啦。因為一個數的階乘2的因子明顯比5的因子要多得多,所以末尾0的個數不能取決于2因子的個數啦,仅仅能取5因子的個數就ok了。然後,有一個小小的公式:

求N。中素因子p的個數 直接:

[ n/p ] + [ n/p^2 ] + [ n/p^3 ] + .....(終止條件: n >= p^x)

代碼:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;

int res, n, m, temp, cas;

int main()
{
    scanf("%d", &cas);
    while(cas--) {
        scanf("%d", &n);
        temp = n, m = 5, res = 0;
        while(m <= temp) {
            res += temp/m;
            m *= 5;
        }
        printf("%d\n", res);
    }
    return 0;
}

版权声明:本文博主原创文章。博客,未经同意不得转载。

时间: 2024-08-28 06:18:37

HDU 1124 Factorial (数论)的相关文章

hdu 1124 Factorial 数论,就是求一个数的阶乘的结果末尾有多少0.

Factorial Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2990    Accepted Submission(s): 1921 Problem Description The most important part of a GSM network is so called Base Transceiver Station

HDU 1124 Factorial(简单数论)

Factorial Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4237    Accepted Submission(s): 2805 Problem Description The most important part of a GSM network is so called Base Transceiver Station

HDU 1124 Factorial (數論)

http://acm.hdu.edu.cn/showproblem.php?pid=1124 題目好長好長,好可怕,看完腎都萎了,以後肯定活不長,我可不能死在這種小事上,小灰灰我勵志死在少女的超短裙下~~~哈哈,所以我就猥瑣的叫 旁邊的小師妹幫我翻譯了,我是不是很禽獸,嘻嘻~~~ 題目大意呢,就是給一個數,要你求出它的階乘的得到的結果後面有幾個0: 解析: 一看就是簡單數論啦,跟數因子有關,最小素因子而且相乘能得到10的(就是後面有0的)就是2*5啦,因為一個數的階乘2的因子明顯比5的因子要多得

HDU 11124 Factorial (数论)

Factorial Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2807    Accepted Submission(s): 1804 Problem Description The most important part of a GSM network is so called Base Transceiver Station

HDU 1124 Factorial (阶乘后缀0)

题意: 给一个数n,返回其阶乘结果后缀有几个0. 思路: 首先将十进制质因数分解得2*5=10.将n!质因数分解,那么分解后,其中应含有min(2个数,5个数)个后缀0. 为何这么说?例如n=15,那么{1 2 3 4 5 6 7 8 9  10 11 12 13 14 15},那么可以产生2的数字有{2,4,6,8,10,12,14},可以产生5的只有{5,10,15},质数中只有2乘以5才能形成10,因为素数只有2是偶数!!!那么min(2个数,5个数)就决定了可以产生10的个数,也就决定了

hdu 1124 数论

题意:求n!中末尾连续0的个数  其实就是2*5的个数 30! 中有5 10 15 20 25 30  是5的倍数有6个   6=30/5; 6/5=1; 这个1 为25 5  10 15 20  25  30 35 40 45 50 55 60  65 70 75 80  85 90 95 100      100/5=20; 25                     50                    75                     100       20/5=4

hdu 4196(数论)

题意:问小于n的数的乘积能拼成的最大平方数是多少? 思路:给n!做质数分解在除去指数为奇数的那些质数,由于题目中需要模运算所以不能直接除,必须乘上摸逆. 代码如下: 1 /************************************************** 2 * Author : xiaohao Z 3 * Blog : http://www.cnblogs.com/shu-xiaohao/ 4 * Last modified : 2014-06-28 15:26 5 * Fi

hdu 1124

题意:求N!中末尾0的个数 其实就是5的个数 因为2*5=10 肯定n中2的个数比5的个数多#include<iostream> using namespace std; int main() { int n; int t,d; int sum; scanf("%d",&t); while(t--) { sum=0; scanf("%d",&n); sum=n/5;n=sum; while(n/5!=0) { sum+=n/5; n=n/

HDU 1142 Factorial ( 算术基本定理 + 分解N! )

HDU 1142 Factorial ( 算术基本定理 + 分解N! ) #include <cstdio> int main() { int t, n; scanf( "%d", &t ); while( t-- ) { scanf( "%d", &n ); int cnt = 0; while( n ) { cnt += n/5; n /= 5; } printf( "%d\n", cnt ); } return