LightOj 1138 Trailing Zeroes (III)
题目描述:
假设有一个数n,它的阶乘末尾有Q个零,现在给出Q,问n最小为多少?
解题思路:
由于数字末尾的零等于min(因子2的个数,因子5的个数),又因为2<5,那么假设有一无限大的数n,n=2^x=5^y,可知x<<y。
所以我们可以直接根据因子5的个数,算阶乘末尾的零的个数。1<=Q<=10^8,所以可以用二分快速求解。
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
#define LL long long
LL find_zeros (LL n)
{
LL ans = ;
while (n)
{
ans += n / ;
n /= ;
}
return ans;
}
int main ()
{
LL t, n, l = ;
scanf ("%lld", &t);
while (t --)
{
scanf ("%lld", &n);
LL low, high, mid;
low = ;
high = ;
while (low <= high)
{
mid = (low + high) / ;
LL num = find_zeros(mid);
if (num < n)
low = mid + ;
if (num >= n)//求出来的n要是最小的,所以这里不能直接返回
high = mid - ;
}
if (find_zeros(low) == n)
printf ("Case %lld: %lld\n", l++, low);
else
printf ("Case %lld: impossible\n", l++);
}
return ;
}
LightOj 1138 Trailing Zeroes (III)的更多相关文章
- LightOJ 1138 Trailing Zeroes (III)(二分 + 思维)
http://lightoj.com/volume_showproblem.php?problem=1138 Trailing Zeroes (III) Time Limit:2000MS M ...
- LightOj 1138 - Trailing Zeroes (III) 阶乘末尾0的个数 & 二分
题目链接:http://lightoj.com/volume_showproblem.php?problem=1138 题意:给你一个数n,然后找个一个最小的数x,使得x!的末尾有n个0:如果没有输出 ...
- lightoj 1138 - Trailing Zeroes (III)【二分】
题目链接:http://lightoj.com/volume_showproblem.php? problem=1138 题意:问 N. 末尾 0 的个数为 Q 个的数是什么? 解法:二分枚举N,由于 ...
- LightOJ 1138 Trailing Zeroes (III) 打表
就是统计5,然后当时因为发现最多有8000w个5的倍数,然后8000w/100,是80w,打表,二分找 然后我看网上的都是直接二分找,真是厉害 #include <cstdio> #inc ...
- light oj 1138 - Trailing Zeroes (III)【规律&&二分】
1138 - Trailing Zeroes (III) PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...
- Light oj 1138 - Trailing Zeroes (III) 【二分查找 && N!中末尾连续0的个数】
1138 - Trailing Zeroes (III) problem=1138"> problem=1138&language=english&type=pdf&q ...
- 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 ...
- 1138 - Trailing Zeroes (III) 二分
1138 - Trailing Zeroes (III) You task is to find minimal natural number N, so that N! contains exa ...
- Light oj 1138 - Trailing Zeroes (III) (二分)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1138 题目就是给你一个数表示N!结果后面的0的个数,然后让你求出最小的N. 我们可以知 ...
随机推荐
- sql 语句哪里添加单引号问题
1.sql 语句哪里添加单引号问题,哪些地方必须加双引号,否则sql语句会报错? :涉及varchar的值的时候,必须有单引号包括varchar值.int等其他字段类型,则不需要加单引号包括. 如: ...
- kvm虚拟化学习笔记(一)之kvm虚拟化环境安装
平时一直玩RHEL/CentOS/OEL系列的操作,玩虚拟化也是采这一类系统,kvm在RHEL6系列操作系统支持比较好,本文采用采用OEL6.3操作系统,网上所有文章都说KVM比xen简单,我怎么感觉 ...
- 学习MarkDown--初体验
学习MarkDownPad 突然兴趣来了,在博客园里面也有Markdown的格式,昨天晚上安装了MarkDownPad pro这个是免费的,但是有些功能不支持.本来想破解的,百度了很多方法感觉不靠谱, ...
- Android拍照、摄像方向旋转的问题 代码具体解释
近期做了个拍照.摄像的应用.遇到了拍照.摄像的图像相对于现实.翻转了90度.原因:相机这个硬件的角度是横屏的角度,所以会出现都是横屏的. 1.照相.摄影预览图像的正确角度显 示: public sta ...
- Deepin-安装和卸载软件
一般默认厂商源安装软件 安装软件: 示例:sudo apt-get install xx 实例:sudo apt-get install nodejs 卸载软件: 示例:sudo apt-get -- ...
- poj1904 二分图匹配+强连通分量
http://poj.org/problem?id=1904 Description Once upon a time there lived a king and he had N sons. An ...
- Redis实践系列丨Codis数据迁移原理与优化
Codis介绍 Codis 是一种Redis集群的实现方案,与Redis社区的Redis cluster类似,基于slot的分片机制构建一个更大的Redis节点集群,对于连接到codis的Redis客 ...
- 使用 C# 开发智能手机软件:推箱子(三)
这是"使用 C# 开发智能手机软件:推箱子"系列文章的第三篇.在这篇文章中,介绍 Common/Block.cs 源程序文件. 1 namespace Skyiv.Ben.Pu ...
- jquery源码学习笔记三:jQuery工厂剖析
jquery源码学习笔记二:jQuery工厂 jquery源码学习笔记一:总体结构 上两篇说过,query的核心是一个jQuery工厂.其代码如下 function( window, noGlobal ...
- CSS的两个class选择器紧挨在一起
有一段HTML代码: <a class="glyphicons white no-js cogwheel" href="#" target="_ ...