【2015-2016 XVI Open CupL】【找规律】Liesbeth and the String 字符串变换何时变为长度1

L. Liesbeth and the String
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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

Examples
input
4
output
6
input
3
output
24

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 1e6+10, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
char s[N];
int f[1010];
void bf(int len)
{
	int h = 0;
	int t = 0;
	for (int i = 1; i <= len; ++i)s[t++] = 'a';
	while (h+1 < t)
	{
		if (s[h] == 'a')s[t++] = 'b', s[t++] = 'c';
		else if (s[h] == 'b')s[t++] = 'a';
		else if (s[h] == 'c')s[t++] = 'a', s[t++] = 'a', s[t++] = 'a';
		h += 2;
	}
	f[len] = h / 2;
}
void table()
{
	for (int len = 2; len <= 1000; ++len)bf(len);
}
LL n;
int main()
{
	//table();
	while (~scanf("%lld", &n))
	{
		LL step = 0;
		while (n > 1)
		{
			if (~n & 1)
			{
				step += n;
				n = n / 2;
			}
			else
			{
				step += n / 2 + 1;
				step += n / 2 + 1;
				n = n / 2 * 3 + 2;
			}
		}
		printf("%lld\n", step);
	}
	return 0;
}
/*
【trick&&吐槽】
不会无限下去,但是会爆int。我真是个傻叉

【题意】
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

给你一个长度为n的全'a'串,问你这个串变成长度为1的步数

【类型】
公式题

【分析】
我们发现,如果n为偶数,经过n/2步会变为全部都是'bc'
然后再经过n/2步会变为n/2个'a'

如果n为奇数,经过n/2步会变为全部都是'bc',最前还有1个'a'
然后再过一步,会变成'c'+(n/2个)'bc'
即(n/2)个'cb'再加一个'c'。
这样再过n/2步,我们会变成'c'+(n/2*3个)'a'
再过一步,变成(n/2*3-1+3个=n/2*3+2)'a'
这样暴力就可以得到答案。

然而,这道题说到有可能无解要输出-1。
什么时候会无解呢?
当n为奇数时,
n = n / 2 * 3 + 2;
相当于n=(n-1)/2*3+2=n-1+(n-1)/2+2=n+1+(n-1)/2

【时间复杂度&&优化】
log(n) or more

*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值