hihocoder 1873 ACM-ICPC北京赛区2018重现赛 D Frog and Portal

http://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
解析 二进制拆分
#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
using namespace std;
const int maxn=1e6+10;
typedef long long ll;
vector<pair<int,int> > ans;
int main()
{
    ll n;
    while(scanf("%lld",&n)!=EOF)
    {
        ans.clear();
        ans.pb(mp(198,198));
        int beg=2;
        if(n%2==1)
        {
            ans.pb(mp(1,199));
            n--;
        }
        else
            ans.pb(mp(1,198));
        while(n)
        {
            if(n%2==1)
            {
                if(n==1)break;
                ans.pb(mp(beg+1,199));
                ans.pb(mp(beg+5,beg+5));
                beg=beg+6;
                n--;
                n/=2;
            }
            else
            {
                ans.pb(mp(beg+3,beg+3));
                beg=beg+4;
                n/=2;
            }
        }
        if(n==0)
            ans.pb(mp(beg,198));
        else
            ans.pb(mp(beg,199));
        printf("%d\n",ans.size());
        for(int i=0;i<ans.size();i++)
            printf("%d %d\n",ans[i].first,ans[i].second);
    }
}


猜你喜欢

转载自www.cnblogs.com/stranger-/p/9979368.html