Frog and Portal

本文探讨了一只小青蛙如何通过跳跃和使用传送门从河的一边到达另一边的问题,这个问题与斐波那契数列紧密相关。文章详细解释了如何利用斐波那契数列来计算青蛙到达对岸的不同路径数量,并提出了一种策略,通过设置传送门使得青蛙到达目的地的路径数量等于预设值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
这样从前往后设置传送门不会影响传递的结果,我们只要将 mmm 逐渐减为0即可,在减的过程中我们发现一个规律,要么 mmm 被直接减至0,要么就以 111 为间隔跳着减为0。我们不妨用 numnumnum 记录传送门的数量,最后在结束的地方 numnumnum 处安置一个死循环门即可,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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旺 崽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值