1. 程式人生 > >Frog and Portal(思維好題)

Frog and Portal(思維好題)

Frog and Portal

https://hihocoder.com/problemset/problem/1873

時間限制:1000ms 單點時限:1000ms 記憶體限制:512MB

描述

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 }
View Code