WOJ1386-Number Tricks

Dora loves impressing her friends with number tricks. One of her favorite tricks is to let them name a number n. Dora will then tell them, how many zeroes there are at the end of n!. Usually, Dora’s friends are very impressed because n! is such a large number. For example, if n is 100, even though n! is bigger than the estimated number of atoms in the universe (not counting parallel universes, though), Dora can quickly tell that n! ends with exactly 24 zeroes.
However, some of Dora’s newest friends are aliens with more or less than 10 fingers (or however you call the corresponding parts of their bodies). Like all life forms she has met so far, they count in a number system that has as many digits as they have fingers (even the centipedes from planet Millennium!). Dora wants to impress them as well, but she is not sure how to adapt her trick. Can you help her?
Problem
Given a base b and a number n, you should compute how many zeroes there are at the end of n! when written in base b. For example, consider b = 2 and n = 5. Since 5! = 120 = (1111000)2, the answer is three.

输入格式

The first line contains the number of scenarios. For each scenario, the input will consist of one line containing the two numbers b (2 <= b <= 1 000) and n (1 <= n <= 1 000 000), both given in base ten.

输出格式

The output for every scenario begins with a line containing “Case #i: ”, where i is the number of the scenario starting at 1. Then print a single line containing (in base ten) k, the number of zeroes at the end of n! when written in base b.

样例输入

3
2 5
10 100
45 10000

样例输出

Case #1: 3
Case #2: 24
Case #3: 2498


// prime factorization
#include<iostream>
#include<cstring>
using namespace std;

typedef struct
{
	int d[200];
	int n;
}prime;

typedef struct
{
	int d[200];
	int c[200];
	int n;
}base;

prime pri;
base temp;
int count[200];
int sum;
//calc prime table where 0 <= i <= n
void primeCalc(int n)
{
	pri.n = 1;
	pri.d[0] = 2;
	for(int i = 3; i < n + 1; i++){
		int j;
		for(j = 0; j < pri.n; j++){
			if(i % pri.d[j] == 0)
				break;
		}
		//i is a prime
		if(j == pri.n){
			pri.d[pri.n++] = i;
		}
	}
}

//analyse prime factors of i
void primeAnalyse(int i)
{
	int j;
	temp.n = 0;
	for(j = 0; j < pri.n && i >= pri.d[j]; j++){
		if(i % pri.d[j] == 0){
			temp.d[temp.n] = pri.d[j];
			temp.c[temp.n] = 1;
			i = i / pri.d[j];
			while(!(i % pri.d[j])){
				temp.c[temp.n]++;
				i = i / pri.d[j];
			}
			temp.n++;
		}
	}
}

//analyse the useful factor of i for base b
/*void analyse(int n)
{
	for(int i = 0; i < temp.n && n >= temp.d[i]; i++){
		if(n % temp.d[i] == 0){
			while(!(n % temp.d[i])){
				n = n / temp.d[i];
				count[i]++;
			}
		}
	}
}*/

void calc(int n)
{
	//n < 1000
	for(int i = 0; i < temp.n; i++){
		//count the useful useful prime factor
		int fac = temp.d[i];
		int c[32];
		int j = 0;
		//less than 30 times multiplication
		/*while(fac <= n){
			c[j] = n / fac;
			fac *= temp.d[i];
			j++;
		}*/
		c[0] = n / fac;
		while(c[j]){
			c[++j] = c[j - 1] / fac;
			c[j - 1] -= c[j];
			count[i] += j * c[j - 1];
		}
//		c[j] = 0;
//		for(int k = 0; k < j; k++){
//			c[k] -= c[k + 1];
//			count[i] += (k + 1) * c[k];
//		}
	}
}
void work(int base, int n)
{
	primeAnalyse(base);
	memset(count, 0, 800);
//	for(int i = 1; i < n + 1; i++){
//		analyse(i);	
//	}
	calc(n);
	sum  = -1 ^ (1 << 31);
	int sumtemp = 0;
	for(int i = 0; i < temp.n; i++){
		sumtemp = count[i] / temp.c[i];
		if(sumtemp < sum)
			sum = sumtemp;
	}
}

int main()
{
	int t;
	int n, b;
	cin >> t;
	primeCalc(1000);
	for(int i = 0; i < t; i++){
		cin >> b >> n;
		work(b , n);
		cout << "Case #" << i + 1 << ": "<< sum << std::endl;
	}
	return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值