LightOj 1138 - Trailing Zeroes (III) 阶乘末尾0的个数 & 二分
题目链接:http://lightoj.com/volume_showproblem.php?problem=1138
题意:给你一个数n,然后找个一个最小的数x,使得x!的末尾有n个0;如果没有输出impossible
可以用二分求结果,重点是求一个数的阶乘中末尾含有0的个数,一定和因子5和2的个数有关,因子为2的明显比5多,所以我们只需要求一个数的阶乘的因子中一共有多少个5即可;
LL Find(LL x)
{
LL ans = ;
while(x)
{
ans += x/;
x /= ;
}
return ans;
}
例如125! 125/5 = 25;所以125!中含5的项为25*5+24*5+23*5+...+3*5+2*5+1*5 = 25!*5
所以125!末尾有25+5+1 = 31 个0;
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
typedef long long LL;
#define N 1000001
using namespace std;
const double eps = 1e-; LL Find(LL x)
{
LL ans = ;
while(x)
{
ans += x/;
x /= ;
}
return ans;
} int main()
{
int T, t = , n;
scanf("%d", &T);
while(T --)
{
scanf("%d", &n);
LL ans = , L = , R = ;
while(L <= R)
{
LL Mid = (L+R)/;
LL ret = Find(Mid); if(ret == n)
ans = Mid;
if(ret >= n)
R = Mid - ;
else
L = Mid + ;
}
if(ans == )
printf("Case %d: impossible\n", t++);
else
printf("Case %d: %lld\n", t++, ans);
}
return ;
}
LightOj 1138 - Trailing Zeroes (III) 阶乘末尾0的个数 & 二分的更多相关文章
- LightOj 1090 - Trailing Zeroes (II)---求末尾0的个数
题目链接:http://lightoj.com/volume_showproblem.php?problem=1090 题意:给你四个数 n, r, p, q 求C(n, r) * p^q的结果中末尾 ...
- LightOJ 1138 Trailing Zeroes (III)(二分 + 思维)
http://lightoj.com/volume_showproblem.php?problem=1138 Trailing Zeroes (III) Time Limit:2000MS M ...
- [LeetCode] Factorial Trailing Zeroes 求阶乘末尾零的个数
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- [LeetCode] 172. Factorial Trailing Zeroes 求阶乘末尾零的个数
Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...
- lightoj 1138 - Trailing Zeroes (III)【二分】
题目链接:http://lightoj.com/volume_showproblem.php? problem=1138 题意:问 N. 末尾 0 的个数为 Q 个的数是什么? 解法:二分枚举N,由于 ...
- LightOj 1138 Trailing Zeroes (III)
题目描述: 假设有一个数n,它的阶乘末尾有Q个零,现在给出Q,问n最小为多少? 解题思路: 由于数字末尾的零等于min(因子2的个数,因子5的个数),又因为2<5,那么假设有一无限大的数n,n= ...
- 172. Factorial Trailing Zeroes(阶乘中0的个数 数学题)
Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...
- LightOJ 1138 Trailing Zeroes (III) 打表
就是统计5,然后当时因为发现最多有8000w个5的倍数,然后8000w/100,是80w,打表,二分找 然后我看网上的都是直接二分找,真是厉害 #include <cstdio> #inc ...
- Light oj 1138 - Trailing Zeroes (III) 【二分查找好题】【 给出N!末尾有连续的Q个0,让你求最小的N】
1138 - Trailing Zeroes (III) PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 ...
随机推荐
- BZOJ3827 : [Poi2014]Around the world
把环倍长,破环成链 设f[i]表示i一次性能飞达的最右边的点,因为f[]单调递增,所以可以$O(n)$求出 这样就形成了一个树结构,对于每个节点,在其到根节点路径上二分出深度最大的点,使得其飞过一圈 ...
- [BZOJ 2631]tree
裸LCT..QAQ写了三遍没写对 真是老了..QAQ 主要错的地方是 init: size[i] = sum[i] = val[i] = mul[i] = 1; pushdown: 注意判断左右儿子是 ...
- PHP将在对象被销毁前调用这个函数.它称为析构函数
-构造函数和析构函数 如果你在一个类中声明一个函数,命名为__construct,这个函数将被当成是一个构造函数并在建立一个对象实例时被执行.清楚地说,__是两个下划线.就像其它任何函数一样,构造函数 ...
- java 中的instanceof的用法
instanceof 运算符是Java.php的一个二元操作符(运算符),和==.>.<是同一类东西.由于它是由字母组成的,所以也是Java的保留关键字.它的作用是判断其左边对象是否为其右 ...
- HDU 1698 线段树 区间更新求和
一开始这条链子全都是1 #include<stdio.h> #include<string.h> #include<algorithm> #include<m ...
- [英语学习]国外的在线广播网站,类似喜马拉雅和荔枝FM
今天在Seattle Art Museum 网站上看东西,发现了这个好网站. 主要是外文资料,可以练听力,也可以找到<我爱吕西安>的英文版本. https://soundcloud.co ...
- [ZZ] HD7970GE vs GTX770
AMD/NV烽烟再起!HD7970GE大战GTX770 泡泡网显卡频道7月8日 高端市场肩负展示厂商实力,树立品牌形象的任务,历来是兵家必争之地.从GTX680 VS HD7970,HD7970GE ...
- mysql字段额外属性,除去字段类型外的其他属性
如果你不想字段为 NULL 可以设置字段的属性为 NOT NULL, 在操作数据库时如果输入该字段的数据为NULL ,就会报错. AUTO_INCREMENT定义列为自增的属性,一般用于主键,数值会自 ...
- MySql练习+加源代码
一.设有一个数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher). 四个表的结构分别如表1-1的表(一)~表(四)所示,数据如表1 ...
- Natural Language Processing Computational Linguistics
http://www.nltk.org/book/ch00.html After this, the pace picks up, and we move on to a series of chap ...