洛谷【P3612】[USACO17JAN]Secret Cow Code秘密奶牛码
我对分治的理解: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秘密奶牛码的更多相关文章
- 洛谷P3611 [USACO17JAN]Cow Dance Show奶牛舞蹈
题目描述 After several months of rehearsal, the cows are just about ready to put on their annual dance p ...
- 洛谷P3605 [USACO17JAN] Promotion Counting 晋升者计数 [线段树合并]
题目传送门 Promotion Counting 题目描述 The cows have once again tried to form a startup company, failing to r ...
- Secret Cow code(USACO)
题目描述:zxyer为了防止他的标程被别人抄走,他在计算机中的rar文件上设置了一个密码,其中每一个密码都有一个初始字符串,字符串由大写字母组成,且长度不超过30位,每一个密码还有一个询问,询问为一个 ...
- 洛谷P3608 [USACO17JAN]Balanced Photo平衡的照片
P3608 [USACO17JAN]Balanced Photo平衡的照片 题目描述 Farmer John is arranging his NN cows in a line to take a ...
- 洛谷 P3609 [USACO17JAN]Hoof, Paper, Scissor蹄子剪刀…
P3609 [USACO17JAN]Hoof, Paper, Scissor蹄子剪刀… 题目背景 欢迎提供翻译,请直接在讨论区发帖,感谢你的贡献. 题目描述 You have probably hea ...
- 洛谷 P2888 [USACO07NOV]牛栏Cow Hurdles
题目戳 题目描述 Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the ...
- 洛谷P2888 [USACO07NOV]牛栏Cow Hurdles
题目描述 Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the gan ...
- 洛谷 P2986 [USACO10MAR]Great Cow Gat…(树形dp+容斥原理)
P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat… 题目描述 Bessie is planning the annual Great Cow Gathering for c ...
- 洛谷 P3605 [USACO17JAN]Promotion Counting晋升者计数
题目描述 The cows have once again tried to form a startup company, failing to remember from past experie ...
随机推荐
- 九度OJ 1256:找出两个只出现了一次的数字 (位运算)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:568 解决:186 题目描述: 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. 输入: 输入的 ...
- 小程序JSON数组操作
- Python的自省机制
什么是自省? 在日常生活中,自省(introspection)是一种自我检查行为. 在计算机编程中,自省是指这种能力:检查某些事物以确定它是什么.它知道什么以及它能做什么.自省向程序员提供了极大的灵活 ...
- What is the difference between iterations and epochs in Convolution neural networks?
https://stats.stackexchange.com/questions/164876/tradeoff-batch-size-vs-number-of-iterations-to-trai ...
- 【oracle案例】ORA-01722
1.1. ORA-01722 日期:2014-06-05 14:09 环境:測试环境 [情景描写叙述] 在数据库的升级过程中,运行SQL> @?/rdbms/admin/catupgrd ...
- VMware Integrated OpenStack (VIO)简介
VMware Integrated OpenStack是一款由VMware提供支持的OpenStack发行版软件,用于帮助IT在现有的VMware基础架构之上更加轻松地运行基于生产级OpenStack ...
- 通过tile和url判断页面跳转是否正确
通过webdriver中的.title和.current_url获取title和url from time import sleep from selenium import webdriver br ...
- var妙用
var广泛使用其实也有用的.比如在一些不太确定类型的地方 (比如要区分int/uint/long/double的时候),用泛型太牛刀而不用又觉得不灵活的时候,其实是比较推荐var的比如设计某种类的时候 ...
- FHQ_treap
上个月还在舔\(splay\):\(FHQ-treap\)太好打了吧真香 前言 还是建议先把\(splay\)学好再看,讲得会比较粗略(但该有的不会少),或者左转其他文章 \(FHQ-treap\)是 ...
- castle windsor学习-----Fluent Registration API 注册
使用xml配置和fluent注册两种搭配使用需要注意的是: 如果先在WindsorContainer构造函数指明用xml配置进行注册,如下设置 IWindsorContainer container ...