Codeforces 1114C Trailing Loves (or L'oeufs?)

版权声明:如果转载,请注明出处。 https://blog.csdn.net/S_999999/article/details/89441721

C. Trailing Loves (or L'oeufs?)

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

扫描二维码关注公众号,回复: 6149217 查看本文章

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 \le n \le 10^{18}$$$, $$$2 \le b \le 10^{12}$$$).

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.

思路:

要求 n! 在 b 进制下有多少个尾 0 就相当于 求 n! % (b^k) == 0 的最大 k。

那么我们现在把 n! 看作一个数 A。问题就是 求 A % (b^k) == 0 的最大 k;

我们知道有素数分解定理: b = p1^a1 * p2^a2 * p3^a3 ...;

那么我们如果可以求得 A 里面 p1^b1 * p2^b2 * p3^b3 ...  的 b1, b2, b3...

那么答案 ans = min(ans, ai/bi ) 了也就是要整除,首先要满足最小的那个能整除。

 

(1)首先对 b 进行素因子分解,直接暴力(log b), 用一个数组离散化形成该素因子的编号和该素因子的幂的映射 或者 用map存储该素因子的幂,得到所有素因子以及素因子的幂 

(2)对于每一个素因子p,计算对应的 A(即 n! ) 中素因子p的幂,两者相除取所有p幂的最小值就是对应的最大整数。

 

这里求 n! 下 素因子 p 的幂 用累除法,因为存在推论:

n! 下 p 的幂 = [ n/p ] + [ n/(p^2) ] + [ n/(p^3) ]  ...

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
#include<set>
#include<map>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MAXN = 2e6+1000;
LL N, B;
vector<LL>prime;
//LL num[MAXN];
map<LL, int>mmp;
void get_p(LL n)
{
    LL len = sqrt((double)n);
    //printf("len %lld\n", len);
    for(LL i = 2; i <= len; i++){
        if(n%i == 0){
            prime.push_back(i);
            while(n%i == 0){
                n/=i;
                //num[prime.size()-1]++;
                mmp[i]++;
            }
        }
    }
    if(n != 1){
        prime.push_back(n);
//        num[prime.size()-1]++;
        mmp[n]++;
    }
}

LL calc(LL n, LL p)
{
    LL res = 0;
    while(n){
        res += n/p;
        n/=p;
        //puts("zjy");
    }
    return res;
}

int main()
{
    LL sum = 1LL;
    scanf("%I64d %I64d", &N, &B);
    get_p(B);
    LL ans = (1LL<<62);
    for(LL i = 0; i < prime.size(); i++){
//        ans = min(ans, calc(N, prime[i])/num[i]);
        //puts("zjy");
        ans = min(ans, calc(N, prime[i])/mmp[prime[i]]);
    }

    printf("%I64d\n", ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/S_999999/article/details/89441721