The input consists of a string followed by N. The string consists of at most 30 uppercase characters, and N≤1018. The code outputs the Nth character of the infinite code built from the initial string. The first character is N=1.
The last character of the string is moved to the first position and then the resulting string is appended to the previous string in each iteration.
SAMPLE INPUT:
COW 8
SAMPLE OUTPUT:
C
In this example, the initial string COW expands as follows:
COW -> COWWCO -> COWWCOOCOWWC
12345678
The problem is that for large values of N eg.107, the code crashes. Can anyone please suggest methods to remove the error.
#include<bits/stdc++.h>
using namespace std;
char sCowCode(string str, long long int N){
long long int a = str.length();
if(N < a){
return str[N-1];
}
while(2*a < N + 1)
a *= 2;
if(a == N)
return sCowCode(str, a-1);
return sCowCode(str, N - a -1);
}
int main(){
long long int N;
string str;
cin >> str;
cin >> N;
char a = sCowCode(str, N);
cout<<a<<endl;
cin.get();
cin.get();
return 0;
}