Gym 100917L Liesbeth and the String 规律&&胡搞
题目:
Description
Statements
Little Liesbeth likes to play with strings. Initially she got a string of length n, consisting of letters 'a' only.
Then Liesbeth performs next operations with the string:
- If the first letter of the string is 'a', then she adds to the end of the string "bc", and after that removes first two letters of the string.
- If the first letter of the string is 'b', then she adds to the end of the string 'a', and after that removes first two letters of the string.
- If the first letter of the string is 'c', then she adds to the end of the string 'aaa" and after that removes first two letters of the string.
Liesbeth stops when she has the string of length 1. For example, if n = 4, she needs 6 operations :
Liesbeth found that for some n number of operations is too big, and its hard to understand if the process is finite. So she asked You to write the program to help her.
Input
First line of the input contains one integer n (2 ≤ n ≤ 106) — length of the initial string.
Output
Print one integer — number of operations needed to obtain string of length 1. If process is infinite, print - 1 insteaSample Input
4
6
3
24 题目大意:给出一个字符串(只含a)的初始长度,做一下变换,求得到字符串长度为1时的步骤数,变换如下:
1.字符串首字母为a时 字符串末尾加bc 并删除前2个字符
2.字符串首字母为b时 字符串末尾加a 并删除前2个字符
3.字符串首字母为c时 字符串末尾加aaa 并删除前2个字符 题目思路:找规律,
当字符串长度为偶数时:经过n/2步变为只含bc长度为n的形式,再经过n/2步变成只含a长度为n/2的形式
当字符串长度为奇数时:经过(n+1)/2 步变为含c+k(bc)形式长度为n的串,在经过(n+1)/2步变成只含a长度为n/2*3+2的串
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<queue>
#include<math.h>
#define INF 0x3f3f3f3f
#define MAX 10000005
#define Temp 1000000000 using namespace std; int main()
{
long long n,sum;
while(scanf("%lld",&n)!=EOF)
{
sum=;
while(n>)
{
if(n%==)
{
while(n> && n%==)
{
sum+=n;
n/=;
}
} else
{
while(n% && n>)
{
sum+=(n/+);
sum+=(n/+);
n=n/*+;
}
}
}
printf("%lld\n",sum);
}
return ;
}
Gym 100917L Liesbeth and the String 规律&&胡搞的更多相关文章
- HDU 4690 EBCDIC (2013多校 1005题 胡搞题)
EBCDIC Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)Total Su ...
- [LeetCode]Integer Break(Dp或胡搞或推公式)
343. Integer Break Given a positive integer n, break it into the sum of at least two positive intege ...
- 胡搞-强化版的light oj-1055-的思路-AI版的6重暴力For循环的BFS
新题目大意: 三个棋子按照先后顺序,可以随意方向合法地走到空位置上(而不是像原题light oj-1055中的一样三个棋子每次走的方向都一致),当三个棋子全部走进目标地点,就结束:求需要指挥的最少次数 ...
- Codeforces Gym 100015A Another Rock-Paper-Scissors Problem 找规律
Another Rock-Paper-Scissors Problem 题目连接: http://codeforces.com/gym/100015/attachments Description S ...
- Codeforces Gym 100114 A. Hanoi tower 找规律
A. Hanoi tower Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descript ...
- Gym 101981G - Pyramid - [打表找规律][2018-2019 ACM-ICPC Asia Nanjing Regional Contest Problem G]
题目链接:http://codeforces.com/gym/101981/attachments The use of the triangle in the New Age practices s ...
- Codeforces Gym 100425D D - Toll Road 找规律
D - Toll RoadTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view ...
- codeforces Gym 100418D BOPC 打表找规律,求逆元
BOPCTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.action?c ...
- HDU 4681 String 胡搞
设串C的第一个字母在串A中出现的位置是stA, 串C的最后一个字母在串A中出现的位置是edA. 设串C的第一个字母在串B中出现的位置是stB, 串C的最后一个字母在串B中出现的位置是edB. 求出每一 ...
随机推荐
- 【LeetCode】434. Number of Segments in a String
Count the number of segments in a string, where a segment is defined to be a contiguous sequence of ...
- 删除putty的session 以及 putty的颜色设置值
HKEY_CURRENT_USER\Software\SimonTatham\PuTTY 将sessions里面的内容清空即可 ==================================== ...
- 深入浅出聊Unity3D项目优化:从Draw Calls到GC
前言: 刚开始写这篇文章的时候选了一个很土的题目...<Unity3D优化全解析>.因为这是一篇临时起意才写的文章,而且陈述的都是既有的事实,因而给自己“文(dou)学(bi)”加工留下的 ...
- Python之生产者&、消费者模型
多线程中的生产者和消费者模型: 生产者和消费者可以用多线程实现,它们通过Queue队列进行通信. import time,random import Queue,threading q = Queue ...
- 解决time_wait过多的问题
#netstat -n | awk ‘/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}’ LAST_ACK 14SYN_RECV 348ESTABLI ...
- iOS 数组字典操作
iOS开发中需要大量对dictionary和array进行操作,因此我们需要一种更加安全可靠的操作方法来避免不必要的crash.当然可以通过自定义dictionary 和array重载增删改查的方法来 ...
- ajax传递的数据类型json传递
$(".del_goods").click(function(){ //删除选中的商品 var clear_data = [];//数组 $("input[name='c ...
- io外挂
c++里最快的io方式是什么呢? 详见这里. 同时给出一个比较常用的方式,就是用fread.然后自己解析文本,而不是用cin或者scanf,见这里: //fast io test #include & ...
- Python笔记2-20151023
一.循环 Python的循环有两种,一种是for...in循环,依次吧list或tuple中的每个元素迭代出来. >>>names = ['Michael','Bob','Tracy ...
- Silverlight中以客户端加载另一项目客户端(先登录后加载的一般实现)
近几日,本人在对一个老的Silverlight的GIS项目进行维护,发现该项目的实现是先把所有的资源加载至客户端,其中包括登录部分和业务实现部分,我就在想可不可以把登录部分和业务实现部分开来.如果用户 ...