SGU 154.Factorial
时间限制:0.25s
空间限制:4M
题意
你的任务是找到最小自然数 N, 使N!在十进制下包含 Q个零. 众所周知 N! = 1*2*...*N. 例如, 5! = 120, 120 结尾包含1个零.
Input
一个数 Q (0<=Q<=10^8).
Output
如果无解,输出"No solution" , 否则输出 N .
Sample test(s)
Input
2
Output
10
Solution:
二分答案。
首先要知道如何求一个数的阶乘中一个因子的个数。
对于k!,其中至少包含一个p因子的数有,k/p个
两个p因子的有k/p^2 个,但其中的一个上一步中已经计算,所以只要加上k/p^2 即可
....
后面的类推
那么只要二分答案就可以得出解
时间复杂度O(log2N*log5N)
#include <iostream>
using namespace std;
int check (int x) {
int s = 0;
while(x){
s+=x/5;
x/=5;
}
return s;
}
int n;
int main() {
cin >> n;
int l = 1, r = n *5, k, mid;
int last = -1;
if(n==0) last=1;
while (l <= r) {
mid = l + (r - l) / 2;
k = check (mid);
if (k == n) {
last = mid;
r = mid - 1;
}
else if (k > n) r = mid - 1;
else if (k < n) l = mid + 1;
}
if (last != -1 ) cout << last;
else
cout << "No solution";
}
SGU 154.Factorial的更多相关文章
- sgu 154
Factorial 题意:能否找到一个数,它的阶乘后面0的个数为n? 数越大,阶乘后的0越多.用二分找.对于一个数x,它的阶乘,将小于等于它的数分解质因数.其中2的个数一定大于5的个数.因此计5的个数 ...
- SGU 分类
http://acm.sgu.ru/problemset.php?contest=0&volume=1 101 Domino 欧拉路 102 Coprime 枚举/数学方法 103 Traff ...
- 今日SGU 5.4
SGU 127 题意:给你n个数字,和m,k,问你有多少个数字的m次幂可以被k整除 收获:快速幂 #include<bits/stdc++.h> #define de(x) cout< ...
- SGU Volume 1
SGU 解题报告(持续更新中...Ctrl+A可看题目类型): SGU101.Domino(多米诺骨牌)------------★★★type:图 SGU102.Coprimes(互质的数) SGU1 ...
- Netty(4-1)factorial~总结
本节大纲: 1.Handler的执行顺序2.自定义二进制协议(每条完整数据的组成),从而解决拆包和粘包.3.通过为每个channel创建新的handler,从而解决即使handler中使用全局变量,也 ...
- 今日SGU 5.29
sgu 299 题意:给你n个线段,然后问你能不能选出其中三个组成一个三角形,数字很大 收获:另一个大整数模板 那么考虑下为什么如果连续三个不可以的话,一定是不存在呢? 连续上个不合法的话,一定是 a ...
- [LeetCode] Factorial Trailing Zeroes 求阶乘末尾零的个数
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- 1Z0-053 争议题目解析154
1Z0-053 争议题目解析154 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 154.A database is running in ARCHIVELOG mode and ...
- CodeForces 515C. Drazil and Factorial
C. Drazil and Factorial time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
随机推荐
- Linux Shell编程(21)——复杂命令
更高级的用户命令find-exec COMMAND \;在每一个find 匹配到的文件执行 COMMAND 命令. 命令序列以 ; 结束( ";" 是 转义符 以保证 shell ...
- checkbox操作
小小示例:自己备份顺便粘出来共享. 引入头部文件:<script src="../js/jQuery1.7.2.js"></script> HTML代码: ...
- UVa816 Abbott's Revenge
Abbott's Revenge Time limit: 3.000 seconds Abbott’s Revenge Abbott’s Revenge The 1999 World FinalsC ...
- Automake
Automake是用来根据Makefile.am生成Makefile.in的工具 标准Makefile目标 'make all' Build programs, libraries, document ...
- spring maven pom
https://spring.io/blog/2009/12/02/obtaining-spring-3-artifacts-with-maven/
- python 3 处理HTTP 请求的包
http http: https://docs.python.org/3/library/http.html http是一个包,里面含有多个模块:http.client,http.server,htt ...
- java_method_readFile读取文件文本xls
package cn.com.qmhd.tools; import java.io.FileInputStream; import java.io.FileNotFoundException; imp ...
- (二)Eclipse 快捷键
编辑 Ctrl+1 快速修复(最经典的快捷键,就不用多说了,可以解决很多问题,比如import类.try catch包围等)Ctrl+Shift+F 格式化当前代码Ctrl+Shift+M 添加类的i ...
- C# ArrayList 基本用法 分类: C# 2014-09-26 11:03 524人阅读 评论(0) 收藏
首先说明一下ArrayList 与 数组的区别: (1)ArrayList 的容量可以根据需要自由扩充,数组的容量是固定的 (2)ArrayList 只能是一维形式,数组可以是多维的 (3)Array ...
- 【设计模式 - 16】之迭代器模式(Iterator)
1 模式简介 迭代器模式是JAVA中非常常用的模式,List.Map.Set等常见集合中都封装了迭代器Iterator. 迭代器模式的介绍: 迭代器模式用于顺序访问集合对象中的元素,而不需要 ...