CF#538(div 2) C. Trailing Loves (or L'oeufs?) 【经典数论 n!的素因子分解】
任意门:http://codeforces.com/contest/1114/problem/C
C. Trailing Loves (or L'oeufs?)
2 seconds
256 megabytes
standard input
standard output
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).
The only line of the input contains two integers nn and bb (1≤n≤10181≤n≤1018, 2≤b≤10122≤b≤1012).
Print an only integer — the number of trailing zero digits in the bb-ary representation of n!n!
6 9
1
38 11
3
5 2
3
5 10
1
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.
题意概括:
求 n! 转化为 b 进制下末尾有多少个 0.
解题思路:
原题:swjtuOJ 2090
这是一道很好的数论题。
首先转换一下思路:
要求 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) ] ...
学习:
https://blog.csdn.net/Wen_Yongqi/article/details/86976902
AC code:
#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+;
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 = ; i <= len; i++){
if(n%i == ){
prime.push_back(i);
while(n%i == ){
n/=i;
//num[prime.size()-1]++;
mmp[i]++;
}
}
}
if(n != ){
prime.push_back(n);
// num[prime.size()-1]++;
mmp[n]++;
}
} LL calc(LL n, LL p)
{
LL res = ;
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<<);
for(LL i = ; 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 ;
}
CF#538(div 2) C. Trailing Loves (or L'oeufs?) 【经典数论 n!的素因子分解】的更多相关文章
- 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 ...
- Codeforces - 1114C - Trailing Loves (or L'oeufs?) - 简单数论
https://codeforces.com/contest/1114/problem/C 很有趣的一道数论,很明显是要求能组成多少个基数. 可以分解质因数,然后统计各个质因数的个数. 比如8以内,有 ...
- CF 1114 C. Trailing Loves (or L'oeufs?)
C. Trailing Loves (or L'oeufs?) 链接 题意: 问n!化成b进制后,末尾的0的个数. 分析: 考虑十进制的时候怎么求的,类比一下. 十进制转化b进制的过程中是不断mod ...
- C. Trailing Loves (or L'oeufs?) (质因数分解)
C. Trailing Loves (or L'oeufs?) 题目传送门 题意: 求n!在b进制下末尾有多少个0? 思路: 类比与5!在10进制下末尾0的个数是看2和5的个数,那么 原题就是看b进行 ...
- CF#538 C - Trailing Loves (or L'oeufs?) /// 分解质因数
题目大意: 求n!在b进制下末尾有多少个0 https://blog.csdn.net/qq_40679299/article/details/81167283 一个数在十进制下末尾0的个数取决于10 ...
- Trailing Loves (or L'oeufs?)
The number "zero" is called "love" (or "l'oeuf" to be precise, literal ...
- C. Trailing Loves (or L'oeufs?)
题目链接:http://codeforces.com/contest/1114/problem/C 题目大意:给你n和b,让你求n的阶乘,转换成b进制之后,有多少个后置零. 具体思路:首先看n和b,都 ...
- 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?)
[链接] 我是链接,点我呀:) [题意] 问你n!的b进制下末尾的0的个数 [题解] 证明:https://blog.csdn.net/qq_40679299/article/details/8116 ...
随机推荐
- MdiContainer
/// <summary> /// 显示form /// </summary> /// <param name="form">要显示的form& ...
- [转]微信小程序开发:http请求
本文转自:http://www.cnblogs.com/dragondean/p/5921079.html 在微信小程序进行网络通信,只能和指定的域名进行通信,微信小程序包括四种类型的网络请求. 普通 ...
- docker安装Ghost博客
1.安装docker-compose curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose ...
- jsp、servlet笔记
1.init 初始化Jsp&Servlet方法 destroy 销毁Jsp&Servlet之前的方法 service 对用户请求生成响应的方法2.Jsp文件必须在jsp服 ...
- 四 Scatter/Gather
scatter/gather用于描述从Channel中读取或者写入到Channel的操作. 分散(scatter):从Channel中读取在读操作中将读取的数据写入多个Buffer中.因此,Chann ...
- eclipse切换workspace后配置问题
正常情况下如果切换了eclipse的workspace后,需要重新配置eclipse,但是可以将原工作目录中的.metadata/.plugins/org.eclipse.core.runtime拷贝 ...
- Jvm性能监控和常用工具
JDK常用命令行工具 Jps : jps [options] [hostid] , -q 只显示jvmid, -m 传递给主类main的参数,-l 类全名,-v jvm启动参数 jstat : ...
- python 2.7支持中文
在代码的第一行加上#coding=utf-8 return render_template('index.html',message=u"小明小明")print u'你要打印的字符 ...
- Yii2.0 新建项目通用准备工作
1.设置 cookieValidationKey 在 config/web.php 中 config 里有 components项中request有个cookieValidationKey需要配置参数 ...
- Android碎笔录2——按键的点击变色和圆角实现
android的Button默认写出来之后都是方形的直角,并且点击感很不明显,只要在drawable中加上一个xml就能解决这个问题: <?xml version="1.0" ...