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. ...
随机推荐
- layDate 日期与时间组件 入门
首先第一步 在官方下载layDate文件.layUI官网:http://layer.layui.com/ https://www.layui.com/laydate/ layDate文件的下载步 ...
- this.class.getClassLoader().getResourceAsStream
this.getClass().getClassLoader().getResource("template"); 首先,调用对象的getClass()方法是获得对象当前的类 ...
- java高级编程-使用反射强制给private字段赋值
转自:http://blog.csdn.net/yaerfeng/article/details/7103397 今天项目中遇到了一个问题,要调用一个类,并获取这个类的属性进行赋值然后将这个类传递到方 ...
- SilverLight:基础控件使用(2)-ComboBox,ListBox控件
ylbtech-SilverLight-Basic-Control:基础控件使用(2)-ComboBox,ListBox控件 直接在 XAML 代码中设置 Items 和通过后台代码绑定数据源 Com ...
- pandas常见函数详细使用
groupby函数 pandas提供了一个灵活高效的groupby功能,它使你能以一种自然的方式对数据集进行切片.切块.摘要等操作,根据一个或多个键(可以是函数.数组.Series或DataFrame ...
- 现代数字信号处理——AR模型
1. AR模型概念观 AR模型是一种线性预测,即已知N个数据,可由模型推出第N点前面或后面的数据(设推出P点),所以其本质类似于插值,其目的都是为了增加有效数据,只是AR模型是由N点递推, ...
- Android——动画的分类
Android包含三种动画:View Animation, Drawable Animation, Property Animation(Android 3.0新引入). 1.View Animati ...
- javascript之Ajax获取和设置标头
XMLHttpRequest对象中与标头有关的方法: setRequestHeader(<header>,<value>)--用指定值设置标头. getResponseHead ...
- mvc用UpdateModel报错
项目中使用UpdateModel时报错:未能更新类型“XXXXXX”的模型. 原因如下:表单Post时,有的参数为空,如a=1&b=2&=3.
- mvc已添加了具有相同键的项
异常详细信息: System.ArgumentException: 已添加了具有相同键的项. 场景重现:在地址栏输入 http://localhost:51709/Home/Index?user[0 ...