Codeforces Round #449 (Div. 2) C. Nephren gives a riddle (DFS)




2017.12.4  烟台正是 大雪纷飞,漫天飞舞,  这是今年的第四场雪, 大雪纷纷何所似?未若柳絮因风起  ?

  


C. Nephren gives a riddle
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
What are you doing at the end of the world? Are you busy? Will you save us?

Nephren is playing a game with little leprechauns.

She gives them an infinite array of strings, f0... ∞.

f0 is "What are you doing at the end of the world? Are you busy? Will you save us?".

She wants to let more people know about it, so she defines fi =  "What are you doing while sending "fi - 1"? Are you busy? Will you send "fi - 1"?" for all i ≥ 1.

For example, f1 is

"What are you doing while sending "What are you doing at the end of the world? Are you busy? Will you save us?"? Are you busy? Will you send "What are you doing at the end of the world? Are you busy? Will you save us?"?". Note that the quotes in the very beginning and in the very end are for clarity and are not a part of f1.

It can be seen that the characters in fi are letters, question marks, (possibly) quotation marks and spaces.

Nephren will ask the little leprechauns q times. Each time she will let them find the k-th character of fn. The characters are indexed starting from 1. If fn consists of less than k characters, output '.' (without quotes).

Can you answer her queries?

Input

The first line contains one integer q (1 ≤ q ≤ 10) — the number of Nephren's questions.

Each of the next q lines describes Nephren's question and contains two integers n and k (0 ≤ n ≤ 105, 1 ≤ k ≤ 1018).

Output

One line containing q characters. The i-th character in it should be the answer for the i-th query.

Examples
input
3
1 1
1 2
1 111111111111
output
Wh.
input
5
0 69
1 194
1 139
0 47
1 66
output
abdef
input
10
4 1825
3 75
3 530
4 1829
4 1651
3 187
4 584
4 255
4 774
2 474
output
Areyoubusy
Note

For the first two examples, refer to f0 and f1 given in the legend.



【题意】

给 n,k   让你 求  k   F(n) 中 k 位置的 字符 是什么

F(n)   给出 定义 是  类似 我是你爸你爸是我爷我是我爸儿我是你爸  这样子  嵌套里定义;

【思路】

嵌套 很容易想到 DFS  ,  注意  字符串 回溯;


四个串 

string a="What are you doing at the end of the world? Are you busy? Will you save us?";
string b="What are you doing while sending \"";
string c="\"? Are you busy? Will you send \"";
string d="\"?";

长度分别为: lla=75,lb=34,lc=32,ld=2;   

由题意 可以看出 F(n)= B + F(n-1) + C + F(n-1)  + D

DFS 根据上面的式子 回溯 

预处理 长度,  从后面 往前扫,   1e18  停止;


【代码实现】

#include <iostream>
#include <bits/stdc++.h>
#include <stdio.h>
#include <string.h>

typedef long long ll;

const ll INF=1e18;
const int MAXN=1e6+5;
using namespace std;

ll la=75,lb=34,lc=32,ld=2;
string a="What are you doing at the end of the world? Are you busy? Will you save us?";
string b="What are you doing while sending \"";
string c="\"? Are you busy? Will you send \"";
string d="\"?";

ll  len[MAXN];

char dfs(ll n,ll k)
{
    if(!n)
    {
        if(k<=la)
            return a[k-1];
        else
            return '.';
    }
    if(k<=lb)
        return b[k-1];
    k-=lb;
    if(k<=len[n-1]||!len[n-1])
        return dfs(n-1,k);
    k-=len[n-1];
    if(k<=lc)
        return c[k-1];
    k-=lc;
    if(k<=len[n-1]||!len[n-1])
        return dfs(n-1,k);
    k-=len[n-1];
    if(k<=ld)
        return d[k-1];
    k-=ld;
    return '.';
}

int main()
{
    //cout<<a.length()<<" "<<b.length()<<" "<<c.length()<<" "<<d.length();

    len[0]=la;
    for(int i=1;len[i-1]<=INF;i++)
    {
        len[i]=2*len[i-1]+(lb+lc+ld);
    }
    int q;
    cin>>q;
    while(q--)
    {
        ll n,k;
        cin>>n>>k;
        putchar(dfs(n,k));
    }
    return 0;
}


13

posted @ 2017-12-04 14:16  Sizaif  阅读(177)  评论(0编辑  收藏  举报