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

Problem G

How many zeros and how many digits?

Input: standard input

Output: standard output


 

Given a decimal integer number you will have to find out how many trailing zeros will be there in its factorial in a given number system and also you will have to find how many digits will its factorial have in a given number system? You can assume that for a b based number system there are b different symbols to denote values ranging from 0 ...b-1.


 

Input

There will be several lines of input. Each line makes a block. Each line will contain a decimal number N (a 20bit unsigned number) and a decimal number B (1<B<=800), which is the base of the number system you have to consider. As for example 5! = 120 (in decimal) but it is 78 in hexadecimal number system. So in Hexadecimal 5! has no trailing zeros


 

Output

For each line of input output in a single line how many trailing zeros will the factorial of that number have in the given number system and also how many digits will the factorial of that number have in that given number system. Separate these two numbers with a single space. You can be sure that the number of trailing zeros or the number of digits will not be greater than 2^31-1


 

Sample Input:

2 10
5 16
5 10

 

Sample Output:

0 1
0 2
1 3
这道题目以我的水平自己AC不知道要何年何月

解题思路是参考以下百度到的内容:

先说求多少位数的方法吧。 b的m-1次 <= n!<= b的m次(PS,这个不等式如果把b换成10大家一定会明白的),
看到这个不等式应该有想法了吧。两边同时取logb,就可以得到Σlogi(1<=i<=n) <= m,m直接就求出来了。m即是位数。
   再说怎么求末尾0的,发散下想法,我们也可以对n!中的每个因子试着求b的因子对,一共有多少对。但是,后面发现这样不行,
因为比如b是16,1和16是一对因子,2和8是一对因子,4和4是一对因子,也就是因为2也是4的因子,这样计算因子对就会重复了。
但是对于b等于10的情况,可以例外而已。

思路有了实现起来又遇到很多问题,就是分解质因数时间复杂度太高,已近用了比较高效的版本

  for (j=2;j<=sqrt(n)+1;;j++)
   while (n%j==0) { ++a[j]; n=n/j;}
   if (n>1) ++a[n]

但是这个runtime error n次 ,忘记了是B进制,分解质因数超过B导致数组越界;

改成  for (j=2;j<=sqrt(n)+1&& j<=B;j++)
   while (n%j==0) {if (j<=B) ++a[j]; n=n/j;}
   if ((n>1)&&(n<=B)) ++a[n];

虽然AC但是时间复杂度我累个去7s多,

然后看到了别人的优化才恍然大悟,我们要分解的是小于B的质因数,因为最后求解的时候大于B的质因数不可能使最后产生连续的0

于是之加了一句话,

  for (j=2;j<=sqrt(n)+1&& j<=B;j++)
   while (n%j==0) {if (j<=B) ++a[j]; n=n/j;}
   if ((n>1)&&(n<=B)) ++a[n];

 0.6sAC

#include<stdio.h>
#include<math.h>
void main()
{int N,B,i,j,n,pos,a[1000],b[1000],c[1000],num,flag,sum;
 double s;
 while (scanf("%d%d",&N,&B)!=EOF)
 {s=0;
  for (i=1;i<=N;i++)
  s=s+log10(i);
  s=s/log10(B)+1e-10;//精度问题~~~~(>_<)~~~~
  pos=s;
 
  for (i=1;i<=B;i++)
  {a[i]=0;b[i]=0;}
 
  for (i=2;i<=N;i++)
  {n=i;
   for (j=2;j<=sqrt(n)+1&&j<=B;j++)
   while (n%j==0) {if (j<=B) ++a[j]; n=n/j;}
   if ((n>1)&&(n<=B)) ++a[n];
  }
 
  n=B;
  for (j=2;j<=sqrt(n)+1;j++)
  while (n%j==0) {++b[j]; n=n/j;}
  if (n!=1) ++b[n];
 
  sum=0;
  for (i=2;i<=B;i++)
   if (b[i]>0) {++sum;c[sum]=i;}
 
  flag=1; num=0;
  while (flag)
  {
   for (i=1;i<=sum;i++)
   {a[c[i]]=a[c[i]]-b[c[i]];
    if (a[c[i]]<0) {flag=0;break;}
   }
   if (flag) ++num;
  }
  printf("%d %d\n",num,pos+1);
 }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值