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. 我们可以知 ...
随机推荐
- vue Iframe
1.Iframe.vue <!-- Iframe --> <template> <div> <!-- 标题栏 --> <mt-header tit ...
- Windows驱动程序开发基础(四)驱动的编译调试和安装
Windows驱动程序开发基础,转载标明出处:http://blog.csdn.net/ikerpeng/article/details/38793995 以下说一下开发出来驱动程序以后怎样编译.一般 ...
- hdu 3183 A Magic Lamp 贪心
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm& ...
- [LeetCode][Java] Remove Duplicates from Sorted List II
题意: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct ...
- C项目实践--图书管理系统(2)
前面在<<C项目实践-图书管理系统(1)>>中把系统中的三大功能模块中可能涉及到的常量,结构体及相关函数进行了声明定义,下来就来实现它们. 执行系统首先从登录到系统开始,所以首 ...
- 在VS2010下使用AppFace
AppFace的介绍网上一大堆,此文仅为自己作个记录,方便以后查看. 一.需要的文件:1.AppFace.h 2.appface.lib 3.appface.dll 4.macosx_af.urf ...
- intellij IDEA破解
方法1 填入下面的license server: http://intellij.mandroid.cn/ http://idea.imsxm.com/ http://idea.iteblog.com ...
- POJ - 2516 Minimum Cost(最小费用最大流)
1.K种物品,M个供应商,N个收购商.每种物品从一个供应商运送到一个收购商有一个单位运费.每个收购商都需要K种物品中的若干.求满足所有收购商需求的前提下的最小运费. 2.K种物品拆开来,分别对每种物品 ...
- lucene segment会包含所有的索引文件,如tim tip等,可以认为是mini的独立索引
A Lucene index segment can be viewed as a "mini" index or a shard. Each segment is a colle ...
- python-----实现微信撤回消息还原
有时候用微信聊天,好友会撤回一些聊天记录,我们好奇,但又没法看,以下代码就可以满足大家的好奇心. #!/usr/bin/env python # -*- coding: utf-8 -*- # @Ti ...