题解:P13907 「TFXOI Round 3」树的染色

· · 题解

猜出来了。

二分答案,于是只需要求给定「距离」的情况下选点数量的最大值。

贪心的想肯定从叶子结点选起啊,然后就会发现叶子的所有贡献最多就是距离「距离」一半的祖先数量的两倍。

然后你会发现这些祖先的子树(不包含自身)就不可能再选择了,不妨直接砍掉,于是得到了一个子问题。

递归即可。

然后由于值域的原因,一旦超出 10^{18} 显然可以剪枝,于是飞速草过去了。

我们就这么做完了。

#include<bits/stdc++.h>
#define int __int128
using namespace std;
int read(){
    int sum=0,fish=1;
    char c=getchar_unlocked();
    while((c<'0'||c>'9')&&c!='-')c=getchar_unlocked();
    if(c=='-')fish=-1,c=getchar_unlocked();
    while(c>='0'&&c<='9')sum=sum*10+(c-'0'),c=getchar_unlocked();
    return sum*fish;
}
void print(int x){
    if(x<0)putchar_unlocked('-'),x=-x;
    if(x<10)putchar_unlocked(x+'0');
    else print(x/10),putchar_unlocked(x%10+'0');
}
int cnt(int x){
    if(x<=0)return 1;
    if(x>60)return 1e18;
    return(1ll<<(x-1));
}
int check(int n,int x){
    int t=cnt(n-(x+1)/2);
    if(t>=1e18)return 1e18;
    if(n<=x)return t*2;
    t=check(n-x,x)+t*2;
    if(t>=1e18)return 1e18;
    return t;
}
void solve(){
    int n,k;
    n=read(),k=read();
    int l=1,r=n*2-2;
    while(l<r){
        int mid=(l+r+1)/2;
        if(check(n,mid)>=k)l=mid;
        else r=mid-1;
    }
    print(l),puts("");
}
signed main(){
    // print(check(1000,1900)),puts("!");
    int t=read();
    while(t--)solve();
    return 0;
}
// 很遗憾,您的《题解:P13907 「TFXOI Round 3」树的染色》不符合推荐标准。原因是:不要写奇怪的表情。

已完成今日「不要写奇怪的表情」大学习,已严肃修改。