# 1626: 又是A+B吗？

8
3

## 样例输出

2
2

a[N] , b[N] 。 b[i]：1,2,3, .. i 的长度；  a[i] ： S1S2S3...Sk    k = i 时字符串的长度。

#include <iostream>
#include <cmath>
using namespace std;

long long  a[1000100];
long long b[1000100];

long long find1(int left, int right, long long e) {
int mid;
while(left <  right) {
mid = (left+right)/2;
(e < a[mid]) ? right = mid : left = mid + 1;
}
return --left;
}
long long find2(int left, int right, long long e) {
int mid;
while(left <  right) {
mid = (left+right)/2;
(e < b[mid]) ? right = mid : left = mid + 1;
}
return --left;
}

int main()
{
a[0] = 0; b[0] = 0;
for(int i=1; i<=9; i++) {
b[i] = i;
a[i] = a[i-1] + b[i];
}
//	for(int i=1; i<=9; i++)
//	 cout << a[i] << endl;

for(int i=10; i<=99; i++) {
b[i] = b[i-1] + 2;
a[i] = a[i-1] + b[i];
}
for(int i=100; i<=999; i++) {
b[i] = b[i-1]+3;
a[i] = a[i-1] + b[i];
}
for(int i=1000; i<=9999; i++) {
b[i] =b[i-1]+4;
a[i] = a[i-1] + b[i];
}
for(int i=10000; i<=99999; i++) {
b[i] = b[i-1]+5;
a[i] = a[i-1] + b[i];
}

long long n;
while(cin >> n){
// for(int i=a[999]; i<=a[1000]; i++) {
//      n = i;
int cnt;
cnt = find1(0, 99998, n);
n -= a[cnt];
//cout << cnt << " " << n << endl;
if(n == 0) {
cout << cnt%10 << endl;
continue;
}

cnt = find2(0, 99998, n);
//	cout << n << " " << cnt << endl;

n -= b[cnt];
if(n == 0) {
cout << cnt%10 << endl; // " ";
continue;
}

//	cout << n << " " << cnt << endl;
cnt ++;
while(cnt >= pow(10, n)) {
cnt /= 10;
}
cout << cnt%10 << endl; // " ";
}
return 0;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120