给出一个序列,要求求出第i位的值,首先dp1求出范围内每个子串的位数,然后dp2求出前n个dp1子串和的长度
根据给出的i,先找到比dp2大的,减去之后就能知道剩下范围,然后在dp1中找比ans大的,就可以知道当前是第几个数字,注意但ans==0的情况
讲道理,用二分可能会更快一点
#include<iostream>
#include<cstdio>
#include<set>
#include<string>
#include<string.h>
#include<cstring>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<cctype>
#include<algorithm>
#include<sstream>
#include<utility>
#include<cmath>
#include<functional>
#define mt(a) memset(a,0,sizeof (a))
#define fl(a,b,c) fill(a,b,c)
#define SWAP(a,b,t) (t=a,a=b,b=t)
#define inf 1000000000+7
using namespace std;
typedef long long ll;
ll dp1[320000];
ll dp2[320000];
int main()
{
ll ch = 1,we=1;
mt(dp1);
mt(dp2);
for (int i = 1; i < 320000; i++)
{
if (i / ch >= 10)
{
we++;
ch *= 10;
}
dp1[i] = dp1[i - 1] + we;
}
for (int i = 1; i < 320000; i++)
dp2[i] = dp2[i - 1] + dp1[i];
int T;
cin >> T;
while (T--)
{
ll ans = 0;
scanf("%lld", &ans); ll start;
for (int i = 0; i < 320000; i++)
{
if (dp2[i]>ans){ ans -= dp2[i - 1]; if (ans == 0)start = i - 1; break; }
}
if (ans!=0)
for (int i = 0; i < 320000; i++)
{
if (dp1[i]>ans){ ans -= dp1[i - 1]; if (ans == 0)start = i - 1; else start = i; break; }
}
//if (ans == 0)start--;
int wei = (int)log10(start)+1;
stringstream ss;
string str;
ss << start;
ss >> str;
if (ans == 0)printf("%c\n", str[str.size() - 1]);
else printf("%c\n", str[ans - 1]);
}
return 0;
}