首页 > 技术文章 > codeforces896A Nephren gives a riddle

RGBTH 2017-12-06 16:55 原文

A. 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.

递归,对于每个n都可以把字符串分解成五部分,left+f[n-1]+mid+f[n-1]+right;

如果k<=left.length;          输出left[k-1]

如果left.length<k<=length.f[n-1]            k-left,return display(n-1,k);

如果length.f[n-1]+left.length<k<=length.f[n-1]+left.length+mid.length    k-length.f[n-1]+left.length,输出·mid[k-1];

如果length.f[n-1]+left.length+mid.length<k<=2*length.f[n-1]+left.length+mid.length     k-length.f[n-1]+left.length+mid.length ,return display(n-1,k);

否则 k-2*length.f[n-1]+left.length+mid.length ,输出right[k-1];

注意当n>53时长度就大于1e18了,所以要特判下,如果小于(n-53)*right.length,取模然后输出right[k-1],不然减去(n-53)*right.length,然后当n=53处理就好了。

很有意思的递归题

下面是我的ac代码:

#include<iostream>
#include<map>
#include<algorithm>
#include<string>
#include<cstdio>
#include<vector>
#include<functional>
#include<set>
#include<string>
#define ll long long
#define kg " "
using namespace std;
ll a[60];
string s1="What are you doing at the end of the world? Are you busy? Will you save us?";
string s2="What are you doing while sending \"";
string s3="\"? Are you busy? Will you send \"";
string s4="\"?";
ll len1=s1.size();
ll len2=s2.size();
ll len3=s3.size();
ll len4=s4.size();

void display(ll n,ll k){
if(k>a[n]){
cout<<".";
return;}
else {
if(n==0){
cout<<s1[k-1];
return ;
}
else{
if(k<=len2){
cout<<s2[k-1];
return;
}
else if(k>len2&&k<=(len2+a[n-1])){
k-=len2;
display(n-1,k);
}
else if(k>len2+a[n-1]&&k<=len2+a[n-1]+len3){
k-=len2+a[n-1];

cout<<s3[k-1];
return ;
}
else if(k>len2+a[n-1]+len3&&k<=len2+2*a[n-1]+len3){
k-=len2+a[n-1]+len3;
display(n-1,k);
}
else
{
k-=len2+len3+2*a[n-1];
cout<<s4[k-1];
return ;
}
}
}
}
int main()
{

a[0]=len1;
for(ll i=1;i<=53;i++)
a[i]=2*a[i-1]+len2+len3+len4;
int t;

cin>>t;
ll n[100005];
ll k[100005];
for(int i=1;i<=t;i++)
cin>>n[i]>>k[i];
for(int i=1;i<=t;i++)
{
if(n[i]>53){
if(k[i]<(n[i]-53)*len2){
cout<<s2[(k[i]%len2)-1];

}
else{
k[i]-=len2*(n[i]-53);
display(53,k[i]);

}
}
else
display(n[i],k[i]);
}
cout<<endl;
return 0;
}

推荐阅读