Frog and Portal HihoCoder - 1873

本文探讨了一只青蛙如何通过跳跃和使用特殊传送门从河的一边到达另一边的问题,涉及到数学中的斐波那契数列,并提供了一个算法解决方案来确定达到目的地的不同路径数量。

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

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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值