22.01.13二分练习题

二分练习
摘要由CSDN通过智能技术生成

说实话 做二分得需要点脑子(doge)

(1)找到最小的N,使得N!末尾恰好有Q个0

相信你已经熟练掌握N!末尾的0的个数的求法。
现在给出数字Q,请找到最小的N,使得N!末尾恰好有Q个0

输入格式

输入一个整数Q(1<=Q<=10^8)

输出格式

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

输入样例

2

输出样例

10

虽然说是二分标签

但是着实没看出来哪里二分

暴力枚举每一种情况下末尾0个数

匹配就输出

超过输出No solution

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cmath>
#include<stack>
#include<algorithm>
#include<string>
using namespace std;

typedef unsigned long long ull;
//const ull MOD=0x3f3f3f3f3f;
const ull MOD=998244353;
const double PI=3.1415926;

ull dp[1000010];
ull jie[1000010];
//string a,b;
int main()
{
    ull x,y;
    cin>>x;
    int i,j,k;
    //int f=1;
    for(i=5;;i++)
    {
        int t=i;
        int sum=0;
        while(t)
        {
            sum+=t/5;
            t/=5;
        }
        //cout<<sum<<endl;
        if(sum==x)
        {
            cout<<i<<endl;
            break;
        }

        if(sum>x)
        {
            cout<<"No solution"<<endl;
            break;
        }
    }

    return 0;
}

不算很快,但是也没超时

(2)

没做出来,看的解析

Hi是一个单增函数

这道题浮点型一看就没法正常暴力枚举,就没法试了

我裂开了无数次

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cmath>
#include<stack>
#include<algorithm>
#include<string>
using namespace std;

typedef unsigned long long ull;
const ull MAX=0x3f3f3f3f3f;
const double PI=3.1415926;

double answer[1000010];
ull jie[1000010];
//string a,b;
double t;
double  x,y;
int n,b;
int i,j,k;

bool ans(double x)
{
    answer[1]=x;
    //int i;//之前定义在了循环内  出循环i失效
    for( i=2; i<n; i++)
    {
        answer[i]=2*answer[i-1]+2-answer[i-2];
        if(answer[i]<1e-3)
            return 0;
    }
    return 1;
}
int main()
{

    //int f=1;
    while(cin>>n>>x)
    {
        answer[0]=x;
        double life=-1,love=1e8;
        //int time=10000;
        while(love-life>1e-8)
        {
            double mid=(life+love)/2;
            //这里不会溢出
            if(ans(mid))
            {
                love=mid;
                //可以继续降低区间
            }
            else
            {
                life=mid;
                //不可以 则提高区间
            }
        }
        cout<<fixed<<setprecision(2)<<answer[n-1]<<endl;
    }

    return 0;
}

 参考的代码

真简洁,不用调用函数逻辑就是清晰

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cmath>
#include<stack>
#include<algorithm>
#include<string>
using namespace std;

typedef unsigned long long ull;
const ull MAX=0x3f3f3f3f3f;
const double PI=3.1415926;

const double eps=1e-8;

double answer[1000010];
ull jie[1000010];
int n;
double a;
int main()
{
    while(cin>>n>>a)
    {
        double life=0,love=1e13,ans=0;
        while(love-life>eps)
        {
            double mid=(love+life)/2;
            double pre1=a,pre2=mid;
            bool is=true;
            for(int i=2;i<n;i++)
            {
                double v=pre2;
                pre2=2*pre2-pre1+2;
                pre1=v;
                if(pre2<eps)
                {
                    is=false;
                    break;
                }
            }
            if(is)
            {
                ans=pre2;
                love=mid;
            }
            else
                life=mid;
        }
        cout<<fixed<<setprecision(2)<<ans<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值