当前位置: 首页 > 工具软件 > Frog CMS > 使用案例 >

Frog and Portal

桓智敏
2023-12-01

Frog and Portal

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

比较有意思的构造题,我们可以从后往前考虑
199 − > 200   1 种 199->200 1种 199>200 1
198 − > 200   2 种 198->200 2种 198>200 2
197 − > 200   3 种 197->200 3种 197>200 3
⋯ \cdots
这样从前往后设置传送门不会影响传递的结果,我们只要将 m m m 逐渐减为0即可,在减的过程中我们发现一个规律,要么 m m m 被直接减至0,要么就以 1 1 1 为间隔跳着减为0。我们不妨用 n u m num num 记录传送门的数量,最后在结束的地方 n u m num num 处安置一个死循环门即可,AC代码如下:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+5;
ll a[N],r[N];
int main(){
    ll m;
    a[200]=1,a[199]=1;
    for(int i=198;i>=150;i--)
        a[i]=a[i+1]+a[i+2];
    while(~scanf("%lld",&m)){
        ll num=0;
        if(m==0){
            printf("2\n1 1\n2 1\n");
        }
        else{
            for(int i=150;i<=200;i++){
                if(m==0) break;
                if(m-a[i]>=0){
                    r[++num]=i;
                    m-=a[i];
                }
            }
            printf("%lld\n",num+1);
            for(int i=1;i<=num;i++){
                printf("%d %lld\n",i*2-1,r[i]);
            }
            printf("%lld %lld\n",num*2,num*2);
        }
    }
    return 0;
}
 类似资料:

相关阅读

相关文章

相关问答