跳到正文

题解

题解:P17188 [ICPC 2017 Hong Kong R] Sets

lin_mine约 22 分钟
原文
文章信息与版本

文章信息

发布
2026年8月3日
收录
2026年8月3日
更新
2026年11月16日
篇幅
约 22 分钟
获赞
1
题目
P17188

版本

  1. v2当前
  2. v1

[ICPC2017 Hong Kong R] Sets

高精度我敲了快三个小时,还差点被除法运算整崩溃了,还好队友给力,否则我们怎么能 AK 呢?
但是高精度不是这道题的重点。这题的关键在于发现每个集合都是连续区间,剩下的就是套高精度模板加一点递推。


题目简述

给定正整数 nn,定义集合序列:

  • S1={1,2,…,n}S_1 = \{1, 2, \dots, n\}
  • Si={x∣x=a+b, a,b∈Si−1, a≠b}S_i = \{x \mid x = a + b,\ a , b \in S_{i-1},\ a \neq b\}

(注意: a,ba, b 不相等)

将每个集合内的元素分别按升序排序,然后按顺序拼接成一个序列 LL。
给定 n≤1000n \leq 1000 和 k≤101000k \leq 10^{1000},求 LL 的第 kk 个数。若该位置不存在(即序列长度不足 kk ),输出 −1-1。


关键观察

注意到当 n=3n = 3 时:

S1={1,2,3},S2={3,4,5},S3={7,8,9},…S_1 =\{1, 2, 3\}, \quad S_2 =\{3, 4, 5\}, \quad S_3 =\{7, 8, 9\}, \quad \dots

我们发现,所枚举出的集合都是连续的区间。这是巧合吗?

经过进一步的分析,我们发现这并不是巧合,而是本题的一个重要结论:

∀i≥1\forall i \geq 1,SiS_i 是连续的整数区间,且其左右端点满足递推关系。

我们考虑证明它。证明完,整道题也就完成了一半。


证明(数学归纳法)

当 i=1i = 1 时,由定义得 Si={1,2,…,n}S_i = \{1,2,\dots,n\},其显然是一个连续区间,左端点 L1=1L_1 = 1,右端点 Ri=nR_i = n。

因此我们得出:∃i≥1\exists i \geq 1,SiS_i 是连续整数区间,记作 Si=[Li,Ri]S_i = [L_i, R_i],即从 LL 到 RR 的所有整数。

考虑 Si+1={x∣x=a+b,a,b∈Si,a≠b}S_{i+1} = \{x|x = a + b, a, b \in S_i, a \neq b \}。

则 Si+1S_{i+1} 的最小值一定取在区间 SiS_i 中最小的两个不相等的数,即 LiL_i 和 Li+1L_i + 1,因此

min⁡Si+1=Li+(Li+1)=2Li+1\min S_{i+1} = L_i + (L_i + 1) = 2L_i + 1

同理,最大值出现在取 Ri−1R_i-1 和 RiR_i 时,即

max⁡Si+1=(Ri−1)+Ri=2Ri−1\max S_{i+1} = (R_i - 1) + R_i = 2R_i-1

考虑 Si+1S_{i+1} 在 min⁡Si+1\min S_{i+1} 和 max⁡Si+1\max S_{i+1} 之间的取值:

首先固定 x=Lix = L_i,令 yy 取遍 Li+1,Li+2,…,RiL_i + 1, L_i + 2, \dots , R_i,则 x+yx + y 取遍了

(2Li+1,2Li+2,…,Li+Ri)(2L_i + 1, 2L_i + 2, \dots , L_i + R_i)

这时固定 y=Riy = R_i,令 xx 取遍 Li+1,Li+2,…,Ri−1L_i + 1, L_i + 2, \dots , R_i - 1,此时 x+yx + y 取遍了

(Li+Ri+1,Li+Ri+2,…,2Ri−1)(L_i + R_i + 1, L_i + R_i + 2, \dots , 2R_i- 1)

显然,x+yx + y 取遍了

(2Li+1,2Li+2,…,2Ri−2,2Ri−1)(2L_i + 1, 2L_i + 2, \dots , 2R_i - 2, 2R_i - 1)

即

Si+1={2Li+1,2Li+2,…,2Ri−2,2Ri−1}S_{i+1} = \{2L_i + 1, 2L_i + 2, \dots , 2R_i - 2, 2R_i - 1 \}

因此,Si+1=[2Li+1,2Ri−1]S_{i+1} = [2L_i+1, 2R_i-1],

那么 Si+1S_{i+1} 也是连续的整数区间。

