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.每次可以走一步或者走两步。给出起点到终点的方案数,但是你可以再两点间建立单向边(使得你必须走这条边)
,使得满足所给方案数
思路:
首先,任何数字都可以被拆成不同(且不相邻)的菲波那契数
证明:假设存在 n 满足f(1) —f(n)可以表示f(1)—f(n+1)的数。
(n=2的时候显然成立,f(1)=f(2)=1)
那么f(1)—f(n-1)可以表示f(1)—f(n)的数,那么加上f(n)这个数,就可以表示出f(n)+1—f(n+1)的数。所以f(n)加上n-1以前的菲波那契数,就可以表示出
1—f(n+1)的数。
然后,间隔的放传送门,保证起点都是1,传送到的点到终点的距离即为相应的菲波那契数。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
ll f[105];
int ans[1005];
void pre()
{
f[1] = f[2] = 1;
for(int i = 3;i <= 55;i++)
{
f[i] = f[i - 1] + f[i - 2];
}
}
int main()
{
ll n;
pre();
while(~scanf("%lld",&n))
{
int cnt = 0;
if(n == 0)
{
printf("2\n1 1\n2 1\n");
continue;
}
if(n == 1)
{
printf("2\n1 199\n2 2\n");
continue;
}
for(int i = 55;i >= 1;i--)
{
if(f[i] <= n)
{
n -= f[i];
ans[++cnt] = i;
}
}
printf("%d\n",cnt + 1);
printf("%d %d\n",2 * cnt,2 * cnt);
for(int i = 1;i <= cnt;i++)
{
printf("%d %d\n",i * 2 - 1,200 - ans[i] + 1);
}
}
return 0;
}