标签:sharp open 英文 ids 范围 can col ace div
题目英文版:
The only difference between the easy and the hard versions is the maximum value of k.
You are given an infinite sequence of form "112123123412345……" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from 1 to 1, the second one — from 1 to 2, the third one — from 1 to 3, ……, the i-th block consists of all numbers from 1 to i.
So the first 56 elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the 1-st element of the sequence is 1, the 3-rd element of the sequence is 2, the 20-th element of the sequence is 5, the 38-th element is 2, the 56-th element of the sequence is 0.
Your task is to answer q independent queries. In the i-th query you are given one integer ki. Calculate the digit at the position ki of the sequence
然后是中文版:
有一个无限长的数字序列,其组成为1 1 2 1 2 3 1.......1 2 ... n...
,即重复的1~1,1~2....1~n
,给你一个k
,求第k(k<=1e18)
个数字是什么
OK!
其实这题是加强版,普通版我都没思路,更别说他了。
然后我就看看题解吧!借鉴大佬们的思路终于解决了。
基本思路都一样,首先找到第K位在哪个数字里,然后找具体的是哪个数字,就解决了。
中途用二分就行了。但是这道题数据范围太大了。
我都无语了。
所以普通的预处理是解决不了的。
需要用到等差数列。
1 now[1]=1; 2 now[2]=9+2; 3 now[3]=9+90*2+3; 4 now[4]=9+90*2+900*3+4; 5 now[5]=9+90*2+900*3+9000*4+5; 6 now[6]=9+90*2+900*3+9000*4+90000*5+6; 7 now[7]=9+90*2+900*3+9000*4+90000*5+900000*6+7; 8 now[8]=9+90*2+900*3+9000*4+90000*5+900000*6+9000000*7+8; 9 now[9]=9+90*2+900*3+9000*4+90000*5+900000*6+9000000*7+90000000*8+9;
就是这个,大家应该能看懂。
然后就是具体的二分了,其是代码不难,就是细节太多了。
然后是代码:
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> using namespace std; const int maxn=16; typedef long long ll; ll a[maxn],now[maxn]; ll suan(ll n,ll d,ll a1){ return n*a1+(n-1)*n/2*d; } ll Get(ll a1,ll d,ll u){ ll l=1,r=9,mids; for(ll i=1;i<d;i++)r*=10; while(l<=r){ mids=(l+r)/2; if(suan(mids,d,a1)==u)return mids-1; else if(suan(mids,d,a1)<u) l=mids+1; else r=mids-1; } if(l>r) swap(l,r); if(suan(r,d,a1)<u) return r; else return l; } int main(){ //freopen("a.in","r",stdin); now[1]=1; now[2]=9+2; now[3]=9+90*2+3; now[4]=9+90*2+900*3+4; now[5]=9+90*2+900*3+9000*4+5; now[6]=9+90*2+900*3+9000*4+90000*5+6; now[7]=9+90*2+900*3+9000*4+90000*5+900000*6+7; now[8]=9+90*2+900*3+9000*4+90000*5+900000*6+9000000*7+8; now[9]=9+90*2+900*3+9000*4+90000*5+900000*6+9000000*7+90000000*8+9; ll t;scanf("%lld",&t); ll cnt=9; for(int i=1;i<=9;i++){ a[i]=now[i]*cnt+(cnt-1)*(cnt)/2*i; cnt*=10;a[i]+=a[i-1]; } while(t--){ ll u;scanf("%lld",&u); ll all; for(all=1;all<=9;all++){ if(a[all]>=u) break; } u-=a[all-1]; ll xian=Get(now[all],all,u); ll hou=suan(xian,all,now[all]); u-=hou; ll jl=0,v=9,z=1; while(u>=v*z){ jl++; u-=(v*z); v*=10,z++; } jl++; if(u%jl==0){ u/=jl; ll q=1; for(ll i=1;i<jl;i++){ q*=10; } u=q+u-1; printf("%lld\n",u%10); } else { ll ff=u-u/jl*jl; u/=jl; ll q=1; for(ll i=1;i<jl;i++){ q*=10; } u=q+u; ll Vector[maxn],Vcnt=0; while(u){ Vector[++Vcnt]=u%10; u/=10; } printf("%lld\n",Vector[Vcnt-ff+1]); } } return 0; }
1 #include <cstdio> 2 #include <iostream> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 const int maxn=16; 7 typedef long long ll; 8 ll a[maxn],now[maxn]; 9 ll suan(ll n,ll d,ll a1){ 10 return n*a1+(n-1)*n/2*d; 11 } 12 ll Get(ll a1,ll d,ll u){ 13 ll l=1,r=9,mids; 14 for(ll i=1;i<d;i++)r*=10; 15 while(l<=r){ 16 mids=(l+r)/2; 17 if(suan(mids,d,a1)==u)return mids-1; 18 else if(suan(mids,d,a1)<u) l=mids+1; 19 else r=mids-1; 20 } 21 if(l>r) swap(l,r); 22 if(suan(r,d,a1)<u) return r; 23 else return l; 24 } 25 int main(){ 26 //freopen("a.in","r",stdin); 27 now[1]=1; 28 now[2]=9+2; 29 now[3]=9+90*2+3; 30 now[4]=9+90*2+900*3+4; 31 now[5]=9+90*2+900*3+9000*4+5; 32 now[6]=9+90*2+900*3+9000*4+90000*5+6; 33 now[7]=9+90*2+900*3+9000*4+90000*5+900000*6+7; 34 now[8]=9+90*2+900*3+9000*4+90000*5+900000*6+9000000*7+8; 35 now[9]=9+90*2+900*3+9000*4+90000*5+900000*6+9000000*7+90000000*8+9; 36 ll t;scanf("%lld",&t); 37 ll cnt=9; 38 for(int i=1;i<=9;i++){ 39 a[i]=now[i]*cnt+(cnt-1)*(cnt)/2*i; 40 cnt*=10;a[i]+=a[i-1]; 41 } 42 while(t--){ 43 ll u;scanf("%lld",&u); 44 ll all; 45 for(all=1;all<=9;all++){ 46 if(a[all]>=u) break; 47 } 48 u-=a[all-1]; 49 ll xian=Get(now[all],all,u); 50 ll hou=suan(xian,all,now[all]); 51 u-=hou; 52 ll jl=0,v=9,z=1; 53 while(u>=v*z){ 54 jl++; 55 u-=(v*z); 56 v*=10,z++; 57 } 58 jl++; 59 if(u%jl==0){ 60 u/=jl; 61 ll q=1; 62 for(ll i=1;i<jl;i++){ 63 q*=10; 64 } 65 u=q+u-1; 66 printf("%lld\n",u%10); 67 } else { 68 ll ff=u-u/jl*jl; 69 u/=jl; 70 ll q=1; 71 for(ll i=1;i<jl;i++){ 72 q*=10; 73 } 74 u=q+u; 75 ll Vector[maxn],Vcnt=0; 76 while(u){ 77 Vector[++Vcnt]=u%10; 78 u/=10; 79 } 80 printf("%lld\n",Vector[Vcnt-ff+1]); 81 } 82 } 83 return 0; 84 }
两个代码,来找不同了。
两个代码都能过哟!
哟啥不同?
这是答案:其实没有不同,哈哈哈哈哈哈!
标签:sharp open 英文 ids 范围 can col ace div
原文地址:https://www.cnblogs.com/DZN2004/p/12793279.html