UVa10061 How many zeros and how many digits?

来源:互联网 发布:jump知乎 编辑:程序博客网 时间:2024/05/16 06:14

Problem G

How many zeros and how many digits?

Input: standard input

Output: standard output


Given a decimal integer number you willhave to find out how many trailing zeros will be there in its factorial in a given number system and alsoyou will have to find how many digits will its factorial have in a given number system? You can assume that forab 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 linewill 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 ina single line how many trailing zeros will the factorial of that numberhave in the given number system and also how many digits will the factorial of thatnumber have in that given number system. Separate these two numbers with a single space. You can be surethat the number of trailing zeros or the number of digits will not be greaterthan 2^31-1


Sample Input:

2 10
5 16
5 10

 

Sample Output:

0 1
0 2
1 3
________________________________________________________________________________________
Shahriar Manzoor
16-12-2000

关于这题,大意给定一个数和一个进制,要求求出在该禁止下,这个数的阶乘,最后求出该阶乘有多少个尾零以及位数。尾零和位数可以分开来算。其中由于N进制中的尾零都是由N的全部因子构成的,所以要求出所有质因数及其个数。然后在循环相乘时要不断把阶乘中含有这些因子的项除尽,并记录下各质因数的数量,最后按比例算尾零的个数,要注意的是当进制为质数时,其本身就要算是它的质因数。另外关于位数,假设对于x进制下的n阶乘的位数公式即为number_digits =  (ln(n!)/ln(x))+1 = ln(n)/ln(x) + ln(n-1)/ln(x)...+ ln(1)/ln(x) + 1,最后将两项输出。

#include <iostream>#include <cstring>#include <cmath>using namespace std;const int N = 1000;int s[N];int f[N];int n_f[N];bool isprime(int a) {if (a == 2)return true;for (int i = 2; i <= sqrt(a); i++)if (a % i == 0)return false;return true;}double _logbn(double b,double n) {return log(n)/log(b);}int main() {int n;int b;int n_0;int n_d;while (cin >> n >> b) {memset(f,0,sizeof(f));memset(n_f,0,sizeof(n_f));int tmp_b;if (isprime(b))f[b]++;else {for (int i = 2; i <= b/2; i++) {tmp_b = b;if (isprime(i)) {while (tmp_b % i == 0) {f[i]++;tmp_b /= i;}}}}int tmp;for (int i = 1; i <= n; i++) {tmp = i;for (int j = 2; j <= b; j++) {if (f[j]) {while (tmp % j == 0) {n_f[j]++;tmp /= j;}}}}for (int i = 2; i <= b; i++) {if (f[i]) {n_f[i] /= f[i];}}n_0 = n;for (int i = 2; i <= b; i++) {if (f[i] && n_0 > n_f[i])n_0 = n_f[i];}double s = 0;for (int i = 1; i <= n; i++) {s += _logbn((double)b,(double)i);}n_d = (int)s + 1;cout << n_0 << " " << n_d << endl;}return 0;}


原创粉丝点击