Frog and Portal(思维好题)
Frog and Portal
https://hihocoder.com/problemset/problem/1873
描述
A small frog wants to get to the other side of a river. The frog is initially located at one bank of the river (position 0) and wants to get to the other bank (position 200). Luckily, there are 199 leaves (from position 1 to position 199) on the river, and the frog can jump between the leaves. When at position p, the frog can jump to position p+1 or position p+2.
How many different ways can the small frog get to the bank at position 200? This is a classical problem. The solution is the 201st number of Fibonacci sequence. The Fibonacci sequence is constructed as follows: F1=F2=1;Fn=Fn-1+Fn-2.
Now you can build some portals on the leaves. For each leaf, you can choose whether to build a portal on it. And you should set a destination for each portal. When the frog gets to a leaf with a portal, it will be teleported to the corresponding destination immediately. If there is a portal at the destination, the frog will be teleported again immediately. If some portal destinations form a cycle, the frog will be permanently trapped inside. Note that You cannot build two portals on the same leaf.
Can you build the portals such that the number of different ways that the small frog gets to position 200 from position 0 is M?
输入
There are no more than 100 test cases.
Each test case consists of an integer M, indicating the number of ways that the small frog gets to position 200 from position 0. (0 ≤ M < 232)
输出
For each test case:
The first line contains a number K, indicating the number of portals.
Then K lines follow. Each line has two numbers ai and bi, indicating that you place a portal at position ai and it teleports the frog to position bi.
You should guarantee that 1 ≤ K, ai, bi ≤ 199, and ai ≠ aj if i ≠ j. If there are multiple solutions, any one of them is acceptable.
- 样例输入
-
0 1 5
- 样例输出
-
2 1 1 2 1 2 1 199 2 2 2 4 199 5 5
一句话题意:给定一个数,用x个斐波那契数列中的数去凑成这个x
用类似下面图的方法去构造
1 #include<iostream> 2 #include<cstring> 3 #include<string> 4 #include<algorithm> 5 #include<cmath> 6 #include<cstdio> 7 #include<map> 8 #include<vector> 9 #include<queue> 10 #include<set> 11 using namespace std; 12 typedef long long ll; 13 ll a[105]; 14 ll m; 15 int ans[105][2]; 16 void AC(){ 17 int co=0; 18 int k=1; 19 while(m){ 20 int p=upper_bound(a,a+50,m)-a; 21 p--; 22 ans[co][0]=k,ans[co++][1]=200-p; 23 m-=a[p]; 24 k+=2; 25 } 26 cout<<co+1<<endl; 27 for(int i=0;i<co;i++){ 28 cout<<ans[i][0]<<" "<<ans[i][1]<<endl; 29 } 30 cout<<ans[co-1][0]+1<<" "<<ans[co-1][0]+1<<endl; 31 } 32 33 int main() 34 { 35 std::ios::sync_with_stdio(false); 36 std::cin.tie(0); 37 std::cout.tie(0); 38 a[0]=1;a[1]=1;a[2]=2; 39 for(int i=3;i<=50;i++){ 40 a[i]=a[i-1]+a[i-2]; 41 } 42 while(cin>>m){ 43 bool flag=false; 44 if(m==0){ 45 cout<<2<<endl; 46 cout<<"1 1"<<endl; 47 cout<<"2 1"<<endl; 48 continue; 49 } 50 int ans1,ans2; 51 AC(); 52 } 53 return 0; 54 }
posted on 2018-11-17 16:44 Fighting_sh 阅读(388) 评论(0) 编辑 收藏 举报