0
点赞
收藏
分享

微信扫一扫

【AtCoder - 4242 】To Infinity(思维)

林塬 2022-06-15 阅读 44

题干:

Problem Statement

Mr. Infinity has a string S consisting of digits from ​​1​​​ to ​​9​​. Each time the date changes, this string changes as follows:

  • Each occurrence of​​2​​ inSis replaced with​​22​​​. Similarly, each​​3​​​ becomes​​333​​​,​​4​​​becomes​​4444​​​,​​5​​​ becomes​​55555​​​,​​6​​​ becomes​​666666​​​,​​7​​​ becomes​​7777777​​​,​​8​​​ becomes​​88888888​​​ and​​9​​​ becomes​​999999999​​​.​​1​​​ remains as​​1​​.

For example, if S is ​​1324​​​, it becomes ​​1333224444​​​ the next day, and it becomes ​​133333333322224444444444444444​​ the day after next. You are interested in what the string looks like after 5×1015 days. What is the K-th character from the left in the string after 5×1015 days?

Constraints

  • Sis a string of length between 1 and 100 (inclusive).
  • Kis an integer between 1 and 1018 (inclusive).
  • The length of the string after 5×1015 days is at leastK.

Input

Input is given from Standard Input in the following format:

S
K

Output

Print the K-th character from the left in Mr. Infinity's string after 5×1015days.

Sample Input 1

1214
4

Sample Output 1

2

The string S changes as follows:

  • Now:​​1214​
  • After one day:​​12214444​
  • After two days:​​1222214444444444444444​
  • After three days:​​12222222214444444444444444444444444444444444444444444444444444444444444444​

The first five characters in the string after 5×1015 days is ​​12222​​. As K=4, we should print the fourth character, ​​2​​.

Sample Input 2

3
157

Sample Output 2

3

The initial string is ​​3​​​. The string after 5×1015 days consists only of ​​3​​.

Sample Input 3

299792458
9460730472580800

Sample Output 3

2

题目大意:

给定初始的一串数字,之后每一天每个数字会复制该数字次,比如’12223’,下一天变为’1222222333’,如此类推,
问经过5e15天后第k位数字是多少。

解题报告:

   因为是个给定的数字啊!!5e15这个数字谁都承受不了啊!!读入的字符串的大小小于100位数。所以显然我们只需要找前缀1的个数就可以了。如果查询的k大于前缀1的个数,那么就输出1后面的第一个数就是了。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 500 + 5;
ll k,cnt;
int main()
{
string s;
cin>>s;
int len = s.length();
for(int i = 0; i<len; i++) {
if(s[i] != '1') break;
cnt++;
}
cin>>k;
if(k <= cnt) printf("1\n");
else printf("%c\n",s[cnt]);


return 0 ;
}

 


举报

相关推荐

0 条评论