[分解质因数/阶乘素因子的幂] C. Trailing Loves (or L'oeufs?) CF1114C

本文探讨了在不同进制下计算阶乘尾部零的数量,这是一个数学与编程结合的问题,涉及质因数分解和进制转换等概念,通过算法优化,实现了高效计算。
部署运行你感兴趣的模型镜像

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 92009200 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 nn and bb (in decimal notation), your task is to calculate the number of trailing zero digits in the bb-ary (in the base/radix of bb) representation of n!n! (factorial of nn).

Input

The only line of the input contains two integers nn and bb (1≤n≤10181≤n≤1018, 2≤b≤10122≤b≤1012).

Output

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

Examples

input

Copy

6 9

output

Copy

1

input

Copy

38 11

output

Copy

3

input

Copy

5 2

output

Copy

3

input

Copy

5 10

output

Copy

1

Note

In the first example, 6!(10)=720(10)=880(9)6!(10)=720(10)=880(9).

In the third and fourth example, 5!(10)=120(10)=1111000(2)5!(10)=120(10)=1111000(2).

The representation of the number xx in the bb-ary base is d1,d2,…,dkd1,d2,…,dk if x=d1bk−1+d2bk−2+…+dkb0x=d1bk−1+d2bk−2+…+dkb0, where didi are integers and 0≤di≤b−10≤di≤b−1. For example, the number 720720 from the first example is represented as 880(9)880(9) since 720=8⋅92+8⋅9+0⋅1720=8⋅92+8⋅9+0⋅1.

You can read more about bases here.

#include <bits/stdc++.h>
#define ll long long
#define db double
using namespace std;

const int mn = 1e6;
const ll inf = 1e18;

int cnt;
bool prime[mn + 10];
ll pm[mn + 10];
void Prime()    /// 素数筛
{
    for (int i = 1; i <= mn; i++)
        prime[i] = 1;
    for (int i = 2; i <= mn; i++)
    {
        if (prime[i] == 0)
            continue;
        pm[++cnt] = i;
        for (int j = 2; i * j <= mn; j++)
            prime[i * j] = 0;
    }
}
ll ling;
map<ll, ll> mp;
void find_prime(ll x)   /// 分解质因数 
{
    for (int i = 1; i <= cnt && pm[i] * pm[i] <= x; i++)
    {
        while (x % pm[i] == 0)
        {
            x /= pm[i];
            mp[pm[i]]++;    // 每个质因数数量
        }
    }
    if (x > 1)  // 大于根号x的因数只会有一次幂
        ling = x;
}

int main()
{
    ll n, b;
    cin >> n >> b;
    
    Prime();
    find_prime(b);
    
    ll ans = inf;
    for (int i = 1; i <= cnt; i++)
    {
        if (mp[pm[i]] == 0)
            continue;
        ll t = 0;
        for (ll j = pm[i]; j <= n; j *= pm[i])
        {
            t += n / j;
            if ((db)j > n / pm[i])
                break;
        }
        ans = min(ans, t / mp[pm[i]]);
    }
    
    if (ling != 0)
    {
        ll t = 0;
        for (ll j = ling; j <= n; j *= ling)
        {
            t += n / j;
            if ((db)j > n / ling)
                break;
        }
        ans = min(ans, t);
    }
    cout << ans << endl;
    
    return 0;
}

 

您可能感兴趣的与本文相关的镜像

AutoGPT

AutoGPT

AI应用

AutoGPT于2023年3月30日由游戏公司Significant Gravitas Ltd.的创始人Toran Bruce Richards发布,AutoGPT是一个AI agent(智能体),也是开源的应用程序,结合了GPT-4和GPT-3.5技术,给定自然语言的目标,它将尝试通过将其分解成子任务,并在自动循环中使用互联网和其他工具来实现这一目标

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值