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 | |||||||||||||
| |||||||||||||
Limits | |||||||||||||
| |||||||||||||
Constraints | |||||||||||||
- | init will be between 1 and 1,000,000,006, inclusive. | ||||||||||||
Examples | |||||||||||||
0) | |||||||||||||
| |||||||||||||
1) | |||||||||||||
| |||||||||||||
2) | |||||||||||||
| |||||||||||||
3) | |||||||||||||
| |||||||||||||
4) | |||||||||||||
| |||||||||||||
5) | |||||||||||||
|
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;
}