题目链接:http://codeforces.com/contest/897/problem/C

题意:

  给你一些字符串:

    A: [What are you doing at the end of the world? Are you busy? Will you save us?]

    B: [What are you doing while sending "]

    C: ["? Are you busy? Will you send "]

    D: ["?]

  (中括号内为给出的字符串,问号、引号、空格也算在内)

  定义字符串f[0] = A,递推式f[n] = B + f[n-1] + C + f[n-1] + D

  比如:

    f[0] = [What are you doing at the end of the world? Are you busy? Will you save us?]

    f[1] = [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?"?]

  然后有q个询问,每个询问有两个整数n,k,让你输出f[n]的第k个字符。如果f[n]没有第k个字符,则输出'.'。

题解:

  定义la,lb,lc,ld分别代表字符串A,B,C,D的长度:

    la=75, lb=34, lc=32, ld=2

  定义len[i]为字符串f[i]的长度,很容易推出len[i] = (2^i)*la + (2^i - 1)*(lb + lc + ld)。

  因为len[i]有可能很大,而k只有10^18,所以当i>=54的时候,直接返回一个很大的数就行了(比如9e18)。

  对于每个询问,先判断是否k >= len[n]。如果是,直接输出'.'。

  否则就从后往前推位置。

  因为f[n] = B + f[n-1] + C + f[n-1] + D

  所以当前位置k在f[n]中的位置有5种情况:

    (1)k在B中,即k∈[1, lb]

    (2)k在第一个f[n-1]中,即k∈[lb+1, lb+len[n-1]]

    (3)k在C中,即k∈[lb+len[n-1]+1, lb+len[n-1]+lc]

    (4)k在第二个f[n-1]中,即k∈[lb+len[n-1]+lc+1, lb+len[n-1]*2+lc]

    (5)k在D中,即k∈[lb+len[n-1]*2+lc+1, lb+len[n-1]*2+lc+ld]

  对于情况1,3,5,找到k在B,C,D中对应的位置,输出即可:

    (1)k不变

    (3)k -= lb+len[n-1]

    (5)k -= lb+len[n-1]*2+lc

  对于情况2,4,将k变成在f[n-1]中的位置,继续执行这种变换即可。

    (2)k -= lb

    (4)k -= lb+len[n-1]+lc

  最多变换n次,所以总复杂度为O(qn)。

AC Code:

 #include <iostream>
#include <stdio.h>
#include <string.h> using namespace std; long long n,k,q;
long long la=,lb=,lc=,ld=;
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=" \"?";
string s; long long len(long long i)
{
if(i>=) return 9000000000000000000LL;
return ((1LL<<i)-1LL)*(la+lb+lc+ld)+la;
} inline bool cont(long long x,long long l,long long r)
{
return l<=x && x<=r;
} int main()
{
cin>>q;
while(q--)
{
cin>>n>>k;
if(k>len(n))
{
s+=".";
continue;
}
long long form=;
for(long long i=n;i>=;i--)
{
long long temp=len(i-);
bool flag=false;
if(cont(k,,lb)) form=,flag=true;
if(cont(k,lb+temp+,lb+temp+lc)) k-=lb+temp,form=,flag=true;
if(cont(k,lb+temp*+lc+,lb+temp*+lc+ld)) k-=lb+temp*+lc,form=,flag=true;
if(flag) break;
if(cont(k,lb+,lb+temp)) k-=lb;
if(cont(k,lb+temp+lc+,lb+temp*+lc)) k-=lb+temp+lc;
}
if(form==) s+=a[k];
if(form==) s+=b[k];
if(form==) s+=c[k];
if(form==) s+=d[k];
}
cout<<s<<endl;
}

Codeforces 897C Nephren gives a riddle:模拟【珂学】的更多相关文章

  1. CodeForces - 896A Nephren gives a riddle

    A. Nephren gives a riddle time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  2. Codeforces Round #449 (Div. 2)-897A.Scarborough Fair(字符替换水题) 897B.Chtholly's request(处理前一半) 897C.Nephren gives a riddle(递归)

    A. Scarborough Fair time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  3. Codeforces Round #449 [ C/A. Nephren gives a riddle ] [ D/B. Ithea Plays With Chtholly ]

    PROBLEM C/A. Nephren gives a riddle 题 http://codeforces.com/contest/896/problem/A codeforces 896a 89 ...

  4. CF&&CC百套计划1 Codeforces Round #449 A. Nephren gives a riddle

    http://codeforces.com/contest/896/problem/A 第i个字符串嵌套第i-1个字符串 求第n个字符串的第k个字母 dfs #include<map> # ...

  5. jzoj5988. 【WC2019模拟2019.1.4】珂学计树题 (burnside引理)

    传送门 题面 liu_runda曾经是个喜欢切数数题的OIer,往往看到数数题他就开始刚数数题.于是liu_runda出了一个数树题.听说OI圈子珂学盛行,他就在题目名字里加了珂学二字.一开始liu_ ...

  6. JZOJ5988 珂学计树题

    题意 liu_runda曾经是个喜欢切数数题的OIer,往往看到数数题他就开始刚数数题.于是liu_runda出了一个数树题.听说OI圈子珂学盛行,他就在题目名字里加了珂学二字.一开始liu_rund ...

  7. codeforces 723B Text Document Analysis(字符串模拟,)

    题目链接:http://codeforces.com/problemset/problem/723/B 题目大意: 输入n,给出n个字符的字符串,字符串由 英文字母(大小写都包括). 下划线'_' . ...

  8. Codeforces Round #304 C(Div. 2)(模拟)

    题目链接: http://codeforces.com/problemset/problem/546/C 题意: 总共有n张牌,1手中有k1张分别为:x1, x2, x3, ..xk1,2手中有k2张 ...

  9. Codeforces 749C:Voting(暴力模拟)

    http://codeforces.com/problemset/problem/749/C 题意:有n个人投票,分为 D 和 R 两派,从1~n的顺序投票,轮到某人投票的时候,他可以将对方的一个人K ...

随机推荐

  1. Android开发和Android Studio使用教程

    Android studio安装和简单介绍http://www.jianshu.com/p/36cfa1614d23 是时候把Android 项目切换到Android Studio http://ww ...

  2. LVS+Keepalived(DR模式)学习笔记

    1.简述 在互联网的中型项目中,单服务器往往已经无法满足业务本身的性能要求,这时候就会平行扩展,把负载分摊到数台服务器上(集群).一般实现集群有DNS轮询,LVS,nginx负载均衡. 集群主要目的包 ...

  3. shell脚本57问

    [1]交互方式.非交互方式.Shell脚本是什么? 经常与linux打交道,肯定对shell这个词不陌生.不明白shell意思的,可以自行翻译:外壳.去壳. 这个翻译结果怎么可以与计算机系统联系起来呢 ...

  4. ASP.NET动态网站制作(15)-- SQL数据库(1)

    前言:数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,用户可以对文件中的数据进行增.删.改.查.数据库有很多种类型,从简单的存储有各种数据的表格到能都进行海量数据存储的大型数据库 ...

  5. Lumen开发:lumen源码解读之初始化(2)——门面(Facades)与数据库(db)

    版权声明:本文为博主原创文章,未经博主允许不得转载. 紧接上一篇 $app->withFacades();//为应用程序注册门面. $app->withEloquent();//为应用程序 ...

  6. stm32DMA通道 ADC通道

    DMA: 1.使用DAC的时候.将转化后得到的模拟信号通过IO口输出的时候.为什么还将IO口配置能输入模式 PS:stm32手冊上定义PA4和PA5分别和DAC1通道和DAC2通道相连  : DMA1 ...

  7. 基于jquery的bootstrap在线文本编辑器插件Summernote (转)

    Summernote是一个基于jquery的bootstrap超级简单WYSIWYG在线编辑器.Summernote非常的轻量级,大小只有30KB,支持Safari,Chrome,Firefox.Op ...

  8. poj2891(线性同余方程组)

    一个exgcd解决一个线性同余问题,多个exgcd解决线性同余方程组. Strange Way to Express Integers Time Limit: 1000MS   Memory Limi ...

  9. vs05字节对齐问题又一不小心就弄去了我一个下午的时间

    由于一字节的对齐问题,我调一个库调了我基本一个下午..... 犯错其实并不可怕, 可怕的是你一犯再犯...... 这也算得上是难能可贵... /Zp (Struct Member Alignment) ...

  10. StackOverflow&&Quora&&More 翻译系列——目录

    启动了一个翻译系列,主要收录个人在伯乐在线上翻译的文章,或者在 StackOverflow.Quora 及其他资讯站上发现的好文,选文比较偏个人喜好.希望能够学习.理解文章的同时提高英语水平,并共享知 ...