codeforces538C.Trailing Loves (or L'oeufs?)

The number “zero” is called “love” (or “l’oeuf” to be precise, literally means “egg” in French), for example when denoting the zero score in a game of tennis.

Aki is fond of numbers, especially those with trailing zeros. For example, the number 9200 has two trailing zeros. Aki thinks the more trailing zero digits a number has, the prettier it is.

However, Aki believes, that the number of trailing zeros of a number is not static, but depends on the base (radix) it is represented in. Thus, he considers a few scenarios with some numbers and bases. And now, since the numbers he used become quite bizarre, he asks you to help him to calculate the beauty of these numbers.

Given two integers n and b (in decimal notation), your task is to calculate the number of trailing zero digits in the b-ary (in the base/radix of b) representation of n! (factorial of n!).

Input
The only line of the input contains two integers n
and b (1≤n≤1e18, 2≤b≤1e12).

Output
Print an only integer — the number of trailing zero digits in the b-ary representation of n!

Examples
Input

6 9

Output

1

Input

38 11

Output

3

Input

5 2

Output

3

Input

5 10

Output

1

题意:求n!阶乘的b进制表示中,有多少0在末尾。即从最低位起,第一个不为0的位置是第几位。
在解决题目之前,首先需要知道两个数论的知识。
首先是算术基本定理(截图自百度百科)
在这里插入图片描述
和阶乘的标准分解式(截图自论文1007-9831 (2014)04-0028-03)
在这里插入图片描述
以 n=5,b=2 为例:
5!=5* 4* 3* 2* 1=(5*3)*8
根据上式可知,5!的二进制表示可视为将15的二进制左移3位,故末尾自然会有3个0.
所以可将原问题进一步转化为 ,求n!中存在b的r次方(记为pow(b,r)),r为未知数
再依据之前提到的分解方法,不难看出这道题的解法

  1. 求b的质因数及其对应的幂次
  2. 求n!中与b共有质因数的幂次
  3. 求n!幂次与b幂次比值的最小值

注意数据的范围以及过程的范围。n!幂次可能超出 int 范围,b幂次不会超出 int 的范围。pow(pi,j)(见阶乘标准分解式)可能超出long long 范围


#include <stdio.h>
#include <climits>
#include <cstring>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <vector>
#include <string>
#define INF 0x3f3f3f3f
#define ll long long
#define re return
#define Pair pair<int,int>
#define Make(a,b) make_pair(a,b)
#define Push(num) push_back(num)
#define rep(index,star,finish) for(register int index=star;index<finish;index++)
#define drep(index,finish,star) for(register int index=finish;index>=star;index--)
using namespace std;

vector<ll> factor;
vector<ll> x,y;
int main(){
    ios::sync_with_stdio(false);
    ll n,b;
    cin>>n>>b;

    ll power;
    //prime factorization of b
    for(ll i=2;i<=sqrt(b);i++){
        if(b%i){
            continue;
        }else{
            factor.Push(i);
            power=0;
            while(! (b%i) ){
                power++;
                b/=i;
            }
            x.Push(power);
        }
    }
    if(b!=1){
        factor.Push(b);
        x.Push(1);
    }
    

    ll ele,temp;;
    int len=factor.size();
    //prime factorization of n
    rep(i,0,len){
        ele=factor[i];
        power=0;
        temp=1;
        while(n/ele>=temp){
            temp*=ele;
            power+=n/temp;
        }
        y.Push(power);
    }

    ll ans=1000000000000000000LL;
    //finding the maximum r that n! is divisible by pow(b,r)
    rep(i,0,len){
        ans=min(ans,y[i]/x[i]);
    }
    cout<<ans<<endl;
    re 0;
}

猜你喜欢

转载自blog.csdn.net/white_156/article/details/87535422