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

时间:2022-03-28 05:36:27

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 231 − 1.
Sample Input
2 10
5 16
5 10
Sample Output
0 1
0 2
1 3

求k进制下,n的阶乘的位数以及末尾0数。

位数求法很简单:我们只要知道一个m位的b进制数n,n一定会满足    b^(m-1)<=n<b^m  (用十进制数模拟一下,就可以得到结论),两边同时 取log(b) 得到 log(n) <= m,对于n!就是 log1+log2+...+log(n)。

然后求末尾0数的话,我们只要将其分解质因子,看能凑齐多少个k。具体看代码吧~

// Asimple
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <vector>
#include <string>
#include <cstring>
#include <stack>
#include <set>
#include <map>
#include <cmath>
#define INF 0x3f3f3f3f
#define mod 1000000007
#define debug(a) cout<<#a<<" = "<<a<<endl
#define test() cout<<"============"<<endl
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = +;
int n, m, T, len, cnt, num, ans, Max, k; //分解质因数
int count_zero(int n, int k) {
int ans = INF;
int p[maxn], q[maxn], c[maxn];
memset(c, , sizeof(c));
memset(q, , sizeof(q)); len = ;
for(int i=; i<=k && k>; i++) {
if( k%i== ) p[len++] = i;
while( k%i== ) {
c[len-]++;
k /= i;
}
} for(int i=; i<=n; i++) {
int t = i;
for(int j=; j<len; j++) {
while( t%p[j]== && t ) {
q[j] ++;
t /= p[j];
}
}
} for(int i=; i<len; i++) {
ans = min(ans, q[i]/c[i]);
} return ans;
} int digits(int n, int k) {
double ans = 0.0;
for(int i=; i<=n; i++) {
ans = ans + log10(i+0.0);
}
ans = ans/log10(k+0.0)+1.0;
return (int)ans;
} void input(){
while( cin >> n >> k ) {
cout << count_zero(n, k) << " " << digits(n, k) << endl;
}
} int main() {
input();
return ;
}

How many zero's and how many digits ? UVA - 10061的更多相关文章

  1. uva 10061 How many zero&&num;39&semi;s and how many digits &quest;

    How many zeros and how many digits? Input: standard input Output: standard output Given a decimal in ...

  2. UVA - 10061 How many zero&amp&semi;&num;39&semi;s and how many digits &quest;

    n!=x*b^y, 当x为正整数时,最大的y就是n!末尾0的个数了, 把n,b分别拆成素因子相乘的形式: 比如, n=5,b=16 n=5,b=2^4, 非常明显,末尾0的个数为0 10进制时,n!= ...

  3. UVA 10061 How many zero&&num;39&semi;s and how many digits &quest; &lpar;m进制,阶乘位数,阶乘后缀0&rpar;

    题意: 给出两个数字a和b,求a的阶乘转换成b进制后,输出 (1)后缀中有多少个连续的0? (2)数a的b进制表示法中有多少位? 思路:逐个问题解决. 设a!=k.  k暂时不用直接转成b进制. (1 ...

  4. n&excl;在k进制下的后缀0

    问n! 转化成k进制后的位数和尾数的0的个数.[UVA 10061 How many zeros and how many digits?] Given a decimal integer numbe ...

  5. &lbrack;LeetCode&rsqb; Reconstruct Original Digits from English 从英文中重建数字

    Given a non-empty string containing an out-of-order English representation of digits 0-9, output the ...

  6. &lbrack;LeetCode&rsqb; Remove K Digits 去掉K位数字

    Given a non-negative integer num represented as a string, remove k digits from the number so that th ...

  7. &lbrack;LeetCode&rsqb; Count Numbers with Unique Digits 计算各位不相同的数字个数

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

  8. &lbrack;LeetCode&rsqb; Add Digits 加数字

    Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...

  9. LeetCode 258&period; Add Digits

    Problem: Given a non-negative integer num, repeatedly add all its digits until the result has only o ...

随机推荐

  1. Mysql 查询Hash分区

    select * from information_schema.partitions where table_schema=database() and table_name='table_name ...

  2. M1分数分配

    进过第一轮迭代我们依据工作量及质量决定分配分数方案: 王皓南 24.5分 黄宇冰 24分 申开亮 23.5分 许晋 21分 王宇杰 17分 吴润凡 16分 巴丹益昔 14分

  3. CentOS7 firewall的使用

    # 查看区域 firewall-cmd --get-zones # 查看默认区域 firewall-cmd --get-default-zone # 给区域添加永久性服务 firewall-cmd - ...

  4. codevs1002搭桥(建图&plus;Prim)

    /* 先来个灌水法 然后建图跑最小生成树 注意观察题目中的规则 a[1][i]!=a[1][j]&&abs(a[2][i]-a[2][j])<=1 建图的时候可以每一个建筑物都看 ...

  5. 去掉firefox点击按钮时的虚线边框

    去掉火狐里面点击按钮时候的虚线边框 button::-moz-focus-inner, input[type="reset"]::-moz-focus-inner, input[t ...

  6. &lbrack;Windows Phone学习笔记&rsqb;UserControl的使用

    UserControl的使用 开发过程中,多个UI控件需要协同工作,相互交互之后,才可完成一个完整的业务需求,此时可把这些控件封装成为一个整体,相互之间的交互逻辑封装其中,外部调用可无需关心内部逻辑, ...

  7. win7热点设置

    1.设置热点名称与密码 netsh wlan set hostednetwork mode=allow ssid=costa key=11112222pause 2.开启 netsh wlan sta ...

  8. java&lowbar;工厂模式

    定义: 初学者总是把所有的代码写在一个类里面,这样是非常危险的,因为所有错误集中在一个类里了,而且代码一长,调试就很困难 所以参照工厂流水线,分车间分模块来写代码,在实际操作中也就是说将代码模块化,封 ...

  9. 2017年3月30日15&colon;00&colon;19 fq以后的以后 动态代理

    代理与继承,组合不同的是,继承是继承父类特性,组合是拼装组合类的特性,代理是使用代理类的指定方法并可以做自定义. 静态类是应用单个类,当代理的类数量较多时可用动态代理,动态代理在概念上很好理解 htt ...

  10. WebSphere安装教程(WAS6&period;1为例)

    1.网络准备 我们选择图形界面安装,如果堡垒机是windows则要在目标机器安装桌面环境并开启vcnserver:如果堡垒机是Linux则在堡垒机安装桌面环境和vncserver,然后将目标机的DIS ...