因此我们得出:∀Si=[Li,Ri]\forall S_i = [L_i, R_i],Si+1=[Li+1,Ri+1]S_{i+1} = [L_{i+1}, R_{i+1}],其中 Li+1=2Li+1L_{i+1} = 2L_i + 1,Ri+1=2Ri−1R_{i+1} = 2R_i - 1。

又因为 S1=[L1,R1]S_1 = [L_1, R_1],其中 L1=1L_1 = 1,R1=nR_1 = n,则由上可得

S2=[L2,R2],S3=[L3,R3],S4=[L4,R4]…S_2 = [L_2, R_2], \quad S_3 = [L_3, R_3], \quad S_4=[L_4, R_4] \quad \dots

终于,我们得到了最终结论:

∀i≥1,Si 是连续的整数区间,且其左端点 Li=2Li−1+1,右端点 Ri=2Ri−1−1\boxed{\forall i \geq 1\text{,}S_i\text{ 是连续的整数区间,且其左端点 }L_i = 2L_{i-1} + 1\text{,右端点 }R_i = 2R_{i-1} - 1}

特别的,L1=1L_1 = 1,R1=nR_1 = n。


推论:长度公式

根据上述结论,我们可知,第 ii 个区间的左右端点:

Li=2i−1,Ri=1+2i−1(n−1)L_i = 2^i - 1, \quad R_i = 1 + 2^{i-1}(n-1)

同时我们还可以推出第 ii 个区间的长度

len⁡i=Ri−Li+1=3+(n−3)⋅2i−1\operatorname{len}_i = R_i - L_i + 1 = 3 + (n - 3) \cdot 2^{i-1}

特别地,当 n=3n=3 时,len⁡i=3\operatorname{len}_i = 3 恒成立,即每个集合长度都是3;当 n≤2n \leq 2 时,len⁡i\operatorname{len}_i 会随 ii 的增长而变小,直至变为 00。 我们稍后就会用这个性质进行特判。

而当且仅当 n≥3n \geq 3 时,序列 LL 无限长;n=1n = 1 时由于序列仅存在 11,故序列长 Len⁡1=1\operatorname{Len}_1 = 1;当 n=2n = 2 时序列为 (1,2,3)(1, 2, 3),故序列长 Len⁡2=3\operatorname{Len}_2 = 3。

故当 n≥3n \geq 3 时,前 tt 个集合的总长度

Pt=∑i=1tlen⁡i=3t+(n−3)(2t−1)P_t = \sum_{i=1}^t \operatorname{len}_i = 3t + (n-3)(2^t - 1)

终于,我们完成了这道题的所有数学推导过程。


解题步骤

  1. 特判 n≤3n \leq 3
    上文中,我们发现 n≤3n \leq 3 时有特殊性质,这里我们利用它进行特判:

    • n=1n = 1:由前可得此时序列只有1。故 k=1k = 1 时输出 11,否则 −1-1。
    • n=2n = 2:由前可得此时序列为 {1,2,3}\{1, 2, 3\} 。若 k≤3k \leq 3 则输出 kk,否则 −1-1。
    • n=3n = 3:虽然说此时序列长度无限,但是此时所有集合的长度均为 33,若采用下文提及的方法模拟,复杂度会退化为 O(k)O(k),无法承受。但是我们会发现第 ii 个集合为 [2i−1,2i+1][2^i - 1, 2^i + 1],因此序列 LL 就是将每个这样的 33 个连续整数依次拼接而成的。由于第 kk 个数可以直接定位,我们可以直接特判:
      • 集合编号 t=⌈k/3⌉t = \lceil k / 3 \rceil,集合内偏移 r=(k−1) mod 3r = (k-1) \bmod 3;
      • 答案就为 ans⁡=(2t−1)+r\operatorname{ans} = (2^t - 1) + r,即
      ans⁡=2t+r−1\boxed{\operatorname{ans} = 2^t + r - 1}
  2. 常规情况n≥4n \geq 4
    直接模拟累加每个集合的长度,直到总长度 ≥k\ge k。
    设 pre⁡\operatorname{pre} 为已累加的总长度,初始 pre⁡=0\operatorname{pre}=0,len⁡=n\operatorname{len} = n(即 len⁡1\operatorname{len}_1)。
    当 pre⁡+len⁡<k\operatorname{pre} + \operatorname{len} < k 时,循环执行:

    • pre⁡←pre⁡+len⁡\operatorname{pre} \leftarrow \operatorname{pre} + \operatorname{len}
    • len⁡←2len⁡−3\operatorname{len} \leftarrow 2 \operatorname{len} - 3(即 len⁡i+1=2len⁡i−3\operatorname{len}_{i+1} = 2 \operatorname{len}_i - 3)
    • t←t+1t \leftarrow t+1

    循环结束后,kk 落在当前集合(编号为 t+1t+1)中,偏移量 d=k−pre⁡d = k - \operatorname{pre}(从1开始计数)。
    当前集合的左端点为 Lt+1=2t+1−1L_{t+1} = 2^{t+1} - 1,因此答案为:

    ans⁡=Lt+1+d−1=2t+1+d−2\boxed{\operatorname{ans} = L_{t+1} + d - 1 = 2^{t+1} + d - 2}
  3. 高精度
    由于 kk 可达 10100010^{1000},累加和 2t+12^{t+1} 远超 6464 位(二进制下其长度更是接近 unsigned long long 的 5252 倍),我们必须使用高精度。
    我手写了一个十进制高精度类(见代码),实现了加减乘除、比较和快速幂(2x2^x)。
    因为只需要正整数运算,而幂运算又仅需考虑底数为2的情况,实现起来相对简单。
    我不敢说这个高精度类能使用在任何情况(因为快速幂仅支持底数为2的情况),但就本题而言,它绝对可行。


