D. Olya and magical square (数学规律题)

详细加图文讲解

 

 

 

#include <bits/stdc++.h>
#define LL long long
#define mem(i, j) memset(i, j, sizeof(i))
#define rep(i, j, k) for(int i = j; i <= k; i++)
#define dep(i, j, k) for(int i = k; i >= j; i--)
#define pb push_back
#define make make_pair
#define INF INT_MAX
#define inf LLONG_MAX
#define PI acos(-1)
using namespace std;

const int N = 1e6 + 5;

LL C[105];

int main() {
    rep(i, 1, 31) C[i] = C[i - 1] * 4LL + 1LL;
    int _; scanf("%d", &_);
    while(_--) {
        LL n, k;
        scanf("%lld %lld", &n, &k);
        if(n > 31) {
            printf("YES %lld\n", n - 1); continue;
        }
        LL res = 0LL, now = 1, j = 0, tmp = 0LL;
        while(res + now <= k && j < n) {
            res += now;
            now = now * 2 + 1;
            j++;
            tmp += C[n - j] * (now - 2LL);
        }
        if(k > res + tmp) puts("NO");
        else printf("YES %lld\n", n - j);
    }
    return 0;
}
View Code

 

posted on 2020-02-20 14:56  Willems  阅读(191)  评论(0编辑  收藏  举报

导航