n!在k进制下的后缀0

问n! 转化成k进制后的位数和尾数的0的个数。【UVA 10061 How many zeros and how many digits?】

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 2311.
Sample Input
2 10
5 16
5 10
Sample Output
0 1
0 2
1 3
View Code

 

#include <stdio.h>
#include <math.h>

int cal_digit(int n, int b)
{
    int i;
    double l;
    for (i = 2, l = 0; i <= n; i++)
        l += log10(i) / log10(b);
    return l + 1;
}

int cal_zero(int n, int b)
{
    int i, d, m, t;
    for (i = 2, d = 1; i <= b; i++) {
        m = 0;
        while (b % i == 0) {
            m++;
            d = i;
            b /= i;
        }
    }
    for (t = 0; n > 0; ) {
        t += n / d;
        n /= d;
    }
    return t / m;
}

int main(void)
{
    int n, b;
    while (scanf("%d%d", &n, &b) != EOF)
        printf("%d %d\n", cal_zero(n, b), cal_digit(n, b));
    return 0;
}
UVA

 

n! 在k进制下后缀0的个数。【洛谷 一道中档题】

输入输出格式
输入格式:
每组输入仅包含一行:两个整数n,k。

输出格式:
输出一个整数:n!在k进制下后缀0的个数。

输入输出样例
输入样例#1: 
10 40
输出样例#1: 
2
说明
对于20%的数据,n <= 1000000, k = 10

对于另外20%的数据,n <= 20, k <= 36

对于100%的数据,n <= 10^12,k <= 10^12

 

  

 


 

给出一个k进制的数n,求n!里一共有多少个0。【ZOJ Factorial Problem in Base K】

 https://www.cnblogs.com/linqiuwei/p/3258408.html 【好解释】

先把n转化为10进制下的数。

把n!分解质因数。

把k分解质因数。

求所有的k的质因数中,除以n!的相同质因数中最小的。就是answer。

  

 

How many zeros are there in the end of s! if both s and s! are written in base k which is not necessarily to be 10? For general base, the digit order is 0-9,A-Z,a-z(increasingly), for example F4 in base 46 is actually 694 in base 10,and f4 in base 46 is 1890 in base 10.

Input
There are multiple cases(less than 10000). Each case is a line containing two integers s and k(0 ≤ s < 2^63, 2 ≤ k ≤ 62).

Output
For each case, output a single line containing exactly one integer in base 10 indicating the number of zeros in the end of s!.

Sample Input
101 2
12 7
Sample Output
3
1

  

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)  
#define maxn 200001
#define mod 10007
#define eps 1e-9
int Num;
char CH[20];
//const int inf=0x7fffffff;   //нчоч╢С
const int inf=0x3f3f3f3f;
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
inline void P(int x)
{
    Num=0;if(!x){putchar('0');puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}
//**************************************************************************************

string s;
int n;
const int p[18]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61};
int a[30];
int main()
{
    while(cin>>s>>n)
    {
        memset(a,0,sizeof(a));
        ll tmp=0;
        ll k=1;
        for(int i=s.size()-1;i>=0;i--)
        {
            if(s[i]<='9'&&s[i]>='0')
                tmp+=(s[i]-'0')*k;
            else if(s[i]<='Z'&&s[i]>='A')
                tmp+=(s[i]-'A'+10)*k;
            else 
                tmp+=(s[i]-'a'+36)*k;
            k*=n;
        }
        for(int i=0;i<18;i++)
        {
            while(n%p[i]==0&&n>0)
            {
                n/=p[i];
                a[i]++;
            }
        }
        ll ans=(1LL<<63)-1;
        for(int i=0;i<18;i++)
        {
            ll now=tmp,tot=0;
            while(now>0)
            {
                now/=p[i];
                tot+=now;
            }
            if(a[i]>0)
                ans=min(ans,tot/a[i]);
        }
        printf("%lld\n",ans);
    }
    
}
ZOJ

 

转载于:https://www.cnblogs.com/Roni-i/p/8797918.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值