跳到正文

个人记录

P12565 [UTS 2024] Big

LYJ240049约 7 分钟
原文
文章信息与版本

文章信息

发布
2026年7月8日
收录
2026年8月4日
更新
2026年11月13日
篇幅
约 7 分钟

版本

  1. v1当前
C++
//等价于按照分数$fact{x}{10^{len(x)}-1}$从大到小排序
//二分得到T后,找哪个长度的哪个数字,其分数恰好等于$fact{T}{D}$

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using i128=__int128_t;

const ll D=1000000000000000000LL;
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 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 dig(ll n,int pos){//第pos位
	int l=len(n);
	int shift=l-1-pos;
	for(int i=0;i<shift;i++)n/=10;
	return'0'+(n%10);
}

struct Grp{
	ll L,R;
	int len;
	ll d;

  Grp(ll l,ll r,int len):L(l),R(r),len(len){//[L,R]内长度len
	d=pow10[len]-1;
  }

  inline i128 gt(i128 wn,i128 wd)const{//值>wn/wd
    if(L>R)return 0;
    i128 th=(wn*d)/wd;
    th++;
    ll st=max((ll)th,L);
    return(st>R)?0:(i128)(R-st+1)*len;
  }

  inline i128 ge(i128 wn,i128 wd)const{//值>=wn/wd
    if(L>R)return 0;
    i128 th=(wn*d)/wd;
    if((wn*d)%wd!=0)th++;
    ll st=max((ll)th,L);
    return(st>R)?0:(i128)(R-st+1)*len;
  }
};

vector<ll>eqc(ll n0,ll d0,const vector<Grp>&gps){//找值=n0/d0,降序排列
	vector<ll>res;
	res.reserve(gps.size());
	for(const auto&g:gps){
    	if(g.L>g.R)continue;
    	i128 t=(i128)n0*g.d;
    	if(t%d0!=0)continue;
    	ll x=(ll)(t/d0);
    	if(x>=g.L&&x<=g.R)res.push_back(x);
  	}
  	sort(res.rbegin(),res.rend());
  	return res;
}

int main(){
  	pow10[0]=1;
	for(int i=1;i<=MAX_L+1;i++) pow10[i]=pow10[i-1]*10;
	ll a=read(),b=read();
	int q=read();
	vector<ll>qs(q);
	
	for(int i=0;i<q;i++) qs[i]=read();
	//按长度分组
	vector<Grp>gps;
	gps.reserve(MAX_L);
	int l1=len(a),l2=len(b);
	for(int l=l1;l<=l2;l++){
		ll L=max(a,pow10[l-1]);
		ll R=min(b,pow10[l]-1);
		if(L<=R)gps.emplace_back(L,R,l);
	}

  	string ans;
  	ans.reserve(q);
  	for(ll k:qs){
  		//严格大于该阈值的字符数<=k
		//大于等于该阈值的字符数>k
   		ll l=0,r=D,T=-1;
    	while(l<=r){
      		ll mid=l+(r-l)/2;
      		i128 cnt_gt=0,cnt_ge=0;
      		for(const auto&g:gps){
        		cnt_gt+=g.gt(mid,D);
        		cnt_ge+=g.ge(mid,D);
      		}
      		if(k<cnt_gt)l=mid+1;
      		else if(k>=cnt_ge)r=mid-1;
      		else{T=mid;break;}
    	}
    	if(T==-1)T=l;

    	ll n0=-1,d0=-1;
    	i128 pre=0,elen=0;//pre: >该数字分数的字符数;  elen: =该数字分数的字符数
    	for(const auto&g:gps){
      		if(g.L>g.R)continue;
      		i128 tn=(i128)T*g.d;
      		if(tn%D!=0)continue;
      		ll now=(ll)(tn/D);
      		if(now<g.L||now>g.R)continue;
      		pre=elen=0;
      		for(const auto&g2:gps){
        		i128 t=g2.gt(now,g.d);
        		pre+=t;
        		elen+=g2.ge(now,g.d)-t;
      		}
      		if(k>=pre&&k<pre+elen){n0=now;d0=g.d;break;}
    	}
	    if(n0==-1){//没有整除使用近似值
	      for(const auto&g:gps){
	        if(g.L>g.R)continue;
	        ll now=(i128)T*g.d/D;
	        now=max(g.L,min(g.R,now));
	        pre=elen=0;
	        for(const auto&g2:gps){
	          	i128 t=g2.gt(now,g.d);
	          	pre+=t;
	          	elen+=g2.ge(now,g.d)-t;
	        }
	        if(k>=pre&&k<pre+elen){n0=now;d0=g.d;break;}
	      }
	    }
    	vector<ll>cls=eqc(n0,d0,gps);
    	i128 pre_all=0;
    	for(const auto&g:gps)pre_all+=g.gt(n0,d0);
    	i128 pos=k-pre_all;
    	for(ll x:cls){
      		int ln=len(x);
      		if(pos<ln){ans+=dig(x,(int)pos);break;}
    		pos-=ln;
    	}
  	}

  cout<<ans<<endl;
  return 0;
}
原文