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$进制下有多少后导零. 寒假好像写过这道题当时好像完 ...
随机推荐
- 696. Count Binary Substrings统计配对的01个数
[抄题]: Give a string s, count the number of non-empty (contiguous) substrings that have the same numb ...
- IP协议、ARP协议等之温故知新
今天才知道: 1.IP协议的固定部分长度为20字节.(貌似有一家运维工程师面试我的时候,问过我这个问题呢.) 2.IP数据包首部中的协议?? 答:协议:占8位,指出此数据报携带的数据使用何种协议以便目 ...
- 洛谷P2569 [SCOI2010]股票交易
P2569 [SCOI2010]股票交易 题目描述 最近lxhgww又迷上了投资股票,通过一段时间的观察和学习,他总结出了股票行情的一些规律. 通过一段时间的观察,lxhgww预测到了未来T天内某只股 ...
- aspx页面上输出xml的问题
在aspx页面上输出xml,需要在后台输出编码格式和编码类型,如下代码: Response.Charset = "utf-8";//格式Response.ContentType = ...
- Charles常见问题
Charles常见问题汇总 Charles是一款很好用的抓包修改工具,但是如果你不是很熟悉这个工具的话,肯定会遇到各种感觉很莫名其妙的状况,这里就来帮你一一解答下面再说说charles的一些其他常用的 ...
- HDU 3723 Delta Wave (高精度+calelan数)
题意:给定一个图,问你只能向上向下,或者平着走,有多少种方法可以走到最后一个格. 析:首先先考虑,如果没有平的情况就是calelan数了,现在有平的情况,那么就枚举呗,因为数很大,所以要用高精度. 答 ...
- UIWebView分页显示
問題:使用iOS UIWebView時,載入本地html檔案,但是該檔案太大,結果螢幕畫面形成一長條型顯示,雖然用滾動畫面可以看見整個html檔案,但是滑來滑去,不好用. 目標:用UIWebView載 ...
- Socket编程(c语言示例)
转自:http://blog.csdn.net/dxpqxb/article/details/8166423 前言 Socket可以看成在两个程序进行通讯连接中的一个端点,是连接应用程序和网络驱动程序 ...
- CentOS下配置防火墙 配置nat转发服务
CentOS下配置iptables防火墙 linux NAT(iptables)配置 CentOS下配置iptables 1,vim /etc/sysconfig/network 这里可以更改主机 ...
- WPF 控件库——轮播控件
WPF 控件库系列博文地址: WPF 控件库——仿制Chrome的ColorPicker WPF 控件库——仿制Windows10的进度条 WPF 控件库——轮播控件 WPF 控件库——带有惯性的Sc ...