码迷,mamicode.com
首页 > 其他好文 > 详细

魔法石的诱惑

时间:2016-12-20 01:28:20      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:iostream   ios   ons   puts   lin   str   它的   贪婪   for   

题目描述:

修罗王远远地看见邪狼狂奔而来,问道:“慌慌张张的跑什么?”
邪狼大口大口的喘气:“我路过一家魔法石店,看到摆着那么多高阶魔法石,我就跑进去抢了一大袋。”
修罗王怒道:“光天化日,朗朗乾坤,众目睽睽之下,你也敢抢?”
邪狼:“我抢魔法石的时候,压根就没看见人,眼里只看见魔法石了”
修罗王:“……”

其实邪狼的贪婪很容易理解,因为高阶魔法石有一个特征,即它的重量进行阶乘运算后末尾有几个0,就拥有同等重量的普通魔法石几倍的魔法力。例如5!=5*4*3*2*1=120,而120结尾包含1个零,这意味着该魔法石拥有同等重量的魔法石 1 倍的魔法力。你的任务是找到最小自然数 N ,使 N!在十进制下包含 Q 个零。

输入格式:

一个数Q(0 <= Q <= 10^8 )。

输出格式:

如果无解,输出“No solution”,否则输出 N 。

输入示例:

2

输出示例:

10

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>

using namespace std;

inline int read(){
    int x=0,f=1;char ch=getchar();
    for(;!isdigit(ch);ch=getchar()) if( ch == - ) f=-1;
    for(;isdigit(ch);ch=getchar()) x=x*10+ch-0;
    return x*f;
}

const int maxx = 1000000000;

int n, ans;

int count(int num)
{
    int count = 0;
    for(; num >= 5; num /= 5) count += num / 5;
    return count;
}

int main(){
    n = read();
    int left=1,right=1000000000,ans=500000001;
    while(left <= right)
    {
        int mid=(right+left) / 2 , cnt = count(mid);
        if(cnt == n && mid < ans) ans = mid;
        if(cnt > n){
            right = mid - 1;
        }
        else if(cnt < n){
            left = mid + 1;
        }
        else if(cnt == n){
            right = mid - 1;
        }
    }
    if(ans == 500000001) puts("No solution");
    else printf("%d\n",ans);
}

 

魔法石的诱惑

标签:iostream   ios   ons   puts   lin   str   它的   贪婪   for   

原文地址:http://www.cnblogs.com/ysmor/p/6201125.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!