1. 程式人生 > >【AtCoder - 4242 】To Infinity(思維)

【AtCoder - 4242 】To Infinity(思維)

題幹:

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 in S is replaced with 22. Similarly, each 3 becomes 3334
    becomes 44445 becomes 555556 becomes 6666667 becomes 77777778 becomes 88888888 and 9 becomes 9999999991 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

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

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

解題報告:

   因為是個給定的數字啊!!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 ;
 }