标签:iii stream using 乘法 直接 long 方法 nat ace
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
3
1
2
5
Sample Output
Case 1: 5
Case 2: 10
Case 3: impossible
其实我特别反感这种思维题目,感觉没别的方法,没什么意思,只能这样写,还有就是我太菜了(这才是原罪)
思路::有几个0出现关键就看2和5的个数,因为是阶乘,都是乘法运算,所有要将没一个数拆开,看看他包括几个2或者几个5,,比如,10 可以分为2和5,所以只有1个5,而
25可以分为5和5 所以有两个5。。。。。在一组数中,二的数目一定比5 多吗,所以直接找5 的个数就可以啦!
记得设置为long long
AC代码:
#include<iostream> #include<cstdio> using namespace std; typedef long long ll; ll sum(ll x){ ll ans=0; while(x) { ans+=x/5; x=x/5; } return ans; } int main() { int t; scanf("%d",&t); for(int i=1;i<=t;i++) { ll n; scanf("%lld",&n); ll left=1,right=1000000000,ans=0,mid; while(left<=right) { mid=(left+right)/2; if(sum(mid)==n){ ans=mid; right=mid-1; }//找到了不一定是最小的比如11和10,阶乘都可以产生2个0 else if(sum(mid)>n){ right=mid-1; } else { left=mid+1; } } if(ans>0) printf("Case %d: %lld\n",i,ans); else { printf("Case %d: impossible\n",i); } } return 0; }
标签:iii stream using 乘法 直接 long 方法 nat ace
原文地址:https://www.cnblogs.com/Accepting/p/11247701.html