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 ...
随机推荐
- 优雅绝妙的Javascript跨域问题解决方案
关于Javascript跨域问题的解决方案已在之前的一片文章中详细说明,详见:http://blog.csdn.net/sfdev/archive/2009/02/13/3887006.aspx: 除 ...
- 【wikioi】1269 匈牙利游戏(次短路+spfa)
http://www.wikioi.com/problem/1269/ 噗,想不到.. 次短路就是在松弛的时候做下手脚. 设d1为最短路,d2为次短路 有 d1[v]>d1[u]+w(u, v) ...
- 在windows下使用gnu的工具
mingw Minimalist GNU for Windows http://www.mingw.org/ 想要在Windows使用wget,之前使用的是单独的安装包,现在又想使用bash的命令提示 ...
- HDU 2089 数位dp入门
开始学习数位dp...一道昨天看过代码思想的题今天打了近两个小时..最后还是看了别人的代码找bug...(丢丢) 传说院赛要取消 ? ... 这么菜不出去丢人也好吧~ #include<stdi ...
- 2016.07.08,英语,《Vocabulary Builder》Unit 24
mand/mend comes from mandare, Latin for 'entrust' or 'order'. command and commandment: [kə'mændmənt] ...
- 使用photoshop,把图片背景变成透明
鄙人使用的是photoshop CS6,win7系统,好了废话不多说,我们开始吧 1.打开photoshop,选择一个要编辑的图片 2.在右下角的图层面板上用鼠标左键快速双击背景图层为图片解锁 3.在 ...
- try...except 抛出错误
- WPF 最大化最小化窗口
public static void FullOrMin(this Window window) { //如果是全屏,则最小化 if (win ...
- zabbix 2.2.2在centos 6.3 x86_64上的安装
zabbix 2.2.2在centos 6.3 x86_64上的安装 更新五月 03, 2014 # 依赖环境 yum install -y php-mbstring mysql-deve ...
- CLI下另一种多进程实现方式----PCNTL
有些时候,你需要对一些脚本进行优化,以期跑的更快,在更短的时间内完成任务.PCNTL是一个不错的选择,它可以fork多个进程,来协同完成一个任务,理论上完成的时间将会和进程数成反比. 不过,PCNTL ...