复杂度分析

  • 循环次数 tt 约为 log⁡2k\log_2 k,因为长度是指数增长的。k≤101000⇒t≤3322k \le 10^{1000} \Rightarrow t \le 3322,非常小。
  • 每次高精度运算的位数约为 log⁡10k≈1000\log_{10} k \approx 1000,所以总复杂度可接受。

赛时代码(AC)

C++
#include <bits/stdc++.h>
using namespace std;

struct BigInt{//高精度类
    static const int BASE=1000000000,WIDTH=9;
    vector<int>a;
    bool neg;
    BigInt():neg(false){//构造函数,支持从整形、字符串转为此类型
        a.push_back(0);
    }
    BigInt(const string&s){
        fromString(s);
    }
    BigInt(long long x){
        fromString(to_string(x));
    }
    BigInt(int x){
        fromString(to_string(x));
    }
    
    void fromString(const string&s){
        a.clear();
        neg=false;
        string str=s;
        if(str[0]=='-'){
            neg=true;
            str=str.substr(1);
        }
        size_t p=str.find_first_not_of('0');
        if(p==string::npos){
            a.push_back(0);
            return;
        }
        str=str.substr(p);
        for(int i=str.length();i>0;i-=WIDTH){
            int st=max(0,i-WIDTH);
            a.push_back(stoi(str.substr(st,i-st)));
        }
        trim();
        return;
    }
    
    string toString()const{//此类型转为string
        if(a.size()==1&&a[0]==0)
            return"0";
        string res;
        if(neg)
            res+='-';
        res+=to_string(a.back());
        for(int i=a.size()-2;i>=0;--i){
            string t=to_string(a[i]);
            res+=string(WIDTH-t.length(),'0')+t;
        }
        return res;
    }
    
    void trim(){//去除前导零,算是坑点了
        while(a.size()>1&&a.back()==0)
            a.pop_back();
        if(a.size()==1&&a[0]==0)
            neg=false;
    }
    
    //以下为绝对值运算
    static int cmpAbs(const BigInt&x,const BigInt&y){
        if(x.a.size()!=y.a.size())
            return x.a.size()>y.a.size()?1:-1;
        for(int i=x.a.size()-1;i>=0;--i)
            if(x.a[i]!=y.a[i])
                return x.a[i]>y.a[i]?1:-1;
        return 0;
    }
    
    static BigInt addAbs(const BigInt&x,const BigInt&y){
        BigInt r;
        int n=max(x.a.size(),y.a.size())+1;
        r.a.assign(n,0);
        long long carry=0;
        for(int i=0;i<n-1;++i){
            long long sum=carry;
            if(i<x.a.size())
                sum+=x.a[i];
            if(i<y.a.size())
                sum+=y.a[i];
            r.a[i]=sum%BASE;
            carry=sum/BASE;
        }
        r.a[n-1]=carry;
        r.trim();
        return r;
    }
    
    static BigInt subAbs(const BigInt&x,const BigInt&y){
        BigInt r;
        r.a.assign(x.a.size(),0);
        long long borrow=0;
        for(int i=0;i<x.a.size();++i){
            long long diff=-borrow+x.a[i];
            if(i<y.a.size())
                diff-=y.a[i];
            if(diff<0){
                diff+=BASE;
                borrow=1;
            }
            else
                borrow=0;
            r.a[i]=diff;
        }
        r.trim();
        return r;
    }

    //运算符重载,但是都是必须的
    bool operator==(const BigInt&o)const{
        return neg==o.neg&&a==o.a;
    }

