【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?333,?4becomes?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
- 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 KOutput
Print the?K-th character from the left in Mr. Infinity's string after?5×1015days.
Sample Input 1
1214 4Sample Output 1
2The 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 157Sample Output 2
3The initial string is?3. The string after?5×1015?days consists only of?3.
Sample Input 3
299792458 9460730472580800Sample 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 ;}?
總結
以上是生活随笔為你收集整理的【AtCoder - 4242 】To Infinity(思维)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 券商拆解比亚迪元EV写研报火了!当事人回
- 下一篇: 中柏EZbook X6笔记本上手:2K价