LightOJ1138 —— 阶乘末尾0、质因子分解
题目链接:https://vjudge.net/problem/LightOJ-1138
| Time Limit: 2 second(s) | Memory Limit: 32 MB |
You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in decimal notation. As you know N! = 1*2*...*N. For example, 5! = 120, 120 contains one zero on the trail.
Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.
Each case contains an integer Q (1 ≤ Q ≤ 108) in a line.
Output
For each case, print the case number and N. If no solution is found then print 'impossible'.
Sample Input |
Output for Sample Input |
|
3 1 2 5 |
Case 1: 5 Case 2: 10 Case 3: impossible |
题意:
求是否存在一个数n,使得 n! 的十进制表示末尾有Q个0,如存在,输出最小值。
题解:
1. 对 n! 进行质因子分解:n! = 2^a1 * 3^a2 * 5^a3 * …… 。
2.可知质因子2出现的次数大于质因子5出现的次数,且质因子中只有2*5 = 10。综上,有多少个质因子5,末尾就有多少个0。
3.那怎么知道 n! 里面有多少个质因子5呢?
答: 从1到n,有多少个数是5的倍数呢? n/5 个。 当这n/5数格子除以5之后,又还剩几个数是5的倍数呢? 那就是 (n/5)/5 个,然后一直下去。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e6+; LL Count(LL tmp)
{
LL sum = ; //不能定义int
while(tmp) sum += tmp/, tmp /= ;
return sum;
} int main()
{
int T, kase = ;
scanf("%d", &T);
while(T--)
{
LL Q;
scanf("%lld", &Q);
LL l = , r = LNF;
while(l<=r)
{
LL mid = (l+r)/;
if(Count(mid)>=Q)
r = mid - ;
else
l = mid + ;
} printf("Case %d: ", ++kase);
if(Count(l)!=Q)
printf("impossible\n");
else
printf("%lld\n", l);
}
}
LightOJ1138 —— 阶乘末尾0、质因子分解的更多相关文章
- Algorithm --> 求阶乘末尾0的个数
求阶乘末尾0的个数 (1)给定一个整数N,那么N的阶乘N!末尾有多少个0?比如:N=10,N!=3628800,N!的末尾有2个0. (2)求N!的二进制表示中最低位为1的位置. 第一题 考虑哪些数相 ...
- Java 计算N阶乘末尾0的个数-LeetCode 172 Factorial Trailing Zeroes
题目 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in ...
- N的阶乘末尾0的个数和其二进制表示中最后位1的位置
问题一解法: 我们知道求N的阶乘结果末尾0的个数也就是说我们在从1做到N的乘法的时候里面产生了多少个10, 我们可以这样分解,也就是将从0到N的数分解成因式,再将这些因式相乘,那么里面有多少个 ...
- [LeetCode] Factorial Trailing Zeroes 阶乘末尾0
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- LightOj 1138 - Trailing Zeroes (III) 阶乘末尾0的个数 & 二分
题目链接:http://lightoj.com/volume_showproblem.php?problem=1138 题意:给你一个数n,然后找个一个最小的数x,使得x!的末尾有n个0:如果没有输出 ...
- [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 ...
- 求N的阶乘N!中末尾0的个数
求N的阶乘N!中末尾0的个数 有道问题是这样的:给定一个正整数N,那么N的阶乘N!末尾中有多少个0呢?例如:N=10,N=3628800,则N!的末尾有两个0:直接上干货,算法思想如下:对于任意一个正 ...
- NYOJ1026 阶乘末尾非0 【模板】
阶乘末尾非0 时间限制:2000 ms | 内存限制:65535 KB 难度:3 描写叙述 我们的问题非常是简单.n! 末尾非0数是几? 比方n=5的时候,n! =120,那么n!末尾非0数是2. ...
随机推荐
- ‘cnpm' 不是内部或外部命令,也不是可运行的程序
昨天用npm 安装环境,实在太慢了,就想用cnpm,然后发现提示‘cnpm' 不是内部或外部命令,也不是可运行的程序. 看了很多方法,选择了下面这个,运气好到爆棚,就直接可以用了.其他的方法暂未去了 ...
- Android Studio 常见命令
android studio 常用命令 Description Mac Linux/Win Lookup IDE commands Cmd+Shift+A Ctrl+Shift+A Open Clas ...
- POJ2104Kth Number
整体二分模板题, 有些细节需要注意 #include<cstdio> #include<cctype> #include<climits> #include< ...
- bootstrap-datetimepicker时间控件的使用
官方文档:http://www.bootcss.com/p/bootstrap-datetimepicker/demo.htm 常规使用: <div class="input-grou ...
- DTrace Oracle Database
http://d.hatena.ne.jp/yohei-a/20100515/1273954199 DTrace で Oracle Database のサーバー・プロセスをトレースしてみた Oracl ...
- 浅析 JavaScript 中的闭包(-------------------------------------------)
一.前言 对于 JavaScript 来说,闭包是一个非常强大的特征.但对于刚开始接触的初学者来说它又似乎是特别高深的.今天我们一起来揭开闭包的神秘面纱.闭包这一块也有很多的文章介绍过了,今天我就浅谈 ...
- Myeclipse 出现了Could not generate DH keypair 错误
在myeclipse下安装svn插件,出现了Could not generate DH keypair,这么一个错误. 这个问题困扰了我半天时间,各种百度也找不到答案,或许是百度能力问题吧.百度出来的 ...
- Android Studio apk 打包流程(转)http://blog.chinaunix.net/uid-26000296-id-5567890.html
1.Build -> Generate Signed APK...,打开如下窗口 2.假设这里没有打过apk包,点击Create new,窗口如下 这里只要输入几个必要项 Key store p ...
- POJ3592 Instantaneous Transference 强连通+最长路
题目链接: id=3592">poj3592 题意: 给出一幅n X m的二维地图,每一个格子可能是矿区,障碍,或者传送点 用不同的字符表示: 有一辆矿车从地图的左上角(0,0)出发, ...
- 使用canvas 的api 实现 图片的显示 及 压缩
在移动端压缩图片并且上传主要用到filereader.canvas 以及 formdata 这三个h5的api.逻辑并不难.整个过程就是: (1)用户使用input file上传图片的时候,用file ...