Codeforces Round #524 (Div. 2) Olya and magical square CodeForces - 1080D

题意:

给你 2^n * 2^n 的正方形,让你执行恰好 k 次操作,每一次操作将一个大小不为 1 的正方形剪成四个一模一样的正方形,但是正方形不能被移动。
问:k 次操作后,能否从左下角找一条路径到达右上角(只能横着或竖着走),使得经过的正方形大小相同(包含起点与终点)。
如果可以,输出经过的正方形的边长的以 2 为底的对数。
 

枚举路径上正方形的边长,判断是否合法。

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string.h>
#include<queue>
#include<stack>
#include<list>
#include<map>
#include<set>
#include<vector>
using namespace std;
typedef long long int ll;
const int maxn =1e9+5;
const int maxm=10000;
const int mod =1e9+7;
const int INF=0x3f3f3f3f;
const double eps=1e-8;

ll fpow(ll a,ll b)
{
	ll ans=1;
	while(b)
	{
		if(b&1)ans=ans*a;
		a=a*a;
		b>>=1;
	}
	return ans;
}
int main()
{
	int t;scanf("%d",&t);
	while(t--)
	{
		ll n,k;scanf("%lld%lld",&n,&k);
		if(n==2&&k==3) printf("NO\n");
        else if(n>31) printf("YES %lld\n",n-1ll);
        else{
            ll a=2;
            int flag=0;
            for(ll i=1;i<=n;i++){
                ll len=n-i;
                ll p=a*a-a*2ll+1ll;
                ll k1=(fpow(4ll,i)-1ll)/3ll;
                ll k2=(fpow(4ll,len)-1ll)/3ll;
                if(k<=k1+k2*p){
                    flag=1;
                    printf("YES %lld\n",len);
                    break;
                }
                a*=2ll;
            }
            if(flag==0) printf("NO\n");
        }
	}
    return 0;
}

猜你喜欢

转载自blog.csdn.net/wzazzy/article/details/84851490