E1 - Numerical Sequence (easy version) 《 Codeforces Round #587 (Div. 3) 》
#include <bits/stdc++.h> using namespace std; #define pb push_back #define ff first #define ss second typedef long long ll; typedef pair<int, int> pii; const int MAXN = 2 * 100 * 1000 + 17; ll q, a[MAXN]; int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); for (int i = 1; i < MAXN; i++) a[i] = a[i - 1] + log10(i) + 1; cin >> q; while (q--) { int k; cin >> k; int x = 1; while (k > a[x]) k -= a[x], x++; int t = upper_bound(a + 1, a + MAXN, k) - a; k -= a[t - 1]; string s; s += to_string(t - 1).back(); s += to_string(t); cout << s[k] << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; // Prioridade typedef long long ll; typedef pair<int,int> pii; typedef pair<ll,ll> pll; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pii> vpi; typedef vector<pll> vpll; #define F first #define S second #define PB push_back #define MP make_pair #define REP(i,a,b) for(int i = a; i < (int)(b); i++) #define INF 0x3f3f3f3f #define INFLL 0x3f3f3f3f3f3f3f3f #define all(x) x.begin(),x.end() #define MOD 1000000007 #define endl '\n' #define mdc(a, b) (__gcd((a), (b))) #define mmc(a, b) (((a)/__gcd(a, b)) * b) #define W(x) cerr << "\033[31m"<< #x << " = " << x << "\033[0m" << endl; // fim da Prioridade int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int q; cin >> q; while(q--){ ll k; cin >> k; ll x = k; ll ans = 0; REP(i,1,x+10000){ ll logg = 0; ll p = i; while(p != 0LL) p/=10LL, logg++; ans += logg; //cout << ans << endl; if(k <= ans){ x = 1; string s; while((int)s.size() < k){ s += to_string(x); x++; } cout << s[k-1] << endl; break; }else{ k -= ans; } } } return 0; }
二分
#include<bits/stdc++.h> #define mid ((l+r)/2) using namespace std; long long sum[50010],l[50010]; long long len(int i){ int re=0; while(i){ re++; i/=10; } return re; } long long n,k=1,t; char z[5000100]; void strint(long long n,char re[]){ int le=0; char te[6]; while(n){ te[le++]=n%10; n/=10; } for(int i=0;i<le;i++) re[i]=te[le-i-1]+'0'; k+=le; } int main(){ for(int i=1;i<=50000;i++){ l[i]=l[i-1]+len(i); sum[i]=sum[i-1]+l[i]; } int num=1; while(k<5000000){ strint(num,z+k); // printf("%d",num); num++; } z[1]='1'; // printf("\n"); // for(int i=1;i<=5000000;i++) // printf("%c",z[i]); // return 0; // for(int i=1;i<1000;i++) // printf("%d ",sum[i]); // return 0; scanf("%d",&n); while(n--){ scanf("%d",&t); int l=0,r=50000; while(l<r-1){ if(sum[mid]<t) l=mid; else r=mid; } t-=sum[l]; printf("%c\n",z[t]); } return 0; }
所遇皆星河