SGU-154 Factorial (末尾0 & 三分)
来源:互联网 发布:linux下查看当前用户 编辑:程序博客网 时间:2024/05/22 00:19
154. Factorial
time limit per test: 0.25 sec.
memory limit per test: 4096 KB
memory limit per test: 4096 KB
input: standard input
output: standard output
output: standard output
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
One number Q written in the input (0<=Q<=10^8).
Output
Write "No solution", if there is no such number N, and N otherwise.
Sample test(s)
Input
2
Output
10
分析:要求出n!的末尾0的个数也就是寻找n!中5的个数,如5!的5因子个数是1,15!的5因子的个数是3,25的5因子个数是6,可以得到这样的递推式s[n!]=n/5+s[(n/5)!]。在(0<=Q<=10^8)内寻找这样的值n,为了抢时间用三分:#include<cstdio>using namespace std;const int maxn=4e8+15;int sum;int getnum(int n){ if(n==0)return 0; sum=n/5+getnum(n/5); return sum;}int ternarysearch(int low,int high,int x){ int res; if(high<low) res= -1; else { int mid1=low+(high-low)/3,mid2=high-(high-low)/3; int q1=getnum(mid1),q2=getnum(mid2); if(x==q1) res=mid1; else if(x<q1) res=ternarysearch(low,mid1-1,x); else if(x==q2) res=mid2; else if(x>q2) res=ternarysearch(mid2+1,high,x); else res=ternarysearch(mid1+1,mid2-1,x); } return res;}int main(){ //freopen("cin.txt","r",stdin); int n; while(~scanf("%d",&n)){ if(n==0){ printf("1\n"); continue; } int ans=ternarysearch(0,maxn,n); if(ans==-1)printf("No solution\n"); else { ans=ans/5*5; printf("%d\n",ans); } } return 0;}
0 0
- SGU-154 Factorial (末尾0 & 三分)
- SGU - 154 - Factorial (数论)
- hdu 1124 Factorial(末尾0)
- SGU 154 Factorial(数论+二分)
- sgu 154 Factorial
- SGU 154-Factorial
- hdu 1124 Factorial (快速求n!末尾0的个数)
- SGU - 154 Factorial(阶乘素因子次数的逆问题)
- SGU 154. Factorial
- SGU 154. Factorial
- Factorial Trailing Zeroes N!末尾0的个数
- 172. Factorial Trailing Zeroes计算n!末尾0的个数
- 数学基础:HUD1124-Factorial(N!末尾0的个数)
- [sgu]Telecasting station【三分】
- sgu 114 三分
- leetcode解题之172# Factorial Trailing Zeroes Java版 (求n的阶乘末尾0的个数)
- SGU 114 三分 类似于 HDU 4355(4355 同样也是一个三分的题目)
- leetcode172-Factorial Trailing Zeroes(求N!末尾有多少个0)
- nyoj17 单调递增最长子序列(DP)
- 把.AndroidStudio1.2文件夹移出系统盘
- 集训目标
- linux下gtk+一个将字符串大写化的小示例
- 7个睡前习惯 让你多活10年
- SGU-154 Factorial (末尾0 & 三分)
- C程序变量的声明、定义、初始化和赋值
- 如何在通达OA系统上搭建一个报价系统
- 如何解决ADB server didn't ACK
- Scala入门到精通——第十六节 泛型与注解
- [Drools]JAVA规则引擎 -- Drools
- PROBLEM_J: Gym 100345J - Zen Garden
- RESTful
- yarn架构-概述