# P3612 [USACO17JAN] Secret Cow Code S Solution

> This article is currently an experimental machine translation and may contain errors. If anything is unclear, please refer to [original Chinese version](<https://blog.wendain.ing/posts/luogu-p3612-secret-cow-code-solution/>). I am continuously working to improve the translation.

# P3612 [USACO17JAN] Secret Cow Code S

## Problem Translation

The cows are experimenting with secret codes, and have devised a method for creating an infinite-length string to be used as part of one of their codes.

Given a string, perform one operation on it (in each correct operation, the last character becomes the new first character), then append the resulting string after the original string. In other words, given an initial string, each subsequent step increases the length of the current string.

Given the initial string and $N$, please help the cows compute the character at position $N$ in the infinite string.

The first line of input contains a string. The string contains at most $30$ uppercase letters, and the data guarantees $N \leq 10^{18}$.

The second line of input contains an integer $N$. Note that the data may be too large to fit into a standard $32$-bit integer container, so you may need to use a $64$-bit integer container (for example, `long long` in C/C++).

Please output the character at index $N$ in the infinite string generated from the initial string. The first character has index $N=1$.

Thanks to @y\_z\_h for the translation

## Problem Statement

The cows are experimenting with secret codes, and have devised a method for creating an infinite-length string to be used as part of one of their codes.

Given a string $s$, let $F(s)$ be $s$ followed by $s$ "rotated" one character to the right (in a right rotation, the last character of $s$ rotates around and becomes the new first character). Given an initial string $s$, the cows build their infinite-length code string by repeatedly applying $F$; each step therefore doubles the length of the current string.


Given the initial string and an index $N$, please help the cows compute the character at the $N$th position within the infinite code string.

## Input Format

The input consists of a single line containing a string followed by $N$. The string consists of at most 30 uppercase characters, and $N \leq 10^{18}$.

Note that $N$ may be too large to fit into a standard 32-bit integer, so you may want to use a 64-bit integer type (e.g., a "long long" in C/C++).

## Output Format

Please output the $N$th character of the infinite code built from the initial string. The first character is $N=1$.

## Sample #1

### Sample Input #1

```TEXT
COW 8
```

### Sample Output #1

```TEXT
C
```

## Hint

In this example, the initial string COW expands as follows:


COW -> COWWCO -> COWWCOOCOWWC

12345678

# Solution

Direct brute-force simulation will MLE, so we must look for a pattern.

Let the initial length of the original string be $len$.

It is not hard to notice that if we split a string into parts A+B, where B is the last character, then after one operation we get the form A+B+B+A.

Suppose at some moment the string's length has grown to $l$, and position n happens to lie in the second half of $l$, i.e., at this point $l$ has "grown to exactly the size where s[n-1] is meaningful".

Consider the case where the n-th character lies in the A part of the second half: by symmetry, we can easily see that the character at position n equals the character at position n-$l$/2-1.

Consider the case where the n-th character lies in the B part of the second half: by symmetry, we can map n back to n-1.

In this way we successfully divide and conquer, transforming the original problem into multiple subproblems.

Now we can keep decreasing n until $len$ ≥ n, at which point we can output directly.

That is, we just need a while(len<n) loop.

The code implementation is as follows

```cpp
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll n,l;
string s;

int main(){
    cin>>s>>n;
    int len=s.length();
    //我们画个图可以发现，当字符串的长度 l 恰好递增到我们可以求出 n 的时候，有 n 处的字符与 n-l/2-1 处的相同
    //并且 n-l/2-1 恰好就在 len 的范围之内
    //如何实现将 n 不断往回倒成 n-l/2-1？
    //还需要特判，当 n 正好卡在中间的时候，也即 n=l/2+1 时，n 往回倒成 n-1（因为中间两个相同）

    //我们需要的是一直往回倒，能倒到直接在最开始的字符串里面求出来
    //也就是说，我们需要先把 l 求到
    //然后这个时候的 n-l/2-1 可以保证一定是在开始的 len 范围之内的
    while(len<n){//假设字符串的长度不够，n 开始就超标使得我们无法直接求出
        l=len; //l 表示当前的字符串长度，这里开始递增
        while(n>l) l*=2;//当字符串的长度还不够整个包住 n 的时候，就一直翻倍
        //上面的循环结束的时候，就是恰好能够包住 n 了（k>=n）
        //依据上面的思路，我们需要 n-l/2-1
        n-=l/2+1; //这样就实现了迭代，n 在不断地减小，通过外层的 while 循环可以反复倒回去
        if(n==0) n=l/2; //即 n=l/2+1 时，n 往回倒成 n-1
    }
    cout<<s[n-1];
}
```
