WOJ1386-Number Tricks
来源:互联网 发布:王朔人品知乎 编辑:程序博客网 时间:2024/06/06 16:25
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.
样例输入
32 510 10045 10000
样例输出
Case #1: 3Case #2: 24Case #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 <= nvoid 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 primeif(j == pri.n){pri.d[pri.n++] = i;}}}//analyse prime factors of ivoid 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 < 1000for(int i = 0; i < temp.n; i++){//count the useful useful prime factorint 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;}
- WOJ1386-Number Tricks
- tricks
- tricks
- UPDATE tricks
- JavaScript Tricks
- Shell Tricks
- struts2 tricks
- macro tricks
- VisualStudio Tricks
- Small tricks
- Python tricks
- Hadoop Tricks
- Ruby Tricks
- grep tricks
- Debug Tricks
- Tips Tricks
- Android Tricks
- coding tricks
- Linux学习(5)待整理笔记
- java基本类型与包装类型
- 前端如何实现遮罩功能
- 小而美的 React Form 组件
- 【Java】简单入门
- WOJ1386-Number Tricks
- 今天是教师节
- vue(1)
- 练习2: 打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。
- 动态规划
- WOJ1406-Internet Service Providers
- 二叉树的遍历
- 网易2018校招编程题
- 70. Climbing Stairs