zoukankan      html  css  js  c++  java
  • Frog and Portal(思维好题)

    Frog and Portal

    https://hihocoder.com/problemset/problem/1873

    时间限制: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

    一句话题意:给定一个数,用x个斐波那契数列中的数去凑成这个x

    用类似下面图的方法去构造

     1 #include<iostream>
     2 #include<cstring>
     3 #include<string>
     4 #include<algorithm>
     5 #include<cmath>
     6 #include<cstdio>
     7 #include<map>
     8 #include<vector>
     9 #include<queue>
    10 #include<set>
    11 using namespace std;
    12 typedef long long ll;
    13 ll a[105];
    14 ll m;
    15 int ans[105][2];
    16 void AC(){
    17     int co=0;
    18     int k=1;
    19     while(m){
    20         int p=upper_bound(a,a+50,m)-a;
    21         p--;
    22         ans[co][0]=k,ans[co++][1]=200-p;
    23         m-=a[p];
    24         k+=2;
    25     }
    26     cout<<co+1<<endl;
    27     for(int i=0;i<co;i++){
    28         cout<<ans[i][0]<<" "<<ans[i][1]<<endl;
    29     }
    30     cout<<ans[co-1][0]+1<<" "<<ans[co-1][0]+1<<endl;
    31 }
    32 
    33 int main()
    34 {
    35     std::ios::sync_with_stdio(false);
    36     std::cin.tie(0);
    37     std::cout.tie(0);
    38     a[0]=1;a[1]=1;a[2]=2;
    39     for(int i=3;i<=50;i++){
    40         a[i]=a[i-1]+a[i-2];
    41     }
    42     while(cin>>m){
    43         bool flag=false;
    44         if(m==0){
    45             cout<<2<<endl;
    46             cout<<"1 1"<<endl;
    47             cout<<"2 1"<<endl;
    48             continue;
    49         }
    50         int ans1,ans2;
    51         AC();
    52     }
    53     return 0;
    54 }
    View Code
  • 相关阅读:
    非原创-MongoDB PHP 扩展
    原创-docker命令
    原创-k8s nginx内核参数优化
    原创-阿里云K8S-PVCyaml文件挂载云盘
    原创-k8s反亲和性
    使用Virtualenv隔离Python、Ansible不同发行版
    基于Scrapy分布式爬虫的开发与设计
    CentOS7.3中将Python2.7.5 升级到Python3.5.1
    如何让django的model名和应用名显示为中文
    Django添加ckeditor富文本编辑器
  • 原文地址:https://www.cnblogs.com/Fighting-sh/p/9974606.html
Copyright © 2011-2022 走看看