跳到正文

题解

[ICPC 2017 Nanning R] The Chosen One的题解

Simon439747约 19 分钟
原文
文章信息与版本

文章信息

发布
2026年8月9日
收录
2026年8月13日
更新
2026年11月18日
篇幅
约 19 分钟
题目
P17216

版本

  1. v1当前

这题先拽答案:

2⌊log⁡2x⌋2^{\lfloor\log_2x\rfloor}
证明

我们每次回去掉 ⌈n2⌉\lceil \frac{n}{2}\rceil 个数据,那么,我们要搞到 11 个数据就需要 ⌊log⁡2x⌋\lfloor\log_2x\rfloor 次操作。

那么那个数据不会被筛掉呢?

我们将遗留下来的数除以二,因为只会剩下偶数位的数据。那么到最后一轮,唯一的那个数据就变成 11 了。

我们每轮会将这个数除以二,进行了 ⌊log⁡2x⌋\lfloor\log_2x\rfloor 轮。于是他就是 2⌊log⁡2x⌋2^{\lfloor\log_2x\rfloor}。

我们发现,这等价于第一个小于等于 xx 的数。我们只需要一点点往上翻番即可。

注意

数据高达 105010^{50} 需要邪恶高精度,或者用 python。

AC Code
C++
//警示后人:十年OI一场空,不开long long见祖宗
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define AC 0
using ll=long long;
using pll=pair<int,int>;
template<typename T>
inline void readinVector(vector<T>& vec,int l,int r){
	for(int i=l;i<=r;i++)cin>>vec[i];
}
template<typename T>
class matvec{
public:
	vector<vector<T> >mat;
	matvec(){}
	matvec(int n){
		mat.resize(n+1);
	}
	void rs(int n){
		mat.resize(n+1);
	}
	matvec(int n,int m){
		mat.resize(n+1,vector<T>(m+1));
	}
	matvec(int n,int m,T v){
		mat.resize(n+1,vector<T>(m+1,v));
	}
	vector<T>& operator[](int i){
		return mat[i];
	}
	void p_b(const vector<T>&vec){
		 mat.push_back(vec);
	}
};
struct Bigint{
    short num[10000];
    int len;
    Bigint(){ len=0; memset(num,0,sizeof(num)); }
    ~Bigint(){}
    Bigint(int x){
        len=0; memset(num,0,sizeof(num));
        if(x==0){ num[len++]=0; return; }
        while(x>0){ num[len++]=x%10; x/=10; }
    }
    Bigint(const string&s){
        len=0; memset(num,0,sizeof(num));
        for(int i=s.size()-1;i>=0;i--) num[len++]=s[i]-'0';
    }
    Bigint&operator=(int x){ *this=Bigint(x); return *this; }
    Bigint&operator=(const string&s){ *this=Bigint(s); return *this; }
    int compare(const Bigint&other)const{
        if(len>other.len)return 1;
        if(len<other.len)return-1;
        for(int i=len-1;i>=0;i--){
            if(num[i]>other.num[i])return 1;
            if(num[i]<other.num[i])return-1;
        }
        return 0;
    }
    Bigint operator-(const Bigint&other)const{
        Bigint res;
        res.len=len;
        int borrow=0;
        for(int i=0;i<len;i++){
            int val=num[i]-other.num[i]-borrow;
            if(val<0){ val+=10; borrow=1; }else borrow=0;
            res.num[i]=val;
        }
        while(res.len>1&&res.num[res.len-1]==0)res.len--;
        return res;
    }
    Bigint operator+(const Bigint&other)const{
        Bigint res;
        int carry=0;
        res.len=max(len,other.len);
        for(int i=0;i<res.len;i++){
            int sum=num[i]+other.num[i]+carry;
            res.num[i]=sum%10; carry=sum/10;
        }
        if(carry!=0)res.num[res.len++]=carry;
        return res;
    }
    Bigint operator*(const Bigint&other)const{
        Bigint res;
        res.len=len+other.len;
        for(int i=0;i<len;i++){
            int carry=0;
            for(int j=0;j<other.len;j++){
                int prod=num[i]*other.num[j]+carry+res.num[i+j];
                res.num[i+j]=prod%10; carry=prod/10;
            }
            if(carry!=0)res.num[i+other.len]+=carry;
        }
        while(res.len>1&&res.num[res.len-1]==0)res.len--;
        return res;
    }
    Bigint multiply_single(int x)const{
        Bigint res;
        int carry=0;
        for(int i=0;i<len;i++){
            int prod=num[i]*x+carry;
            res.num[i]=prod%10; carry=prod/10;
        }
        while(carry>0){ res.num[res.len++]=carry%10; carry/=10; }
        while(res.len>1&&res.num[res.len-1]==0)res.len--;
        if(res.len==0)res.len=1;
        return res;
    }
    Bigint operator/(const Bigint&other)const{
        Bigint res,remainder;
        if(compare(other)<0)return Bigint(0);
        for(int i=len-1;i>=0;i--){
            remainder=remainder.multiply_single(10);
            remainder.num[0]=num[i];
            if(remainder.len==0)remainder.len=1;
            while(remainder.len>1&&remainder.num[remainder.len-1]==0)remainder.len--;
            int cnt=0;
            while(remainder.compare(other)>=0){ remainder=remainder-other; cnt++; }
            res.num[i]=cnt;
        }
        res.len=len;
        while(res.len>1&&res.num[res.len-1]==0)res.len--;
        return res;
    }
    Bigint operator%(const Bigint&other)const{
        Bigint remainder;
        if(compare(other)<0)return *this;
        for(int i=len-1;i>=0;i--){
            remainder=remainder.multiply_single(10);
            remainder.num[0]=num[i];
            if(remainder.len==0)remainder.len=1;
            while(remainder.len>1&&remainder.num[remainder.len-1]==0)remainder.len--;
            while(remainder.compare(other)>=0)remainder=remainder-other;
        }
        return remainder;
    }
    friend ostream&operator<<(ostream&out,const Bigint&b){
        for(int i=b.len-1;i>=0;i--)out<<b.num[i];
        if(b.len==0)out<<0;
        return out;
    }
    friend istream&operator>>(istream&in,Bigint&b){
        string s; in>>s; b=Bigint(s); return in;
    }
    bool operator<=(const Bigint& other){
    	int fl=this->compare(other);
    	if(fl==-1||fl==0){
    		return true;
		}else return false;
	}
}; 
int T=1;
void solve(){
	Bigint n;
	cin>>n;
	Bigint x=1;
	while(x*Bigint(2)<=n)x=x*Bigint(2);
	cout<<x<<endl;
}
signed main(){
//	freopen("xxx.in","r",stdin);
//	freopen("xxx.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
    cin>>T;
	while(T--)solve();
	return AC;
	//AK IOI!;
}

