51 nod 1859 Clarke and number
第三题,思想不难,坑点贼多。
克拉克是一名人格分裂患者.某一天克拉克变成了一名数论研究者,在研究数字.
他想到了一个题:给定非负整数 x 和正整数 k ,可以做若干操作,每次操作是以下两种方法之一:
1. x=x−k
2. x=⌊x√⌋2
现在克拉克想知道,这个整数最少经过多少次操作可以变成 0 .
Input
第一行是一个正整数T(1≤T≤100),表示数据组数. 每组数据只有一行两个整数x, k(0≤x≤10^18, 1≤k≤2).
Output
每组数据输出一行一个整数,表示最少的操作数.若不存在方案,输出-1
Input示例
2 2 1 3 2
Output示例
2
-1
首先我们发现操作1是将一个数减去K,操作2是将一个数变为不大于它的最大的完全平方数,接着我们发现一个数只要变成4之后都可以变为0,而操作1和2的组合可以让一个完全平方数退到比起小的数上:4^2-->3^2,这样我们只要先把一个数变成完全平方数再把它一步步推到4就可以了
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> using namespace std; typedef long long ll; int T; ll x,y; int Time=0; int main() { scanf("%d",&T); while(T--) { ll ans=0; scanf("%lld %lld",&x,&y); if(x<=4) { if(y==1) { if(x==0) cout<<0<<endl; if(x==1) cout<<1<<endl; if(x==2) cout<<2<<endl; if(x==3) cout<<2<<endl; if(x==4) cout<<3<<endl; continue; } if(y==2) { if(x==0) cout<<0<<endl; if(x==1) cout<<-1<<endl; if(x==2) cout<<1<<endl; if(x==3) cout<<-1<<endl; if(x==4) cout<<2<<endl; continue; } } ll sq=sqrt(x); ll cha=x-sq*sq; if(cha<0) { ans++,sq--; }else if(cha==1&&y==2&&x>9) ans+=2,sq--;else if(cha!=0) ans++; //1ans++; ans+=(sq-2)*2; if(y==1) ans+=3;else ans+=2; cout<<ans<<endl; } }