下午看到这题,第一时间想翻题解结果没翻到
这里给出一种比较好理解的办法供大佬参考
题目大意
给定区间 ,将区间内所有整数按某种顺序拼接,形成最大的数字 。然后有 次查询,每次问 的第 位是什么(从 0 开始编号)。
解题思路
1.排序
我们采用 表示字符串 后面拼接字符串 。我们已知若 ,则 排在 前面,否则 排在 前面。
容易发现 可能很大,一个个拼接肯定不行。众所周知数学是万能的,我们借助数学转化为对应的分数来比较。 对于数字 (十进制位数 ),定义其权重: 则 与 等价
证明
- 证:当 时, 由拼接定义, 则 移项, 即
- 证:当 时, 上面公式倒过来推就好了,不赘述
2.第k位
对于每个查询 ,我们需要在不实际构造整个数字的情况下,找到拼接后数字的第 位。 我们可以将问题分解为三步:
- 确定第 位所在的权重值
- 确定该权重值下的具体数字
- 确定在该数字中的具体位置
第一步:确定第 位所在的权重值 我们需要找到权重值 ,使得:
所有满足 的数字拼接后的总长度 所有满足 的数字拼接后的总长度
换句话说,第 位位于那些权重值正好等于 的数字中。这不明显的二分吗
由于 ,且 ,,所以 的范围是 。我们使用 (其中 )来近似表示 。
设 表示所有权重 的数字的总长度 表示所有权重 的数字的总长度
对于每个长度组 : 满足 的条件是: 解不等式得: 因此最小的 该组的贡献为: 满足 的条件是: 解不等式得: 该组的贡献为:
则对于每个长度组 :
第二步:确定该权重值下的具体数字 二分找到的 只是一个近似值,我们需要找到精确的 使得:
- 是一个有效的权重值(即存在数字 使得 )
我们可以在 附近的小范围内搜索 对于每个 ,令 对于每个长度组 :
- 计算
- 在 附近的小范围内(如 )搜索候选数字 、
- 检查是否满足条件
如果搜索失败,检查每个长度组的边界值,这些边界值可能是权重值的临界点(如 , , 等)
对于候选的 ,计算:
检查是否满足
第三步:确定在该数字中的具体位置 对于找到的 ,我们需要找到所有满足 的数字。 对于每个长度组 ,解方程: 得: 接下来检查条件:
- 必须是整数(即 能被 整除)
- 的位数是
当权重相同时,按数字本身降序排列,前缀长度:,在等价类中的位置:,第 位就是答案。
小细节
- 快读(或关闭同步流)
- 高精乘或 ,避免乘法溢出
- 时间复杂度 ,D为二分范围,G为group数量,但常数较大(等)
以下给出AC代码供参考
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using i128=__int128_t;
const ll D = 1000000000000000000LL;//1e18
const int MAX_L=18;
ll pow10[MAX_L+2];
//快读
char *p1,*p2,buf[100000];
#define nc() (p1==p2 && (p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
inline ll read_ll(){
ll x=0;
char ch=nc();
while(ch<48||ch>57) ch=nc();
while(ch>=48&&ch<=57) x=x*10+ch-48,ch=nc();
return x;
}
inline int len(ll n){
if(n<10) return 1;
int l=0;
while(n>=pow10[l+1]) l++;
return l+1;
}
inline char get_digit(ll n, int pos){
int l=len(n);
int shift=l-1-pos;
for(int i=0;i<shift;i++) n/=10;
return '0'+(n%10);
}
struct node{
ll A,B;
int l;
ll d; //10^l-1
node(ll A,ll B,int l): A(A),B(B),l(l){
d=pow10[l]-1;
}
inline i128 calc_L_gt(i128 w0_n,i128 w0_d) const{
if(A>B) return 0;
i128 threshold=(w0_n*d)/w0_d;
threshold++;
ll n_start=max((ll)threshold,A);
return (n_start>B)?0:(i128)(B-n_start+1)*l;
}
inline i128 calc_L_ge(i128 w0_n,i128 w0_d) const{
if(A>B) return 0;
i128 threshold=(w0_n*d)/w0_d;
if((w0_n*d)%w0_d!=0) threshold++;
ll n_start=max((ll)threshold,A);
return (n_start>B)?0:(i128)(B-n_start+1)*l;
}
inline i128 L_gt_T(ll T) const{return calc_L_gt(T,D);}
inline i128 L_ge_T(ll T) const{return calc_L_ge(T,D);}
};
vector<ll> find_ec(ll n0,ll d0,const vector<node>& groups){
vector<ll> eq_class;
eq_class.reserve(groups.size());
for(const auto& g:groups) {
if(g.A>g.B) continue;
i128 target=(i128)n0*g.d;
if(target%d0!=0) continue;
ll n=(ll)(target/d0);
if(n>=g.A&&n<=g.B){
eq_class.push_back(n);
}
}
sort(eq_class.rbegin(), eq_class.rend());
return eq_class;
}
int main()
{
pow10[0]=1;
for(int i=1;i<=MAX_L+1;i++) pow10[i]=pow10[i-1]*10;
ll a=read_ll(),b=read_ll();
int q=read_ll();
vector<ll> queries(q);
for(int i=0;i<q;i++) queries[i]=read_ll();
vector<node> groups;
groups.reserve(MAX_L);
int l_min=len(a),l_max=len(b);
for(int l=l_min;l<=l_max;l++){
ll A=max(a,pow10[l-1]);
ll B=min(b,pow10[l]-1);
if(A<=B) groups.emplace_back(A,B,l);
}
string ans;
ans.reserve(q);
for(ll k:queries){
ll low=0,high=D,found=-1;
while(low<=high){
ll mid=low+(high-low)/2;
i128 L_gt=0,L_ge=0;
for(const auto& g:groups){
L_gt+=g.L_gt_T(mid);
L_ge+=g.L_ge_T(mid);
}
if(k<L_gt) low=mid+1;
else if(k>=L_ge) high=mid-1;
else{
found=mid;
break;
}
}
if(found==-1) found=low;
ll n0=-1,d0=-1;
i128 tmp_gt=0,tmp_ge=0;
for(const auto& g:groups){
if(g.A>g.B) continue;
i128 target=(i128)found*g.d;
if(target%D!=0) continue;
ll candidate=(ll)(target/D);
if(candidate<g.A||candidate>g.B) continue;
tmp_gt=tmp_ge = 0;
for(const auto& g2:groups){
tmp_gt+=g2.calc_L_gt(candidate,g.d);
tmp_ge+=g2.calc_L_ge(candidate,g.d);
}
if(tmp_gt<=k&&k<tmp_ge){
n0=candidate;
d0=g.d;
break;
}
}
if(n0==-1){
for(const auto& g:groups){
if(g.A>g.B) continue;
ll candidate=(i128)found*g.d/D;
candidate=max(g.A,min(g.B,candidate));
tmp_gt=tmp_ge = 0;
for(const auto& g2:groups){
tmp_gt+=g2.calc_L_gt(candidate,g.d);
tmp_ge+=g2.calc_L_ge(candidate,g.d);
}
if(tmp_gt<=k&&k<tmp_ge){
n0=candidate;
d0=g.d;
break;
}
}
}
vector<ll> eq_class=find_ec(n0,d0,groups);
i128 L_gt_w0=0;
for(const auto& g:groups) L_gt_w0+=g.calc_L_gt(n0,d0);
i128 pos=k-L_gt_w0;
for(ll n:eq_class){
int l_n=len(n);
if(pos<l_n){
ans+=get_digit(n,(int)pos);
break;
}else pos-=l_n;
}
}
cout<<ans;
return 0;
}