codeforces 1114C
题目连接 : https://codeforces.com/contest/1114/problem/C
题目大意:给一个整数n(1e18>=n>=0),和一个整数k(1e12>=k>=2),问n!在k进制情况下末尾有多少个0.
题目很好理解,思路也很有意思,首先n!在十进制下有多少个0,很好想,10分解质因数有2,5,无论在什么情况下n!中分解出5的个数比2多,
分解是这个意思,5能找出1个5,10能找出一个5,25能找出2个5(因为是5*5,可以被5除两次)。以此类推,找这个东西是log级的。
代码如下:
sum=;//分解出5的个数
d=;//因为要分解5所以是5
while(d<=n){
sum+=n/d;
d*=;
}
进一步推广,要分解k进制,首先k分解质因数,在这里有一个问题,我们只是单单的找质因数中最大的就可以么,这个不一定,我们不知道最大的质因数要
多少个能组成k,虽然说质因数中最大的一定是数量最小的之一,但组成k需要的个数可能会让其他数不够用,比如48,有4个2,和1个3,在4!情况下,有3个2和1个3
这时反而2不够用,那么我们就要记录所有的质因数和需要的个数,然后每个都去找,看看那个最小的就是末尾0 的个数
AC代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<math.h>
#include<vector>
#include<queue>
#include<map>
using namespace std;
typedef long long ll;
//map<int,int> mp;
#define INT_MAX 1e18;
typedef struct W_w{
ll shu;
ll ge;
}miao;
miao x[];
int main()
{
ll m,n;
scanf("%I64d %I64d",&m,&n);
int d=sqrt(n);
int ge=;
memset(x,,sizeof(x));
for(ll i=;i<=d;i++){
int flag=;
while(n%i==){
flag=;
x[ge].shu=i;
x[ge].ge++;
n/=i;
}
if(flag==) ge++;
}
if(n!=){
x[ge].shu=n;
x[ge].ge=;
ge++;
}
ll minn=INT_MAX;
//printf("%d\n",ge);
for(int i=;i<ge;i++){
ll dd=x[i].shu;
int ha=;
while(dd){
ha++;
dd/=;
}
dd=x[i].shu;
ll sum=;
while(dd<=m){
sum+=m/dd;
int haha=;
ll ddd=dd;
while(ddd){
ddd/=;
haha++;
}
if(haha+ha>) break;
dd*=x[i].shu;
}
minn=min(minn,sum/x[i].ge);
}
printf("%I64d",minn);
return ;
}
codeforces 1114C的更多相关文章
- Codeforces - 1114C - Trailing Loves (or L'oeufs?) - 简单数论
https://codeforces.com/contest/1114/problem/C 很有趣的一道数论,很明显是要求能组成多少个基数. 可以分解质因数,然后统计各个质因数的个数. 比如8以内,有 ...
- 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 ...
- Codeforces 1114C(数论)
题面 传送门 分析 我们先考虑n!在10进制下有多少个0 由于10=2*5, 我们考虑n!的分解式中5的指数,答案显然等于\(\frac{n}{5}+\frac{n}{5^2}+\frac{n}{5^ ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
随机推荐
- hdu 1729 Stone Game
Stone Game HDU - 1729 题意: 给定n个箱子,每个箱子的容量为si,每个箱子里最初有ci个石子,每次放入石子不能超过放入前的石子数的平方,谁无法继续放入石子就算输. /* 这是 ...
- php使用百度地图API
首先注册百度开发者平台账号,创建应用获取AK 不同的应用功能不同,一定要注意,没有的功能调用会提示APP被禁用 根据开发文档使用 给出例子:百度地图WEB api http://lbsyun.baid ...
- ELK系列(4) - Elasticsearch cannot write xcontent for unknown value of type class java.math.BigDecimal
问题与分析 在使用Elasticsearch进行index数据时,发现报错如下: java.lang.IllegalArgumentException: cannot write xcontent f ...
- 虚拟机上安装Cell节点(12.1.2.3.3)
安装介质下载 打开firefox,输入:https://edelivery.oracle.com 点击"Sign In",输入帐号.密码,登陆edelivery网站. ...
- shell学习(13)- vim
其中部分内容是转载的. 在命令状态下对当前行用== (连按=两次), 或对多行用n==(n是自然数)表示自动缩进从当前行起的下面n行.你可以试试把代码缩进任意打乱再用n==排版,相当于一般IDE里的c ...
- 提高mysql千万级大数据SQL查询优化30条经验
1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...
- 003 Longest Substring Without Repeating Characters 最长不重复子串
Given a string, find the length of the longest substring without repeating characters.Examples:Given ...
- 迅雷笔试题 (JAVA多线程)启动三个线程,分别打印A B C,现在写一个程序 循环打印ABCABCABC
题目:http://wenku.baidu.com/view/d66187aad1f34693daef3e8a.html 启动三个线程,分别打印A B C,现在写一个程序 循环打印ABCABCABC. ...
- CentOS6.5安装配置详解
1. 环境要求 VMWare软件: CentOS6.5对应的iso镜像文件(位数对应个人计算机位数). 2. 安装步骤 打开VMWare,文件->新建虚拟机(以下几步默认跳过即可) 命名和选择安 ...
- 初识MAC(由window到mac的转变适应)
* Windows上的软件可以拿到Mac上面安装吗? Windows上面的软件不能拿到Mac的操作系统上安装,除此之外,Windows里的 exe文件,在Mac下面也是无法运行的,要特別注意.如果你要 ...