谜之WA

完全k叉树

谜之WA

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
ll k,n;
ll res=0;
ll check()
{
    ll ans=0;
    ll t=1ll;
    ll b=k-1;
    while((t*k-1)/b<n)t*=k,ans++;
   // if(t*k<0)puts(0);
    res=n-(t-1)/b;
    if(res==0)
    {
        res=0;
        return ans;
    }
    if(res>t/k)res=1;
    else res=0;
    return ans;
}
int main()
{
    ll t;
    cin>>t;
    while(t--)
    {
        res=0;
        cin>>k>>n;
        
        //else 
        if(k==1)cout<<n-1<<'\n';
        //else if(k>1000000000)puts(0);
        else
        {
            ll x=check();
            if(res)cout<<x+x<<'\n';
            else cout<<x+(x-1)<<'\n';
        }
    }
}

AC:

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
ll k,n;
ll res=0;
ll check()
{
    ll ans=0;
    ll t=1; 
 
    while(t<n)n-=t,t*=k,ans++;
   //ll a=1;
   
   // if(t*k<0)puts(0);
    res=n;
    //cout<<res<<'\n';
    if(res==0)
    {
        res=1;
        return ans-1;
    }
    if(res>t/k)res=1;
    else res=0;
    return ans;
}
int main()
{
    ll t;
    cin>>t;
    while(t--)
    {
        res=0;
        cin>>k>>n;

        //else
        if(k==1)cout<<n-1<<'\n';
        //else if(k>1000000000)puts(0);
        else
        {
            ll x=check();
            if(res)cout<<x+x<<'\n';
            else cout<<x+(x-1)<<'\n';
        }
    }
}

 

posted @ 2019-07-29 18:00  liulex  阅读(148)  评论(0编辑  收藏  举报