Codeforces Round #739 (Div. 3) F1

F1. Nearest Beautiful Number (easy version)

很像网络赛北大出的那题 感觉这题是简化版
我们只需要把所有数都搞出来 然后二分即可
我们先枚举k1的情况
这个很简单 先枚举长度 然后枚举数
k
2的情况呢
我们也是先枚举长度 然后再枚举两个数为什么
然后最后1<<len的枚举每一位填第一个数还是第二个数
为了万无一失 我们还是unique一下
最后二分即可
最后我们发现这两个数加起来不超过1e5

vector<int>v[3];
void init(){
    v[1].push_back(0);
    for(int i=1;i<=10;i++){
        for(int j=1;j<=9;j++){
            string s;
            int k=i;
            while(k--){
                s+=char('0'+j);
            }
            v[1].push_back(stoll(s));
        }
    }
    for(int len=2;len<=10;len++){
        for(int j=0;j<=9;j++){
            for(int k=0;k<=9;k++){
                if(k==j)continue;
                for(int m=1;m<(1<<len)-1;m++){
                    string s;
                    for(int r=len-1;r>=0;r--){
                        if(m>>r&1){
                            s+=(char)('0'+k);
                        }else s+=(char)('0'+j);
                    }
                    v[2].push_back(stoll(s));
                }
            }
        }
    }
    sort(all(v[1]));
    sort(all(v[2]));
    v[2].erase(unique(all(v[2])),v[2].end());
}
void solve(){
    int n,m;cin>>n>>m;
    if(m==1)cout<<*lower_bound(all(v[m]),n)<<endl;
    else cout<<min(*lower_bound(all(v[m]),n),*lower_bound(all(v[1]),n))<<endl;
}
posted @ 2022-11-26 22:09  ycllz  阅读(19)  评论(0)    收藏  举报