2018icpc北京Frog and Portal转换二进制

题意:

时间限制:1000ms

单点时限:1000ms

内存限制:512MB

描述

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?

输入

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)

输出

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.

样例输入

0
1
5

样例输出

2
1 1
2 1
2
1 199
2 2
2
4 199
5 5

​ 这道题意思就是一共有200层楼梯,可以一次上一层,也可以一次上两层。我们可以建造若干个传送门,可以直接从a传送到b(强制传送)。给你一个n,问到第200层有n种方案数,需要建多少个传送门,如何建?(多种建造方案任选一种即可)

思路:

​ 这道题应该深思如何建造传送门。有3种合理的建造:

  1. 由k层传送到199层,这样总方案数会加上第k层的方案数

  2. 由k层传送到k层。死循环,总方案数不会记入该层方案数

  3. 由k层传送到k+1层。会使k层的方案数累计到k+1层中。

    由这三种构造,我们可以转化为二进制来求解。

    比如当n为7时,它的二进制为111,也就是1 + 2 + 4

    那么传送门就应该建6个。

    分别是1传送到199(总方案数+1)

    (这样2,3分别有1,1种方案数)

    3传送到4

    (这样4,5分别有2,2种方案数)

    5传送到199 (总方案数+2)

    (这样6,7分别有2,2种方案数)

    7传送到8

    (这样8,9分别有4,4种方案数)

    9传送到199 (总方案数+4)

    10传送到10 (封死)

    (10以后无法到达200,方案数为0)

    这样总方案数为7,符合条件

代码:

#include <stdio.h>
#include <string.h>
int flag[205];
long long dp[205];
int main () {
    long long n;
    while(scanf("%lld", &n) == 1) {
        int ans = 0;
        memset(flag, 0, sizeof(flag));
        if (n == 0) {
            printf("2\n1 1\n2 1\n");
            continue;
        }
        if (n % 2) {
            flag[1] = 1;
            ans ++;
            n /= 2;
            int inx = 3;
            while(n) {
                flag[inx] = 2;
                ans++;
                inx += 2;
                if (n % 2) {
                    flag[inx] = 1;
                    ans++;
                    inx += 2;
                }
                n /= 2;
            }
            ans++;
            flag[inx - 1] = 3;
        } else {
            n /= 2;
            int inx = 1;
            while(n) {
                flag[inx] = 2;
                ans++;
                inx += 2;
                if (n % 2) {
                    flag[inx] = 1;
                    ans++;
                    inx += 2;
                }
                n /= 2;
            }
            ans++;
            flag[inx - 1] = 3;
        }
        printf("%d\n", ans);
        for (int i = 1; i <= 198; i++) {
            if (flag[i] == 1) {
                printf("%d 199\n", i);
            } else if (flag[i] == 2) {
                printf("%d %d\n", i, i + 1);
            } else if (flag[i] == 3){
                printf("%d %d\n", i, i);
            }
        }
    }
    return 0;
}

转载请注明出处!!!

如果有写的不对或者不全面的地方 可通过主页的联系方式进行指正,谢谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值