結果

問題 No.3375 Binary Grid
コンテスト
ユーザー hirakuuuu
提出日時 2025-11-21 23:08:31
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,515 bytes
コンパイル時間 2,987 ms
コンパイル使用メモリ 276,200 KB
実行使用メモリ 7,852 KB
最終ジャッジ日時 2025-11-21 23:08:37
合計ジャッジ時間 5,124 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 1 WA * 6
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;
#define rep(i, a, n) for(int i = a; i < n; i++)
#define rrep(i, a, n) for(int i = a; i >= n; i--)
#define inr(l, x, r) (l <= x && x < r)
#define ll long long
#define ld long double

// using mint = modint1000000007;
// using mint = modint998244353;
constexpr int IINF = 1001001001;
constexpr ll INF = 1e18;

template<class t,class u> void chmax(t&a,u b){if(a<b)a=b;}
template<class t,class u> void chmin(t&a,u b){if(b<a)a=b;}

int main(){
    int t; cin >> t;
    while(t--){
        ll r, c; cin >> r >> c;
        if(r == 1){
            cout << 0 << endl;
            continue;
        }


        
        ll mr = 1, mc = 1;
        while(mr*2 <= r){
            mr *= 2;
            mc += 1;
        }
        ll u = max(mr-1, mc-1);

        auto f = [&](auto self, ll pr, ll pc) -> ll {
            if(c == pc){
                return r-pr;
            }
            if(r <= pr){
                ll _c = 0;
                while((r&((1LL<<_c)-1)) == ((1LL<<_c)-1)){
                    _c++;
                }
                if(c <= _c){
                    return max(pr-r+(_c-c-1), pc-c);
                }
                return max(pr-r, pc-c);
            }

            ll nc = pc-1;
            ll nr = pr+(1LL<<(nc-1));
            return (nr-pr)+self(self, nr, nc);
        };
        ll d = f(f, mr, mc);
        // cout << u << ' ' << d << endl;
        cout << u+d << endl;
    }
    return 0;
}
0