1. 程式人生 > >hihocoder 1873 ACM-ICPC北京賽區2018重現賽 D Frog and Portal

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