[ICPC2017 Hong Kong R] Sets
高精度我敲了快三个小时,还差点被除法运算整崩溃了,还好队友给力,
否则我们怎么能 AK 呢?
但是高精度不是这道题的重点。这题的关键在于发现每个集合都是连续区间,剩下的就是套高精度模板加一点递推。
题目简述
给定正整数 ,定义集合序列:
(注意: 不相等)
将每个集合内的元素分别按升序排序,然后按顺序拼接成一个序列 。
给定 和 ,求 的第 个数。若该位置不存在(即序列长度不足 ),输出 。
关键观察
注意到当 时:
我们发现,所枚举出的集合都是连续的区间。这是巧合吗?
经过进一步的分析,我们发现这并不是巧合,而是本题的一个重要结论:
, 是连续的整数区间,且其左右端点满足递推关系。
我们考虑证明它。证明完,整道题也就完成了一半。
证明(数学归纳法)
当 时,由定义得 ,其显然是一个连续区间,左端点 ,右端点 。
因此我们得出:, 是连续整数区间,记作 ,即从 到 的所有整数。
考虑 。
则 的最小值一定取在区间 中最小的两个不相等的数,即 和 ,因此
同理,最大值出现在取 和 时,即
考虑 在 和 之间的取值:
首先固定 ,令 取遍 ,则 取遍了
这时固定 ,令 取遍 ,此时 取遍了
显然, 取遍了
即
因此,,
那么 也是连续的整数区间。
因此我们得出:,,其中 ,。
又因为 ,其中 ,,则由上可得
终于,我们得到了最终结论:
特别的,,。
推论:长度公式
根据上述结论,我们可知,第 个区间的左右端点:
同时我们还可以推出第 个区间的长度
特别地,当 时, 恒成立,即每个集合长度都是3;当 时, 会随 的增长而变小,直至变为 。 我们稍后就会用这个性质进行特判。
而当且仅当 时,序列 无限长; 时由于序列仅存在 ,故序列长 ;当 时序列为 ,故序列长 。
故当 时,前 个集合的总长度
终于,我们完成了这道题的所有数学推导过程。
解题步骤
-
特判
上文中,我们发现 时有特殊性质,这里我们利用它进行特判:- :由前可得此时序列只有1。故 时输出 ,否则 。
- :由前可得此时序列为 。若 则输出 ,否则 。
- :虽然说此时序列长度无限,但是此时所有集合的长度均为 ,若采用下文提及的方法模拟,复杂度会退化为 ,无法承受。但是我们会发现第 个集合为 ,因此序列 就是将每个这样的 个连续整数依次拼接而成的。由于第 个数可以直接定位,我们可以直接特判:
- 集合编号 ,集合内偏移 ;
- 答案就为 ,即
-
常规情况
直接模拟累加每个集合的长度,直到总长度 。
设 为已累加的总长度,初始 ,(即 )。
当 时,循环执行:- (即 )
循环结束后, 落在当前集合(编号为 )中,偏移量 (从1开始计数)。
当前集合的左端点为 ,因此答案为: -
高精度
由于 可达 ,累加和 远超 位(二进制下其长度更是接近unsigned long long的 倍),我们必须使用高精度。
我手写了一个十进制高精度类(见代码),实现了加减乘除、比较和快速幂()。
因为只需要正整数运算,而幂运算又仅需考虑底数为2的情况,实现起来相对简单。
我不敢说这个高精度类能使用在任何情况(因为快速幂仅支持底数为2的情况),但就本题而言,它绝对可行。
复杂度分析
- 循环次数 约为 ,因为长度是指数增长的。,非常小。
- 每次高精度运算的位数约为 ,所以总复杂度可接受。
赛时代码(AC)
#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 的那一刻真爽。