10061 - How many zero's and how many digits ?

题目的意思是求n!以m进制表示时的位数,以及有多少位尾零。

看了一些答案,觉得不是太冗长就是太难理解。

求位数用log就行,稍微注意些精度。

尾零的个数就是n!能凑出几个m相乘,2100在十进制下有两个尾零就是因为能凑成21*10*10,有两个10

求尾零的位数的基本思路:进制m因式分解有多少个最大质因数,设为1组;1到n包含多少组,就有多少个尾零。

比如:10因式分解有1个最大质因数5,1到5的因数中包含因子5的因数只有5,也就是有一组,所以5!在十进制下有1个尾零;

比如:9因式分解有2个最大质因数3,1到12的因数中包含因子3的因数有3,6,9,12,其中9包含两个,所以有5/2=2组,所以12!在十进制下有2个尾零(可以稍微在脑洞中消化一下,其正确性是显而易见的)

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cstring>
#include <iomanip>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
//#define LOCAL
using namespace std;

double dig[1<<20];

int main()
{
   #ifdef LOCAL
   freopen("test_in.txt", "r", stdin);
   freopen("test_out.txt", "w", stdout);
   #endif
   
   dig[0]=0.0;
   for(int i=1;i<(1<<20);++i)
      dig[i]=dig[i-1]+log(i);

   int m,n;
   while(cin>>m>>n){
      int a=n,ma,sum=0,count;

      for(int i=2;i<=n;++i){    //寻找进制n的最大质因数及其个数
         count=0;
         while(a%i==0){
            a/=i;
            count++;
         }
         if(a==1){
            ma=i;
            break;
         }
      }
      a=m;
      while(a/ma>0){           //1到m的因数中有多少组这种质因数
         sum+=a/ma;
         a/=ma;
      }
      sum/=count;
      cout<<sum<<" "<<int(dig[m]/log(n)+1+1e-8)<<endl;
   }

   return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值