TC Member SRM 478 DIV 1(CarrotJumping-操作观察)

Problem Statement

  Rabbits often feel hungry, so when they go out to eat carrots, they jump as quickly as possible.

Initially, rabbit Hanako stands at position init. From position x, she can jump to either position 4*x+3 or 8*x+7 in a single jump. She can jump at most 100,000 times because she gets tired by jumping.

Carrots are planted at position x if and only if x is divisible by 1,000,000,007 (i.e. carrots are planted at position 0, position 1,000,000,007, position 2,000,000,014, and so on). Return the minimal number of jumps required to reach a carrot. If it‘s impossible
to reach a carrot using at most 100,000 jumps, return -1.

Definition

 
Class: CarrotJumping
Method: theJump
Parameters: int
Returns: int
Method signature: int theJump(int init)
(be sure your method is public)

Limits

 
Time limit (s): 2.000
Memory limit (MB): 64

Constraints

- init will be between 1 and 1,000,000,006, inclusive.

Examples

0)  
 
125000000
Returns: 1
She can jump from 125000000 to 1000000007.
1)  
 
281250001
Returns: 2
281250001 -> 1125000007 -> 9000000063
2)  
 
18426114
Returns: 58
3)  
 
4530664
Returns: 478
4)  
 
705616876
Returns: 100000
 
5)  
 
852808441
Returns: -1
She can‘t reach any carrot by making at most 100,000 jumps.

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

可以发现本题的2个操作(设为A,B)满足

1.AB=BA

2.3A=2B

故可得到如下转移图

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (1000000007)
#define MAXN (100000)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
class CarrotJumping
{
public:
	ll moveA(int a){return add(mul(4,a),3);	}
	ll moveB(int a){return add(mul(8,a),7);	}

	int theJump(int init)
	{
		int &x=init;
		if (!x) return 0;
		if (!moveB(x)) return 1;
		For(i,MAXN*3)
		{
			x=moveA(x);
			if ((!x)&&(i/3*2+i%3<=MAXN)) return i/3*2+i%3;
			if ((!moveB(x))&&(i/3*2+i%3+1<=MAXN)) return i/3*2+i%3+1;
		}
		return -1;
	}
}c;
int main()
{
	freopen("TC-677 SRM 478 DIV1-250CarrotJumping.in","r",stdin);

	int a;
	while(cin>>a)
	{
		cout<<c.theJump(a)<<endl;
	}

	return 0;
}

TC Member SRM 478 DIV 1(CarrotJumping-操作观察),布布扣,bubuko.com

时间: 2024-10-01 07:12:56

TC Member SRM 478 DIV 1(CarrotJumping-操作观察)的相关文章

TopCoder SRM 560 Div 1 - Problem 1000 BoundedOptimization &amp; Codeforces 839 E

传送门:https://284914869.github.io/AEoj/560.html 题目简述: 定义"项"为两个不同变量相乘. 求一个由多个不同"项"相加,含有n个不同变量的式子的最大值. 另外限制了每一个变量的最大最小值R[i]和L[i]和所有变量之和的最大值Max. n<=13 题外话: 刚开始做这道题的时候,感觉意外眼熟? codeforces 839 E(此题的退化版):http://codeforces.com/contest/839/pro

TopCoder SRM 634 Div.2[ABC]

TopCoder SRM 634 Div.2[ABC] ACM 题目地址: TopCoder SRM 634 赛后做的,感觉现场肯定做不出来Orz,简直不能多说. Level One-MountainRanges[水题] 题意: 问序列中有几个完全大于旁边的峰. 分析: 傻逼题,不多说. 代码: /* * Author: illuz <iilluzen[at]gmail.com> * File: one.cpp * Create Date: 2014-09-26 21:01:23 * Desc

TopCoder SRM 628 DIV 2

250-point problem Problem Statement    Janusz is learning how to play chess. He is using the standard chessboard with 8 rows and 8 columns. Both the rows and the columns are numbered 0 through 7. Thus, we can describe each cell using its two coordina

竞赛图的得分序列 (SRM 717 div 1 250)

SRM 717 DIV 1 中 出了这样一道题: 竞赛图就是把一个无向完全图的边定向后得到的有向图,得分序列就是每个点的出度构成的序列. 给出一个合法的竞赛图出度序列, 要求构造出原图(原题是求(u, v)有路径的点对数,似乎有不需要构造出原图的方法). 当时我的做法是 直接构造一个网络,跑最大流. 比赛后总觉得这个题有什么神奇的性质,于是搜了一下相关资料: 有一篇关于得分序列的论文:http://www.sciencedirect.com/science/article/pii/0095895

Topcoder口胡记 SRM 562 Div 1 ~ SRM 592 Div 1

传送门:https://284914869.github.io/AEoj/index.html Topcoder SRM 562 Div 1 - Problem 1000 InducedSubgraphs 当K*2<=N的时候,显而易见的是编号为i(K<=i<=N-K+1)的点一定会形成一条链. 枚举合法的这样的链,剩下的暴力dp吧. 当K*2>N的时候,显而易见的是编号为i(N-K+1<=i<=K)的点一定会形成一个联通快. 如果把这个联通块去掉,树会形成若干个不相交

TC Srm 597 Div 1 T3

题意: 给出M,R,G,B,2M=R+B+G,M代表一个2*M区域的列数,RGB分别代码红绿蓝格子的个数,要求:每个2*2格子中,三种颜色每种至少有一个格子,且相邻格子颜色不同,问有多少种排列方式 首先我们发现可以把一列两格的放置方案分为两组,{RB,BG,GR},{BR,GB,RG} 一个合法方案必定只由其中的一组组成,因此我们可以计算一组的方案,然后将其×2即可 那么我们假设RB有x个,BG有y个,GR有z个,那么ans等价于:求使得BR,RG,GB分别不能与自己相邻的放置方案数. 我们假设

Topcoder SRM 648 (div.2)

第一次做TC全部通过,截图纪念一下. 终于蓝了一次,也是TC上第一次变成蓝名,下次就要做Div.1了,希望div1不要挂零..._(:зゝ∠)_ A. KitayutaMart2 万年不变的水题. #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> #include<set> #include<map&

[topcoder]SRM 633 DIV 2

第一题,http://community.topcoder.com/stat?c=problem_statement&pm=13462&rd=16076 模拟就可以了. #include <vector> #include <algorithm> using namespace std; class Target { public: vector <string> draw(int n) { vector<string> result(n,

[topcoder]SRM 646 DIV 2

第一题:K等于1或者2,非常简单.略.K更多的情况,http://www.cnblogs.com/lautsie/p/4242975.html,值得思考. 第二题:http://www.cnblogs.com/lautsie/p/4245242.html BFS和DFS都可以,注意的是,写的时候,可以往que里几个东西一起扔,就不用建立对象了.也可以直接用二维矩阵记录blocked和visited. 剪枝什么的,最基本的是发现其实在步数限制的情况下,棋盘就是有界的了. 第三题:http://ap