Frog and Portal HihoCoder - 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?

Input

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)

Output

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.

Sample Input

0
1
5

Sample Output

2
1 1
2 1
2
1 199
2 2
2
4 199
5 5

 题目大意:

让你从0号位置跳到200号位置,每次只能跳一格或是两格,给你一个数m让你通过在两点之间建立传送门使得能走到终点的所有情况数为m,还给了一些关于斐波那契的东西,还请读者仔细阅读一下题目。

解题思路:

首先,要能明白所有的正整数都可以分解成多个斐波那契数的和,那么我们想办法这m种情况分成由许多斐波那契数相加的情况。具体如何分呢?我们可以从后往前推,在位置1架设一个传送门直接传送到位置pos,然后在位置2架设一个传送门传送到位置2,那么所有的情况就都是从位置pos到终点的情况种数了(一旦到达位置2就gg了,相信聪明的读者一定明白为什么)类似于这样,我们可以在1,3,5,7这些位置架设传送门(中间要是不隔一个的话是走不过来的,每次可是只能走一步或者两步),这个操作也就对应于把m分解成若干个斐波那契数的和,最后的话只需要在最后一个传送门的下一个位置设置一个自己到自己的传送门就ok了。仔细理解一下,可能有点绕,画出示意图就好理解一点了。

上代码。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

typedef long long ll;

ll fibo[210];

int lt[300], rt[300];

int main()
{
	//freopen("in.txt", "r", stdin);
	ll m;
	fibo[1] = 1;
	fibo[2] = 2;
	for(int i = 3; i <= 210; ++ i)
	{
		fibo[i] = fibo[i - 1] + fibo[i - 2];
	}	
	
	while(cin >> m)
	{
		int cnt = 50;
		int res = 1;
		int top = 0;
		if(m == 0)   //注意还要特判一下,因为答案个数必须要大于一
		{
			cout << 2 << endl << 1 << ' ' << 1 << endl << 2 << ' ' << 1 << endl;
			continue;
		}
		while(m)
		{
			while(fibo[cnt] > m)
			{
				cnt --;
			}
		    lt[top] = res;
			rt[top++] = 200 - cnt;
			res += 2;
			m -= fibo[cnt];
		}
		lt[top] = res - 1;
		rt[top++] = res - 1;
		cout << top << endl;
		for(int i = 0; i < top; ++ i)
		{
			cout << lt[i] << ' ' << rt[i] << endl;
		}
	}

	return 0;
}

猜你喜欢

转载自blog.csdn.net/aqa2037299560/article/details/84281784