LightOJ - 1138 (二分+阶乘分解)
题意:求阶乘尾部有Q(1 ≤ Q ≤ 108)个0的最小N
分析:如果给出N,然后求N!尾部0的个数的话,直接对N除5分解即可(因为尾部0肯定是由5*2构成,那么而在阶乘种,2的因子个数要比5少,所以求阶乘中因子5的个数就是尾部0的个数)。本题是给出尾部0的个数,逆推N。如果从小到大枚举的话,肯定会超时。
一般这种问题,可以用二分的方法搜答案。得到答案之后再向下逼近。
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long LL;
int cnt(int N)
{
int cnt=;
while(N){
cnt+=N/;
N/=;
}
return cnt;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int T,N,cas=;
scanf("%d",&T);
while(T--){
scanf("%d",&N);
int L=,R=,ans=-;
while(L<=R){
int m = L+(R-L)/;
int re = cnt(m);
if(re==N){
while(m%!=) m--; //向下逼近
ans = m;
break;
}
else if(re<N) L = m+;
else R = m-;
}
printf("Case %d: ",cas++);
if(ans>) printf("%d\n",ans);
else printf("impossible\n");
}
return ;
}
LightOJ - 1138 (二分+阶乘分解)的更多相关文章
- Trailing Zeroes (III) LightOJ - 1138 二分+找规律
Time Limit: 2 second(s) Memory Limit: 32 MB You task is to find minimal natural number N, so that N! ...
- LightOJ 1138 二分
1138 - Trailing Zeroes (III) PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...
- LightOJ 1138 Trailing Zeroes (III)(二分 + 思维)
http://lightoj.com/volume_showproblem.php?problem=1138 Trailing Zeroes (III) Time Limit:2000MS M ...
- 数论-质数 poj2689,阶乘分解,求阶乘的尾零hdu1124, 求尾零为x的最小阶乘
/* 要求出[1,R]之间的质数会超时,但是要判断[L,R]之间的数是否是素数却不用筛到R 因为要一个合数n的最大质因子不会超过sqrt(n) 所以只要将[2,sqrt(R)]之间的素数筛出来,再用这 ...
- 将n(0<=n<=10000)的阶乘分解质因数,求其中有多少个m
给定两个数m,n,其中m是一个素数. 将n(0<=n<=10000)的阶乘分解质因数,求其中有多少个m. 输入 第一行是一个整数s(0<s<=100),表示测试数据的组数 随后 ...
- luogu1445 [violet]樱花 阶乘分解
题目大意 求方程$$\frac{1}{x}+\frac{1}{y}=\frac{1}{N!}$$的正整数解的组数. 思路 咱们把式子整理得$$xy-(x+y)N!=0$$.$xy$和$x+y$?貌似可 ...
- LightOj 1138 - Trailing Zeroes (III) 阶乘末尾0的个数 & 二分
题目链接:http://lightoj.com/volume_showproblem.php?problem=1138 题意:给你一个数n,然后找个一个最小的数x,使得x!的末尾有n个0:如果没有输出 ...
- LightOJ Trailing Zeroes (III) 1138【二分搜索+阶乘分解】
1138 - Trailing Zeroes (III) PDF (English) problem=1138" style="color:rgb(79,107,114)" ...
- LightOJ 1340 - Story of Tomisu Ghost 阶乘分解素因子
http://www.lightoj.com/volume_showproblem.php?problem=1340 题意:问n!在b进制下至少有t个后缀零,求最大的b. 思路:很容易想到一个数通过分 ...
随机推荐
- jQuery Colorbox使用教程
jQuery Colorbox是一款弹出层,内容播放插件,效果极佳,最关键的是大小只有10KB,当然我主要是用来弹出图片啦,(之前介绍过jquery Fancybox插件,个人很喜欢).jQuery ...
- PAT003 List Leaves
题目: Given a tree, you are supposed to list all the leaves in the order of top down, and left to righ ...
- oracle的dual表
1.DUAL表的用途Dual 是 Oracle中的一个实际存在的表,任何用户均可读取,常用在没有目标表的Select语句块中 --查看当前连接用户Connected to Oracle Databas ...
- 第二百二十六节,jQuery EasyUI,Tree(树)组件
jQuery EasyUI,Tree(树)组件 本节课重点了解 EasyUI 中 Tree(树)组件的使用方法,这个组件依赖于 Draggable(拖 动)和 Droppable(放置)组件. 一.加 ...
- String, StringBuffer StringBuilder的区别。
解答:String的长度是不可变的: StringBuffer的长度是可变的,如果你对字符串中的内容经常进行操作,特别是内容要修改时,那么使用StringBuffer,如果最后需要String,那么使 ...
- 读写app.config AppSettings,保留注释与不保留注释
不保留 using System; using System.Configuration; namespace ConsoleApplication1 { class Program { static ...
- websphere web.xml
解决WAS更新web.xml文件不生效的问题(web_merged.xml是罪魁祸首) 问题原因分析 近日碰到更新web.xml文件到WAS服务器(WebSphere Application Se ...
- js 风格(注意事项)
类型 • 原始类型:我们可以直接使用值. ο string ο number ο boolean ο null ο undefined var foo = 1, bar = foo; bar = ...
- 【R】均值假设检验
p_value<-function(cdf,x,parament=numeric(0),side=0) { n<-length(parament) p<-switch(n+1, cd ...
- WPF数据验证(4)——响应与获取验证错误
1780 前面的示例中,有关用户接受到错误的唯一指示是在违反规则的文本框周围的红色轮廓.为了提供更多信息,可以处理 Error 事件,但存储或清除错误时会引发该事件,但前提是必须确保已将 Bindin ...