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种跳发,让你添加K个传送实现这个
思路
所有的数都可以斐波那契的和表示,把N转变为多个数的和,传送到后面,转变为加法
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[100];
ll ans[100];
ll p;
void ok(ll n)
{
if(n==0) return ;
for(ll i=1;i<=50;i++)
{
if(n == a[i])
{
ans[p++] = i;
return ;
}
if(n < a[i+1])
{
ans[p++] = i;
ok(n-a[i]);
return ;
}
}
}
int main()
{
ll i,j,m,x,n;
a[1] = 1;
a[2] = 2;
for(i=3;i<=50;i++) a[i] = a[i-1] + a[i-2];
while(scanf("%lld",&n) != EOF)
{
if(n == 0)
{
printf("2\n1 1\n2 1\n");
continue;
}
p = 0;
ok(n);
ll l = 1;
printf("%lld\n",p+1);
for(i=0;i<p;i++)
{
printf("%lld %lld\n",l,199-ans[i]+1);
l+=2;
}
printf("%lld %lld\n",l-1,l-1);
}
return 0;
}