zoukankan      html  css  js  c++  java
  • Factorial Problem in Base K 质因数分解,然后求阶乘的质因数个数

    Description

    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
    ***********************************************************************************************************************************************************
    加个链接,很详细(阶乘的质因数求法)http://blog.csdn.net/hackbuteer1/article/details/6690015
    ***********************************************************************************************************************************************************
     1 #include<iostream>
     2 #include<string>
     3 #include<cstring>
     4 #include<cstdio>
     5 using namespace std;
     6 #define inf  0x7fffffff
     7 long long  a[1001],b[1001],ge;
     8 void init(long long kk)//对k进行质因数分解
     9 {
    10     long long  k=kk,it,jt;
    11     ge=0;
    12     for(it=2;it<=kk;it++)
    13     {
    14         if(k%it==0&&k>0)
    15         {
    16             ge++;
    17             jt=0;
    18             a[ge]=it;
    19             while(k%it==0)
    20             {
    21                 jt++;
    22                 k/=it;
    23             }
    24             b[ge]=jt;
    25         }
    26     }
    27 }
    28 int main()
    29 {
    30     char str1[1001];
    31     long long n,k,i,j;
    32     while(scanf("%s %lld",str1,&k)!=EOF)
    33     {
    34         init(k);
    35         long long len=strlen(str1);
    36         //cout<<"len:: "<<len<<endl;
    37         n=0;
    38         for(i=0;i<len;i++)
    39         {
    40             if(str1[i]>='0'&&str1[i]<='9')
    41               n=n*k+(str1[i]-'0');
    42             if(str1[i]>='A'&&str1[i]<='Z')
    43               n=n*k+(str1[i]-'A'+10);
    44             if(str1[i]>='a'&&str1[i]<='z')
    45               n=n*k+(str1[i]-'a'+36);
    46         }
    47         //cout<<"n:: "<<n<<endl;
    48         //cout<<"ge:: "<<ge<<endl;
    49         //for(i=1;i<ge;i++)
    50         //{
    51             //printf("a[%lld]: %lld   b[%lld]: %lld
    ",i,a[i],i,b[i]);
    52         //}
    53         long long min=-1,sum,m;
    54         for(i=1;i<=ge;i++)
    55         {
    56             sum=0;
    57             m=n;
    58             while(m>0)
    59             {
    60                 sum+=(m/a[i]);
    61                 m/=a[i];
    62             }
    63             if(min==-1)
    64                min=sum/b[i];
    65             else
    66             {
    67                 if(min>(sum/b[i]))
    68                   min=(sum/b[i]);
    69             }
    70         }
    71         printf("%lld
    ",min);
    72 
    73     }
    74 
    75 }
    View Code


  • 相关阅读:
    作业二 Git的安装与使用
    第一次作业
    字符串、文件操作,英文词频统计预处理
    了解大数据的特点、来源与数据呈现方式以及用Python写Mad Libs游戏
    第五次作业:结对项目-四则运算 “软件”之升级版
    第四次作业:个人项目-小学四则运算 “软件”之初版
    第3次作业:阅读《构建之法》1-5章
    分布式版本控制系统Git的安装与使用
    第一次作业-准备
    字符串操作,英文词频统计预处理
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3439464.html
Copyright © 2011-2022 走看看