    bool operator<(const BigInt&o)const{
        if(neg!=o.neg)
            return neg;
        if(neg)
            return cmpAbs(*this,o)>0;
        return cmpAbs(*this,o)<0;
    }

    bool operator>(const BigInt&o)const{
        return o<*this;
    }
    
    bool operator<=(const BigInt&o)const{
        return *this<o||*this==o;
    }
    
    BigInt operator+(const BigInt&o)const{
        if(neg==o.neg){
            BigInt r=addAbs(*this,o);
            r.neg=neg;
            return r;
        }
        if(cmpAbs(*this,o)>=0){
            BigInt r=subAbs(*this,o);
            r.neg=neg;
            return r;
        }
        else{
            BigInt r=subAbs(o,*this);
            r.neg=o.neg;return r;
        }
    }
    
    BigInt operator-(const BigInt&o)const{
        BigInt t=o;
        t.neg=!o.neg;
        return *this+t;
    }
    
    BigInt operator*(const BigInt&o)const{
        BigInt r;
        r.a.assign(a.size()+o.a.size()+1,0);
        r.neg=neg^o.neg;
        for(int i=0;i<a.size();++i){
            long long carry=0;
            for(int j=0;j<o.a.size();++j){
                long long cur=r.a[i+j]+(long long)a[i]*o.a[j]+carry;
                r.a[i+j]=cur%BASE;
                carry=cur/BASE;
            }
            int idx=i+o.a.size();
            while(carry){
                if(idx>=(int)r.a.size())
                    r.a.push_back(0);
                long long cur=r.a[idx]+carry;
                r.a[idx]=cur%BASE;
                carry=cur/BASE;
                idx++;
            }
        }
        r.trim();
        return r;
    }
    
    BigInt operator/(const BigInt&o)const{
        if(o.a.size()==1&&o.a[0]==0)
            throw runtime_error("Division by zero");
        if(a.size()==1&&a[0]==0)
            return BigInt(0);
        BigInt r;
        r.neg=neg^o.neg;
        BigInt div=*this,divs=o;
        div.neg=false;
        divs.neg=false;
        if(cmpAbs(div,divs)<0)
            return BigInt(0);
        r.a.assign(div.a.size(),0);
        BigInt rem;
        rem.a.assign(1,0);
        for(int i=div.a.size()-1;i>=0;--i){
            rem=rem*BigInt(BASE)+BigInt(div.a[i]);
            int q=0,lo=0,hi=BASE-1;
            while(lo<=hi){
                int mid=(lo+hi)/2;
                if(divs*BigInt(mid)<=rem){
                    q=mid;
                    lo=mid+1;
                }
                else
                    hi=mid-1;
            }
            r.a[i]=q;
            rem=rem-divs*BigInt(q);
        }
        r.trim();
        return r;
    }
    
    BigInt operator%(const BigInt&o)const{
        return *this-(*this/o)*o;
    }
    
    BigInt& operator+=(const BigInt&o){
        *this=*this+o;
        return *this;
    }
       
    BigInt& operator-=(const BigInt&o){
        *this=*this-o;
        return *this;
    }
    
    BigInt operator-()const{
        BigInt r=*this;
        if(!(a.size()==1&&a[0]==0))
            r.neg=!neg;
        return r;
    }
    
    friend ostream& operator<<(ostream&os,const BigInt&x){
        os<<x.toString();
        return os;
    }
};

//快速幂(虽然底数仅支持2,但是够用)
BigInt pow2(const BigInt&exp){
    BigInt res(1),base(2),e=exp;
    while(e>0){
        if(e%2==1)
            res=res*base;
        base=base*base;
        e=e/2;
    }
    return res;
}

BigInt pow(int base,const BigInt&exp){
    return pow2(exp);
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    string s;
    while(cin>>n>>s){//文件末尾可以这么判
        BigInt k=s;
        if(n==1)
            cout<<(k==BigInt(1)?"1\n":"-1\n");
        else if(n==2)
            cout<<(k<=BigInt(3)?k:BigInt(-1))<<'\n';
        else if(n==3){
            BigInt r=(k-1)%3;
            cout<<pow2((k-1)/3+1)-1+r<<'\n';
        }
        else{
            BigInt size=n,tot=n,m=1;
            while(tot<k){
                size=BigInt(2)*size-3;
                tot+=size;
                m+=1;
            }
            BigInt cnt=tot-size;
            cout<<pow2(m)-1+(k-cnt-1)<<'\n';
        }
    }
    return 0;
}

敲高精度真的累,但 AC 的那一刻真爽。

原文
抓取于
  1. __SeaWave__

    %%%%