题目链接:https://cn.vjudge.net/problem/

题意

找一个最小的正整数n

使得n!有a个零

思路

就是有几个因数10呗

考虑到10==2*5,也就是说找n!因数5有几个

数据量略大(N<=1e8),打表之类的O(N)算法是直接不可以

分析到这里,可能的算法也就是二分了

找了找很久规律,发现可以有O(log5(n))的方法确定n!的因数5的个数

于是有二分

代码

// binary search
// [f(m)<n, f(m)=n, f(m)>n]
// [l, r, r] (find min r)
// [l, l, r] (find max l)
//
// Attention:
// 1. make sure the initial value of l and r.
// 2. whlie (l<r-1).
// 3. l <= mid < r.
// 4. check if r(l) is the answer. #include <cstdio>
long long find(long long n){
long long ans=0;
while (n){
ans+=n/5; n/=5;
}return ans;
} long long search(long long n){
long long l=1, r=5e8;
while (l<r-1){
long long mid=(r+l)/2, m=find(mid);
if (m>=n) r=mid;
else l=mid;
}
if (find(r)==n) return r;
else return -1;
} int main(void){
int T; long long n; scanf("%d", &T);
for (int cnt=1; cnt<=T; cnt++){
scanf("%lld", &n);
long long ans=search(n);
if (ans>0) printf("Case %d: %lld\n", cnt, ans);
else printf("Case %d: impossible\n", cnt);
} return 0;
}
Time Memory Length Lang Submitted
20ms 1088kB 896 C++ 2018-05-17 18:18:26

LightOJ-1138 Trailing Zeroes (III) 唯一分解定理 算n!的某个因数个数的更多相关文章

  1. LightOJ 1138 Trailing Zeroes (III)(二分 + 思维)

    http://lightoj.com/volume_showproblem.php?problem=1138 Trailing Zeroes (III) Time Limit:2000MS     M ...

  2. LightOj 1138 - Trailing Zeroes (III) 阶乘末尾0的个数 & 二分

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1138 题意:给你一个数n,然后找个一个最小的数x,使得x!的末尾有n个0:如果没有输出 ...

  3. lightoj 1138 - Trailing Zeroes (III)【二分】

    题目链接:http://lightoj.com/volume_showproblem.php? problem=1138 题意:问 N. 末尾 0 的个数为 Q 个的数是什么? 解法:二分枚举N,由于 ...

  4. LightOj 1138 Trailing Zeroes (III)

    题目描述: 假设有一个数n,它的阶乘末尾有Q个零,现在给出Q,问n最小为多少? 解题思路: 由于数字末尾的零等于min(因子2的个数,因子5的个数),又因为2<5,那么假设有一无限大的数n,n= ...

  5. LightOJ 1138 Trailing Zeroes (III) 打表

    就是统计5,然后当时因为发现最多有8000w个5的倍数,然后8000w/100,是80w,打表,二分找 然后我看网上的都是直接二分找,真是厉害 #include <cstdio> #inc ...

  6. light oj 1138 - Trailing Zeroes (III)【规律&&二分】

    1138 - Trailing Zeroes (III)   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit:  ...

  7. Light oj 1138 - Trailing Zeroes (III) 【二分查找 &amp;&amp; N!中末尾连续0的个数】

    1138 - Trailing Zeroes (III) problem=1138"> problem=1138&language=english&type=pdf&q ...

  8. 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 ...

  9. 1138 - Trailing Zeroes (III) 二分

    1138 - Trailing Zeroes (III)   You task is to find minimal natural number N, so that N! contains exa ...

随机推荐

  1. addEventListener()与removeEventListener(),追加事件和删除追加事件

    addEventListener()与removeEventListener()用于追加事件和删除追加.所有的DOM节点中都包含这两种方法,并且它们都接受3个参数:要处理的事件名.作为事件处理程序的函 ...

  2. SpringMVC框架入门

    简介 SpringMVC采用模型(Model)-视图(View)-控制器(controller)的方法把业务逻辑.数据与界面显示分离.用通俗的话来讲,MVC的理念就是把数据处理.数据展示和程序/用户的 ...

  3. Java 面向对象详解

    0 引言 接触项目开发也有很长一段时间了,最近开始萌发出想回过头来写写以前学过的基础知识的想法. 1 面向对象 面向对象(Object Oriented)是一种新兴的程序设计方法,或者是一种新的程序设 ...

  4. 使用ajax实现搜索功能

      最近要做一个搜索功能,网上搜了一圈,终于做出来了,很简单的一个,这里分享我的方法,希望对大家有用,不足之处还请指教. 这里使用ajax提交数据,配合jquery将数据显示出来. 用jq的keyup ...

  5. SQLserver 导入超大CSV(100G以上)方法

    1.似乎SQLSERVER2008对UTF8不兼容,采用SQLSERVER20052.采用SQLSERVER2005,还是出现UTF8诸如此类的问题,修改表结构,varchar改成 nvarchar3 ...

  6. Linux系统串口接收数据编

    http://blog.csdn.net/bg2bkk/article/details/8668576 之前基于IBM deveplopworks社区的代码,做了串口初始化和发送的程序,今天在此基础上 ...

  7. HDU——T 1054 Strategic Game

    http://acm.hdu.edu.cn/showproblem.php?pid=1054 Time Limit: 20000/10000 MS (Java/Others)    Memory Li ...

  8. C++ throw的实验 & 异常类继承关系

    如果定义了 throw() 表示函数不抛出异常,这时候如果还是抛出,会导致运行时错误. #include <iostream> #include <exception> #in ...

  9. codeforces7D Palindrome Degree(manacher&amp;dp或Hsh&amp;dp)

    D. Palindrome Degree time limit per test 1 second memory limit per test 256 megabytes input standard ...

  10. [HTML5] Inlining images with SVG and data URIs

    The main reason you want to do I"nlining images with SVG and data URIs" is to reduce http ...