cf 1485 A

http://codeforces.com/problemset/problem/1485/A

这个题感觉比b,c题还烦。不好想到。

#include <bits/stdc++.h>
//#define int long long
#define ll long long
using namespace std;
template <class cl>
void read(cl &x)
{
    x = 0;
    int f = 0;
    char ch;
    ch = getchar();
    while (!isdigit(ch))
    {
        f = f | (ch == '-'), ch = getchar();
    }
    while (isdigit(ch))
    {
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    x = f ? -x : x;
    return;
}
template <class cl>
void put(cl x)
{
    if (x < 0)
        putchar('-'), x = -x;
    if (x > 9)
        put(x / 10);
    putchar(x % 10 + '0');
    return;
}
int a, b;
int ans = 0;
int nn=100;
int res;
int aa;
int bb;
/*inline void to()
{
    read(a);
    read(b);
    if (a < b)
    {
        puts("1");
    }
    else
    {
        while (1)
        {
            if(a==0) break;
            if ((a - a / b) * 2 > a - (a / (b + 1)))
            {
                a = a / b;
                 ans += 1;
            }
            else
            {
                b = b + 1;
                a = a / b;
                ans += 2;
            }
        }
        put(ans);
        puts("");
    }
    return ;
}*/
// 步骤1与步骤2交叠进行显然不如步骤二进行到一定程度后再一直进行步骤一。
inline void to()
{
    read(a);
    read(b);
    aa=a;//这里容易写反,易错。
    bb=b;
    ans=0;
    nn=100;//勿忘。
    if(a==0)
    {
        puts("0");
        return ;
    }
    if(a<b)
    {
        puts("1");
        return ;
    }
    if(a==b)
    {
        puts("2");
        return ;
    }
    if(b<2)
    {
       for(int i=2-b;i<=nn;i++)
       {//这里的更新nn的值很好用。因为要找的步骤数要比现有的要小。这样时间复杂度就下来了。
           ans=0;//勿忘值为零
           a=aa;//这里a与b要被重置 ,勿忘。
           b=bb;
           b=b+i;
           ans+=i;
           while(1)
           {
               if(a==0)break;
               a=a/b;
               ans++;
           }
           if(ans<=nn)
           {
               nn=ans;res=ans;
           } 
       }   
    }
    else 
    {
        for(int i=0;i<=nn;i++)
        {
            ans=0;//勿忘值为零
            a=aa;
            b=bb;
            b=b+i;
            ans+=i;
            while(1)
            {
                if(a==0) break;
                a=a/b;
                ans++;
            }
            if(ans<=nn)
             {
                nn=ans;
                res=ans;
             }
        }
    }
    put(res);
    puts("");
    return ;
}
signed main()
{
   // cout<<(1<<30)<<endl;
    int t;
    read(t);
    while (t--)
    {
        to();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值