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≤10^18 , 2<=b<=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
Note
In the first example, 6!(10)=720(10)=880(9).
In the third and fourth example, 5!(10)=120(10)=1111000(2).
The representation of the number x in the b-ary base is d1,d2,…,dk if x=d1bk−1+d2bk−2+…+dkb0, where di are integers and 0≤di≤b−1. For example, the number 720 from the first example is represented as 880(9) since 720=8⋅92+8⋅9+0⋅1.
思路:把b分解质因数,然后看对n!献出了多少贡献,即(n!%(a1^k+a2^k......)==0
我们需要去求k,就需要先把b分解,并且记录下它的质因子的指数数,然后用n进行迭代求,然后每次缩小一次指数,最后除本身的指数就ok了,注意minn开的一定要尽可能的大
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<cmath>
typedef long long ll;
using namespace std;
ll cnt=0;
ll num[4000005];
void primeFactor(ll n) {
while(n % 2 == 0) {
num[cnt++]=2;
n /= 2;
}
for(ll i = 3; i <= sqrt(n); i += 2) {
while(n % i == 0) {
num[cnt++]=i;
n /= i;
}
}
if(n > 2)
num[cnt++]=n;
}
int main() {
ll n,b;
ll ans;
ll sss;
scanf("%lld%lld",&n,&b);
primeFactor(b);
ll s;
ll ss;
ll k=1;
ll minn=999999999999999999;
for(int t=0; t<cnt; t++) {
if(num[t]!=num[t+1]) {
s=0;
ans=n;
ss=num[t];
while(ans>=ss) {
s+=(ans/ss);
ans/=ss;
}
minn=min(minn,s/k);
k=1;
} else {
k++;
}
// cout<<num[t]<<endl;
}
printf("%lld",minn);
return 0;
}
Trailing Loves (or L'oeufs?)的更多相关文章
- CF 1114 C. Trailing Loves (or L'oeufs?)
C. Trailing Loves (or L'oeufs?) 链接 题意: 问n!化成b进制后,末尾的0的个数. 分析: 考虑十进制的时候怎么求的,类比一下. 十进制转化b进制的过程中是不断mod ...
- CF#538(div 2) C. Trailing Loves (or L'oeufs?) 【经典数论 n!的素因子分解】
任意门:http://codeforces.com/contest/1114/problem/C C. Trailing Loves (or L'oeufs?) time limit per test ...
- C. Trailing Loves (or L'oeufs?) (质因数分解)
C. Trailing Loves (or L'oeufs?) 题目传送门 题意: 求n!在b进制下末尾有多少个0? 思路: 类比与5!在10进制下末尾0的个数是看2和5的个数,那么 原题就是看b进行 ...
- C. Trailing Loves (or L'oeufs?)
题目链接:http://codeforces.com/contest/1114/problem/C 题目大意:给你n和b,让你求n的阶乘,转换成b进制之后,有多少个后置零. 具体思路:首先看n和b,都 ...
- Codeforces Round #538 (Div. 2) C. Trailing Loves (or L'oeufs?) (分解质因数)
题目:http://codeforces.com/problemset/problem/1114/C 题意:给你n,m,让你求n!换算成m进制的末尾0的个数是多少(1<n<1e18 ...
- Trailing Loves (or L'oeufs?) CodeForces - 1114C (数论)
大意: 求n!在b进制下末尾0的个数 等价于求n!中有多少因子b, 素数分解一下, 再对求出所有素数的最小因子数就好了 ll n, b; vector<pli> A, res; void ...
- Codeforces - 1114C - Trailing Loves (or L'oeufs?) - 简单数论
https://codeforces.com/contest/1114/problem/C 很有趣的一道数论,很明显是要求能组成多少个基数. 可以分解质因数,然后统计各个质因数的个数. 比如8以内,有 ...
- 【Codeforces 1114C】Trailing Loves (or L'oeufs?)
[链接] 我是链接,点我呀:) [题意] 问你n!的b进制下末尾的0的个数 [题解] 证明:https://blog.csdn.net/qq_40679299/article/details/8116 ...
- Codeforces1114C Trailing Loves (or L'oeufs?)
链接:http://codeforces.com/problemset/problem/1114/C 题意:给定数字$n$和$b$,问$n!$在$b$进制下有多少后导零. 寒假好像写过这道题当时好像完 ...
随机推荐
- Tensorflow训练结果测试
代码参考(https://blog.csdn.net/disiwei1012/article/details/79928679) import osimport sysimport randomimp ...
- Centos里没有lsb_release
查看Centos操作系统版本,输入指令 lsb_release -a 报无此命令 解决办法,安装lsb_release 1.执行指令:yum install -y redhat-lsb 2.安装完毕后 ...
- Django--form验证及错误处理
需求 from表单验证和ajax验证时返回的错误信息处理 速查 1.form表单提交时错误信息显示 views 1 2 error = form表单实例化对象.errors return render ...
- HDU 3729 I'm Telling the Truth (二分匹配)
题意:给定 n 个人成绩排名区间,然后问你最多有多少人成绩是真实的. 析:真是没想到二分匹配,....后来看到,一下子就明白了,原来是水题,二分匹配,只要把每个人和他对应的区间连起来就好,跑一次二分匹 ...
- vmware10中arch使用遇到的一些问题
xfce4中安装中文输入法fcitx step 1 #pacman -S fcitx-im setp 2 在~/.xinitrc中添加如下内容: export LC_ALL=zh_CN.UTF-8e ...
- Java java.lang.Thread#join()方法分析
结论:A 线程调用 B 线程对象的 join 方法,则 A 线程会被阻塞,直到 B 线程 挂掉 (Java Doc 原话: Watis for this thread to die). 一.分析 查看 ...
- C# 中关于汉字与16进制转换的代码
/// <summary> /// 从汉字转换到16进制 /// </summary> /// <param name="s"></par ...
- Castle Windsor 注册组件
1.逐个注册组件即对每个接口通过代码指定其实现类,代码: container.Register( Component.For<IMyService>() //接口 .Implemented ...
- 关于命名空间 namespace的总结
namespace 有作用的类型 类.函数.常量关键字namespace必须在所有代码之前 除用于编码的declare语句 namespace Myproject; const A = 1; cla ...
- 通过python的paramiko模块获取cisco交换机的配置
import paramiko import time import os import threading,Queue class MyExpection(Exception): pass clas ...