暑期集训之Trailing Zeroes (III)
来源:互联网 发布:网络语言粗鄙化的原因 编辑:程序博客网 时间:2024/06/07 00:00
唉,先说下感受,其实发现现在很多都是看着别人的博客想思路了,基本上碰到一个题就不会做,碰到一个题就不会做,只有看半天别人的博客才能明白怎么做了,初学阶段比葫芦画瓢,但有时候感觉一直找葫芦搞得自己很惭愧,但不看又只能干瞪眼,总之走一步看一步吧,这东西还是得多练,毕竟现在每天接触的都是一些新的东西
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 starts with an integer T (≤ 10000), denoting the number of test cases.
Each case contains an integer Q (1 ≤ Q ≤ 108) in a line.
For each case, print the case number and N. If no solution is found then print 'impossible'.
3
1
2
5
Case 1: 5
Case 2: 10
Case 3: impossible
这道题题的大致意思就是说,给你一个数q,q代表的是某个数N的阶乘的末尾0的个数,最后让你找出这个N,当然这道题因为数据量很大,所以一个一个的找肯定是实现不了的,所以用到了二分思想,当然今天的专题就是二分,话不多说,代码奉上,代码中有详细的解释:
#include<stdio.h>long long cal(long long x)//这里是一个找出N!末尾有几个0的函数,当成模板记住就好了,原理的话在数学小知识里面有 {long long p=0;while(x){p=p+x/5;x=x/5;}return p;}int main(){int n;scanf("%d",&n);int t=n;while(n--){ long long q;long long l=0,r=500000000;//二分思想的话得有边界值,这个至目前算是最大的那个值了,再大就WA了 long long ans;//储存符合条件的N; scanf("%lld",&q);while(l<=r)//二分经典模板,改下条件直接套用就好了 {long long mid=(r+l)/2;if(cal(mid)>=q) {ans=mid;r=mid-1;}elsel=mid+1;}if(cal(ans)!=q)//如果最后得出来的那个N!最后末尾0的个数和给的个数不等意味着这个数不存在,注意的是这里是cal(ans)和q比较的,因为ans是N,而cal(ans)才是0的个数 printf("Case %d: impossible\n",t-n);elseprintf("Case %d: %d\n",t-n,ans);//输出的时候输出所得的N就行了 }return 0; }
- 暑期集训之Trailing Zeroes (III)
- Trailing Zeroes (III)<二分>
- Trailing Zeroes (III)
- Trailing Zeroes (III)
- lightoj Trailing Zeroes (III)
- Trailing Zeroes (III) LightOJ
- Trailing Zeroes (III)
- Trailing Zeroes (III) (数学)
- Trailing Zeroes (III)
- acm Trailing Zeroes (III)
- 二分法-Trailing Zeroes (III)
- Trailing Zeroes (III)
- Trailing Zeroes (III)
- LightOJ1138 Trailing Zeroes (III)
- Trailing Zeroes (III)
- Trailing Zeroes (III)
- lightOJ 1138 Trailing Zeroes (III)
- LightOJ 1138 Trailing Zeroes (III)
- Android 监控(检查)SD卡指定目录的变化
- 欢迎使用CSDN-markdown编辑器
- Android编程规范
- Android 闹钟AlarmManager简略使用说明
- 域名解析过程和域名劫持、域名污染
- 暑期集训之Trailing Zeroes (III)
- Android_贝塞尔曲线
- HDU 2612 Find a way 简单BFS
- ERROR:../../gi/pygi-argument.c:1586:
- 计算机网络、OSI模型、TCP\IP族
- lua 创建二维数组
- svn 拉分支
- google代理服务器设置
- Python中如何使用以及去除时间序列数据中的趋势信息