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 ...
随机推荐
- 获取用户Ip地址通用方法常见安全隐患(HTTP_X_FORWARDED_FOR)
分析过程 这个来自一些项目中,获取用户Ip,进行用户操作行为的记录,是常见并且经常使用的. 一般朋友,都会看到如下通用获取IP地址方法. function getIP() { if (isset($_ ...
- wms-ssv数据字典
--------------------------------------------以下,托盘-- dbo.Container --托盘 , "托盘状态", "Con ...
- storm之topology的启动
一个topology的启动包括了三个步骤 1)创建TopologyBuilder,设置输入源,输出源 2)获取config 3)提交topology(这里不考虑LocalCluster本地模式) 以s ...
- MySQL 8.0 压缩包版安装方法
转自:https://blog.csdn.net/yangs_2012/article/details/80412016 注意: 操作系统:Windows 10 专业版(64位) MySQL版本:my ...
- js 去除空格回车换行
开发中遇到特殊需求,需要将空格回车换行替换成逗号 直接使用正则表达式解决: 变量.replace(/\s+/g,","); 如果想要去除则将后面逗号变成空就OK了.
- 二、redis的配置
# redis 配置文件示例 # 当你需要为某个配置项指定内存大小的时候,必须要带上单位, # 通常的格式就是 1k 5gb 4m 等酱紫: # # 1k => 1000 bytes # 1kb ...
- 第一行代码 10.2使用HTTP协议访问网络 HttpURLConnection代码中的问题
实现HttpURLConnection代码的时候,遇到了问题. 怎样点击途中Send Request按钮,没有任何改变. 最后将MainActivity中的一段代码URL url = new URL( ...
- ThreadLocal 理解
主要方法 public void set(T value); public T get(); private T setInitialValue(); public void set(T value) ...
- RN记录
react-native run-android 出现 java.lang.nullpointerexception(no error message) 错误 删除 工程目录\android.grad ...
- 通过JTS源码分析Rtree(未完待续)
前言 R树在数据库等领域做出的功绩是非常显著的.它很好的解决了在高维空间搜索等问题.它把B树的思想很好的扩展到了多维空间,采用了B树分割空间的思想,并在添加.删除操作时采用合并.分解结点的方法,保证树 ...