码迷,mamicode.com
首页 > 其他好文 > 详细

Frog and Portal(思维好题)

时间:2018-11-17 17:05:10      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:ref   span   color   more   ima   tab   imm   png   other   

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

 

Frog and Portal(思维好题)

标签:ref   span   color   more   ima   tab   imm   png   other   

原文地址:https://www.cnblogs.com/Fighting-sh/p/9974606.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!