当前位置 : 主页 > 编程语言 > c语言 >

2017 ACM-ICPC, Universidad Nacional de Colombia Programming Contest D

来源:互联网 收集:自由互联 发布时间:2023-09-06
Alex has two magic machines A and B. Machine A will give you 2x + 1 coins if you insert x coins in it, machine B will give you 2x + 2. Alex has no coins and wants to get exactly n coins in order to buy a new unicorn, but he can’t

Alex has two magic machines A and B. Machine A will give you 2x + 1 coins if you insert x coins in it, machine B will give you 2x + 2. Alex has no coins and wants to get exactly n coins in order to buy a new unicorn, but he can’t figure out how to do it. Your task is to find a way to use the machines to get exactly n coins.

Input
The input consists of a single line containing n (1 ≤ n ≤ 109).

Output
For each one output a string of A’s and B’s giving the order in which the machines are used.

Examples
Input
7
Output
AAA
Input
10
Output
ABB

额。。。其实很简单的一道题。。
本来打算 dfs 的。。。。
然后T在19个点。

int dfs(int x) {
    if (x > n)return 0;
    if (x == n)return 1;
    if (dfs(2 * x + 1)) {
        vis[++tot] = 1;
        return 1;
    }
    if (dfs(2 * x + 2)) {
        vis[++tot] = 2;
        return 1;
    }
    return 0;
}

其实分个奇偶就可以了。。。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<string>
#include<bitset>
#include<ctime>
#include<deque>
typedef long long ll;
using namespace std;
typedef unsigned long long int ull;
#define maxn 30005
#define ms(x) memset(x,0,sizeof(x))
#define Inf 0x7fffffff
#define inf 0x3f3f3f3f
const long long int mod = 1e9 + 7;
#define pi acos(-1.0)
#define pii pair<int,int>
#define eps 1e-7
#define pll pair<ll,ll>



ll quickpow(ll a, ll b) {
    ll ans = 1;
    a = a % mod;
    while (b > 0) {
        if (b % 2)ans = ans * a;
        b = b / 2;
        a = a * a;
    }
    return ans;
}

int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a%b);
}


int n;
int vis[maxn];
int tot;

void dfs() {
    while (n) {
        if (n % 2 == 0) {
            vis[++tot] = 2;
            n = (n - 2) / 2;
        }
        else {
            vis[++tot] = 1;
            n = (n - 1) / 2;
        }
    }
}


int main()
{
//  ios::sync_with_stdio(false);
//  cin >> n;
    scanf("%d", &n);
    dfs();
    for (int i = tot; i >= 1; i--) {
        if (vis[i] == 1)printf("A");
        else if (vis[i] == 2)printf("B");
    }
    printf("\n");
}
【文章原创作者:美国服务器 http://www.558idc.com/mg.html提供,感恩】
上一篇:2016 ACM/ICPC Asia Regional Qingdao Online B
下一篇:没有了
网友评论