分享一下我的高精度模板:

模板
C++
struct Bigint{
    short num[10000];
    int len;
    Bigint(){ len=0; memset(num,0,sizeof(num)); }
    ~Bigint(){}
    Bigint(int x){
        len=0; memset(num,0,sizeof(num));
        if(x==0){ num[len++]=0; return; }
        while(x>0){ num[len++]=x%10; x/=10; }
    }
    Bigint(const string&s){
        len=0; memset(num,0,sizeof(num));
        for(int i=s.size()-1;i>=0;i--) num[len++]=s[i]-'0';
    }
    Bigint&operator=(int x){ *this=Bigint(x); return *this; }
    Bigint&operator=(const string&s){ *this=Bigint(s); return *this; }
    int compare(const Bigint&other)const{
        if(len>other.len)return 1;
        if(len<other.len)return-1;
        for(int i=len-1;i>=0;i--){
            if(num[i]>other.num[i])return 1;
            if(num[i]<other.num[i])return-1;
        }
        return 0;
    }
    Bigint operator-(const Bigint&other)const{
        Bigint res;
        res.len=len;
        int borrow=0;
        for(int i=0;i<len;i++){
            int val=num[i]-other.num[i]-borrow;
            if(val<0){ val+=10; borrow=1; }else borrow=0;
            res.num[i]=val;
        }
        while(res.len>1&&res.num[res.len-1]==0)res.len--;
        return res;
    }
    Bigint operator+(const Bigint&other)const{
        Bigint res;
        int carry=0;
        res.len=max(len,other.len);
        for(int i=0;i<res.len;i++){
            int sum=num[i]+other.num[i]+carry;
            res.num[i]=sum%10; carry=sum/10;
        }
        if(carry!=0)res.num[res.len++]=carry;
        return res;
    }
    Bigint operator*(const Bigint&other)const{
        Bigint res;
        res.len=len+other.len;
        for(int i=0;i<len;i++){
            int carry=0;
            for(int j=0;j<other.len;j++){
                int prod=num[i]*other.num[j]+carry+res.num[i+j];
                res.num[i+j]=prod%10; carry=prod/10;
            }
            if(carry!=0)res.num[i+other.len]+=carry;
        }
        while(res.len>1&&res.num[res.len-1]==0)res.len--;
        return res;
    }
    Bigint multiply_single(int x)const{
        Bigint res;
        int carry=0;
        for(int i=0;i<len;i++){
            int prod=num[i]*x+carry;
            res.num[i]=prod%10; carry=prod/10;
        }
        while(carry>0){ res.num[res.len++]=carry%10; carry/=10; }
        while(res.len>1&&res.num[res.len-1]==0)res.len--;
        if(res.len==0)res.len=1;
        return res;
    }
    Bigint operator/(const Bigint&other)const{
        Bigint res,remainder;
        if(compare(other)<0)return Bigint(0);
        for(int i=len-1;i>=0;i--){
            remainder=remainder.multiply_single(10);
            remainder.num[0]=num[i];
            if(remainder.len==0)remainder.len=1;
            while(remainder.len>1&&remainder.num[remainder.len-1]==0)remainder.len--;
            int cnt=0;
            while(remainder.compare(other)>=0){ remainder=remainder-other; cnt++; }
            res.num[i]=cnt;
        }
        res.len=len;
        while(res.len>1&&res.num[res.len-1]==0)res.len--;
        return res;
    }
    Bigint operator%(const Bigint&other)const{
        Bigint remainder;
        if(compare(other)<0)return *this;
        for(int i=len-1;i>=0;i--){
            remainder=remainder.multiply_single(10);
            remainder.num[0]=num[i];
            if(remainder.len==0)remainder.len=1;
            while(remainder.len>1&&remainder.num[remainder.len-1]==0)remainder.len--;
            while(remainder.compare(other)>=0)remainder=remainder-other;
        }
        return remainder;
    }
    friend ostream&operator<<(ostream&out,const Bigint&b){
        for(int i=b.len-1;i>=0;i--)out<<b.num[i];
        if(b.len==0)out<<0;
        return out;
    }
    friend istream&operator>>(istream&in,Bigint&b){
        string s; in>>s; b=Bigint(s); return in;
    }
    bool operator<=(const Bigint& other){
    	int fl=this->compare(other);
    	if(fl==-1||fl==0){
    		return true;
		}else return false;
	}
}; 
原文