【AtCoder-4242】解题报告(思维,水题)

原始题目

C - To Infinity

  • Time limit : 2sec / Memory limit : 1000MB

  • Score: 300 points

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 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 \times 10^{15}\) days. What is the K-th character from the left in the string after \(5 \times 10^{15}\) days?

Constraints

  • S is a string of length between 1 and 100 (inclusive).
  • K is an integer between 1 and \(10^{18}\) (inclusive).
  • The length of the string after \(5 \times 10^{15}\) 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×1015 days.

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',如此类推, 问经过\(5 \times 10^{15}\)天后第k位数字是多少。

解题思路

由于天数是固定的,而且很大,即便一位2经过5×1015,位数会变为\(2^{5 \times 10^{15}}\) ,远超过 \(10^{18}\) 所以只要判断第几位非1即可。

解题代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define mp make_pair
#define np next_permutation
#define pb push_back
#define all(x) (x.begin(),x.end())
#define rep(i,a,n) for(int i=a;i<n;++i)
#define per(i,a,n) for(int i=n-1;i>=a;--i)

#define fi first
#define se second
using namespace std;
const int maxn=1e4+5;
const int maxl=26;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int,int> pii;
int main()
{
ios::sync_with_stdio(false);
string s;
ull k;
while(cin>>s>>k){
int index=0;
for(int i=0;i<s.length();i++){
if(s[i]!='1'){
index=i;
break;
}
}
if(k-1<index) cout<<1<<endl;
else cout<<s[index]<<endl;
}
}

收获与反思

  • 思考一下即可,不过拓展应该是天数可变的时候,有机会找一下相关题再补吧。