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 ...
随机推荐
- 【Bigdecimal】
---恢复内容开始--- 大位数除法的时候注意1/3问题:异常:[Exception in thread "main" java.lang.ArithmeticException: ...
- log4j记录日志 和 webAppRootKey关系
今天发现一个问题,就是后台从某天开始不再记录日志了,最后发现是 webAppRootKey 的 value 的值必须要和log4j的注入变量要一致. 如下:web.xml文件的 webAppRootK ...
- 关于asp.net假分页的删除操作的随笔
作为一个新人,上周负责优化一个后台管理系统,遇到一个问题:点击删除按钮之后,页面又回到了第一页. 而我需要达到的效果是:点击了删除按钮之后,原来是那一页,删除后还是在那一页. 由于项目是已经验收了的, ...
- EasyPusher推流类库的.NET调用说明
EasyPusher推流类库的.NET调用说明 以下内容基于在使用EasyPusher过程中遇到的问题,以及相应的注意事项.本文主要是基于对C++类库的二次封装(便于调试发现问题)以供C#调用以及对一 ...
- Ajax实现页面跳转与结果返回
ajax实现页面局部跳转与结果返回 1.带有结果返回的提交过程 这里用一个提交按钮来演示,HTML代码为: <input type="button" class=" ...
- 在linux命令行利用SecureCRT上传下载文件
一般来说,linux服务器大多是通过ssh客户端来进行远程的登陆和管理的,使用ssh登陆linux主机以后,如何能够快速的和本地机器进行文件的交互呢,也就是上传和下载文件到服务器和本地?与ssh有关的 ...
- Canvas中的save方法和restore方法
初学者也许会误认为canvas中save方法是用来保存绘图状态的图形,而restore方法是用来还原之前保存的绘图状态的图形,其实不然. save():保存当前的绘图状态. restore():恢复之 ...
- React Native之React速学教程(上)
概述 本篇为<React Native之React速学教程>的第一篇.本篇将从React的特点.如何使用React.JSX语法.组件(Component)以及组件的属性,状态等方面进行讲解 ...
- CentOS 7运维管理笔记(2)----修改命令提示符颜色
使用 su 命令切换到root用户: 使用 vi /etc/bashrc 命令插入如下代码: PS1="[\e[1;32m\u\e[m\e[1;33m@\e[m\e[1;35m\h\e[m ...
- kafka leader平衡策略
1.1个partition的默认leader是replicas中的第一个replica 2.kafka controller会启动一个定时的check线程,kafka默认是5min周期,mafka是3 ...