Factorial--阶乘分解

http://acm.hdu.edu.cn/showproblem.php?pid=1124

Problem

定义函数Z(n),表示n!的结果末尾零的个数。

Soultion

如果将数x进行质因数分解可以发现,要想两个质数相乘为零,只能是2*5了,所有我们就去找n!中质因数为2和质因数为5的个数各有多少个,取最小值即可。

下面贴上阶乘分解的代码。

求n!中质因子p出现的个数num = n/p+n/p^{2}+n/p^{3}+...

while(n){
    cnt2 += n/p;
    n /= p;
}

Code

#include <bits/stdc++.h>
#define ll long long
#define pir pair<int,int>
const int N = 2e5+7;
const int mod = 1e9+7;
const ll ds = 1e15;
const double eps = 1e-8;
 
using namespace std;

ll read()
{
    ll x=0,f=1;
    char c=getchar();
    while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();}
    while(c>='0'&&c<='9') x=(x<<1)+(x<<3)+c-'0',c=getchar();
    return f*x;
}

void print(__int128 x)
{
    if(x < 0) {putchar('-');x = -x;}
    if(x/10) print(x/10);
    putchar(x%10+'0');
}

void solve(){  
    int cnt1 = 0,cnt2 = 0;
    int n,tmp;
    n = read();
    tmp = n;
    while(tmp){
        cnt1 += tmp/2;
        tmp /= 2;
    }
    while(n){
        cnt2 += n/5;
        n /= 5;
    }
    print(min(cnt1,cnt2));
    puts("");
}
 
int main(){
    int t;
    t = read();
    while(t--)
        solve();
    system("pause");
    return 0;
}

做完可以看看这道类似题:https://ac.nowcoder.com/acm/contest/1021/B

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值