【CodeForces - 1080D】Olya and magical square

@Olya and magical square@


@Description@

Recently, Olya received a magical square with the size of 2n×2n.

It seems to her sister that one square is boring. Therefore, she asked Olya to perform exactly k splitting operations.

A Splitting operation is an operation during which Olya takes a square with side a and cuts it into 4 equal squares with side a2. If the side of the square is equal to 1, then it is impossible to apply a splitting operation to it (see examples for better understanding).

Olya is happy to fulfill her sister’s request, but she also wants the condition of Olya’s happiness to be satisfied after all operations.

The condition of Olya’s happiness will be satisfied if the following statement is fulfilled:

Let the length of the side of the lower left square be equal to a, then the length of the side of the right upper square should also be equal to a. There should also be a path between them that consists only of squares with the side of length a. All consecutive squares on a path should have a common side.

Obviously, as long as we have one square, these conditions are met. So Olya is ready to fulfill her sister’s request only under the condition that she is satisfied too. Tell her: is it possible to perform exactly k splitting operations in a certain order so that the condition of Olya’s happiness is satisfied? If it is possible, tell also the size of the side of squares of which the path from the lower left square to the upper right one will consist.

Input
The first line contains one integer t (1≤t≤10^3) — the number of tests.

Each of the following t lines contains two integers ni and ki (1≤ni≤109,1≤ki≤1018) — the description of the i-th test, which means that initially Olya’s square has size of 2ni×2ni and Olya’s sister asks her to do exactly ki splitting operations.

Output
Print t lines, where in the i-th line you should output “YES” if it is possible to perform ki splitting operations in the i-th test in such a way that the condition of Olya’s happiness is satisfied or print “NO” otherwise. If you printed “YES”, then also print the log2 of the length of the side of the squares through space, along which you can build a path from the lower left square to the upper right one.

You can output each letter in any case (lower or upper).

If there are multiple answers, print any.

Example
input
3
1 1
2 2
2 12
output
YES 0
YES 1
NO

@Translation@

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

@Solution@

基本想法是比较简单的:枚举路径上正方形的边长,判断是否合法。

怎么判断呢?我们可以算出使得合法路径存在的最小操作数与最大操作数,如果在这个范围内就合法。

从大到小依次考虑几种情况,尝试找找规律。

情况(1):
解释图1
可以发现这时最小操作数必然为 1,不可能更少。
最大操作数呢?除了右下角那个正方形外,其他是不能被操作的。所以我们的最大操作数 = 将右下角那个正方形全部剪成 1*1 的操作数 + 1。

情况(2):
解释图2
这个时候,我们发现操作次数 <= 3 时无论如何都不能合法,所以最小操作次数为 4。
最大操作数,一样地,不能操作那些被路径经过的数。可以发现这个时候除了右下角最大的那个正方形,我们还多了一些正方形可以操作。
情况(3):

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值