TC Member SRM 478 DIV 1(CarrotJumping-操作观察)
2014-07-02 13:00
417 查看
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) | ||||||||||||
|
可以发现本题的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 SRM 540 DIV2
- TC SRM 400 DIV2
- TC SRM 604 div2
- (水题)TC SRM 441 DIV 500 PaperAndPaintEasy
- tc-SRM-626-DIV1-250
- TC SRM 521 (div. 2) 1000'
- [TC] SRM 633 div2 250
- TC SRM 593 DIV2 1000
- TC SRM 570 div2 1000【Tree,树上统计】
- tc srm 636 div2 500
- TC SRM 549 DIV2
- TC SRM 562 DIV 2
- TC SRM 562 DIV 2
- TC SRM 593 DIV1 250(dfs)
- SRM 207 Div II Level Two: RegularSeason,字符串操作(sstream),多关键字排序(操作符重载)
- TC SRM 547 div2
- TC SRM 557 DIV2 1000
- [TC SRM 571] DIV1 1000
- TC-SRM-609-DIV2-950- VocaloidsAndSongs
- TC SRM 636 Div2 C ChocolateDividingHard 二分