我对分治的理解:https://www.cnblogs.com/AKMer/p/9728574.html

题目传送门:https://www.luogu.org/problemnew/show/P3612

因为每次操作都会使原串增长一倍,并且后半部分与前半部分极其相似,所以我们可以将其分治。

因为是将原串的最后一个字符放到第一个来,其余字符全部后移一位,所以在后一半的第\(i\)位就是前一半的第\(i-1\)位,如果是后一半的第\(1\)位,那么就是前一半的第\(len\)位。我们每次分治可以将串长减少一半,所以只需要\(logn\)的时间就可以将询问的位数减小至最开始的原串长度以内,就可以直接出结果了。

时间复杂度:\(O(logn)\)

空间复杂度:\(O(1)\)

代码如下:

#include <cstdio>
#include <cstring>
using namespace std;
#define ll long long ll n;
int len;
char s[35]; char find(ll id) {
if(id<=len)return s[id];//如果到初始串内了就直接返回答案
ll tmp=len;
while((tmp<<1)<id)tmp<<=1;//tmp就是前半部分的长度
ll new_id=id-tmp-1;if(!new_id)new_id=tmp;//new_id是id位在前半部的位置
return find(new_id);//递归去找
} int main() {
scanf("%s%lld",s+1,&n);
len=strlen(s+1);
printf("%c",find(n));//找第n为
return 0;
}

洛谷【P3612】[USACO17JAN]Secret Cow Code秘密奶牛码的更多相关文章

  1. 洛谷P3611 [USACO17JAN]Cow Dance Show奶牛舞蹈

    题目描述 After several months of rehearsal, the cows are just about ready to put on their annual dance p ...

  2. 洛谷P3605 [USACO17JAN] Promotion Counting 晋升者计数 [线段树合并]

    题目传送门 Promotion Counting 题目描述 The cows have once again tried to form a startup company, failing to r ...

  3. Secret Cow code(USACO)

    题目描述:zxyer为了防止他的标程被别人抄走,他在计算机中的rar文件上设置了一个密码,其中每一个密码都有一个初始字符串,字符串由大写字母组成,且长度不超过30位,每一个密码还有一个询问,询问为一个 ...

  4. 洛谷P3608 [USACO17JAN]Balanced Photo平衡的照片

    P3608 [USACO17JAN]Balanced Photo平衡的照片 题目描述 Farmer John is arranging his NN cows in a line to take a ...

  5. 洛谷 P3609 [USACO17JAN]Hoof, Paper, Scissor蹄子剪刀…

    P3609 [USACO17JAN]Hoof, Paper, Scissor蹄子剪刀… 题目背景 欢迎提供翻译,请直接在讨论区发帖,感谢你的贡献. 题目描述 You have probably hea ...

  6. 洛谷 P2888 [USACO07NOV]牛栏Cow Hurdles

    题目戳 题目描述 Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the ...

  7. 洛谷P2888 [USACO07NOV]牛栏Cow Hurdles

    题目描述 Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the gan ...

  8. 洛谷 P2986 [USACO10MAR]Great Cow Gat…(树形dp+容斥原理)

    P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat… 题目描述 Bessie is planning the annual Great Cow Gathering for c ...

  9. 洛谷 P3605 [USACO17JAN]Promotion Counting晋升者计数

    题目描述 The cows have once again tried to form a startup company, failing to remember from past experie ...

随机推荐

  1. Is this its limit?

    import sys import os curPath = os.path.abspath(os.path.dirname(__file__)) rootPath = os.path.split(c ...

  2. less (css预处理)

    用法 1. 必须在head内 2. 样式文件必须先加载 <head> <meta charset='utf-8'> <link rel="stylesheet/ ...

  3. HTML/CSS/JS初始化

    CSS <link type="text/css" href="http://www.mazey.cn/css/mazey-base.css" rel=& ...

  4. CSS 布局实例系列(一)总结CSS居中的多种方法

    使用 CSS 让页面元素居中可能是我们页面开发中最常见的拦路虎啦,接下来总结一下常见的几种居中方法吧. 1. 首先来聊聊水平居中: text-align 与 inline-block 的配合 就像这样 ...

  5. linux c编程:文件夹操作

    创建目录: 用mkdir函数创建目录: mkdir(const char *pathname, mode_t mode) 参数mode有下列的组合: S_ISUID 04000 文件的执行时设置用户I ...

  6. BAPI LIST

    [转自 http://blog.csdn.net/minsenwu/article/details/8432081] 库存管理BAPI 库存: 1. BAPI_MATERIAL_AVAILABILIT ...

  7. 第7条:用列表推导式来取代map和filter

    核心知识点: 1.列表推导式要比内置的map和filter函数清晰,因为它无需额外编写lambda表达式. 2.列表推导式可以跳过输入列表中的某些元素,如果改用map来做,那就必须辅以filter方能 ...

  8. HTML 获取屏幕,浏览器,页面的高度

    1,物理尺寸和分辨率 容器的尺寸是指当前分辨率下的高度.宽度,而不是物理高度.宽度. 如:一个22寸的显示器,屏幕分辨率为1366 * 768,那么获取到的屏幕高度为1366px,宽度为768px. ...

  9. Java编程思想(第4版) 中文清晰PDF完整版

    Java编程思想(第4版) 中文清晰PDF完整版 [日期:2014-08-11] 来源:Linux社区  作者:Linux [字体:大 中 小]     <Java编程思想>这本书赢得了全 ...

  10. 每天一个Linux命令(27)gzip命令

    zip命令用来压缩文件.gzip是个使用广泛的压缩程序,文件经它压缩过后,其名称后面会多处“.gz”扩展名.     (1)用法:     用法:  gzip [选项参数][-s <压缩字尾字符 ...