CodeForces 586E Alice, Bob, Oranges and Apples 水题

起始水果池橙子和苹果各有x和y个,A和B各有1橙子和1苹果。操作A表示A将自己拥有橙子和苹果数的橙子和水果从水果池给B,操作B类似。

问(x,y)在如何的操作序列后可以做到AB的水果数相等且水果池水果拿完了。操作序列任意。

总觉得题意写的很难懂。。


x、y与A、B之间顺序无关,下面只考虑x>y的情况。

若y=1,那么操作A x/y=x次就结束了。

否则操作A x/y次,A剩x%y个。

这样子做应该是O(logn)的?


好像printf不能取消/清除输出只能用sprintf缓存了?

不知道能不能操作stdout。

说好10^6个字符的缓存,我开10^4个也A了(难道爆数组并无问题)。

#include <cstdio>
#include <algorithm>
using namespace std;
int main() {
    long long a, b; char A = 'A', B = 'B';
    char ans[10000]; int len = 0;
    scanf("%I64d%I64d", &a, &b);
    while (a && b) {
        if (a < b) swap(a, b), swap(A, B);
        if (b == 1) {
            if (a > 1) len += sprintf(ans + len, "%I64d%c", a - 1, A);
            puts(ans);
            return 0;
        }
        len += sprintf(ans + len, "%I64d%c", a / b, A);
        a %= b;
    }
    puts("Impossible");
    return 0;
}


E. Alice, Bob, Oranges and Apples
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Alice and Bob decided to eat some fruit. In the kitchen they found a large bag of oranges and apples. Alice immediately took an orange for herself, Bob took an apple. To make the process of sharing the remaining fruit more fun, the friends decided to play a game. They put multiple cards and on each one they wrote a letter, either 'A', or the letter 'B'. Then they began to remove the cards one by one from left to right, every time they removed a card with the letter 'A', Alice gave Bob all the fruits she had at that moment and took out of the bag as many apples and as many oranges as she had before. Thus the number of oranges and apples Alice had, did not change. If the card had written letter 'B', then Bob did the same, that is, he gave Alice all the fruit that he had, and took from the bag the same set of fruit. After the last card way removed, all the fruit in the bag were over.

You know how many oranges and apples was in the bag at first. Your task is to find any sequence of cards that Alice and Bob could have played with.

Input

The first line of the input contains two integers, x, y (1 ≤ x, y ≤ 1018, xy > 1) — the number of oranges and apples that were initially in the bag.

Output

Print any sequence of cards that would meet the problem conditions as a compressed string of characters 'A' and 'B. That means that you need to replace the segments of identical consecutive characters by the number of repetitions of the characters and the actual character. For example, string AAABAABBB should be replaced by string 3A1B2A3B, but cannot be replaced by 2A1A1B2A3B or by 3AB2A3B. See the samples for clarifications of the output format. The string that you print should consist of at most 106 characters. It is guaranteed that if the answer exists, its compressed representation exists, consisting of at most 106 characters. If there are several possible answers, you are allowed to print any of them.

If the sequence of cards that meet the problem statement does not not exist, print a single word Impossible.

Sample test(s)
input
1 4
output
3B
input
2 2
output
Impossible
input
3 2
output
1A1B
Note

In the first sample, if the row contained three cards with letter 'B', then Bob should give one apple to Alice three times. So, in the end of the game Alice has one orange and three apples, and Bob has one apple, in total it is one orange and four apples.

In second sample, there is no answer since one card is not enough for game to finish, and two cards will produce at least three apples or three oranges.

In the third sample, cards contain letters 'AB', so after removing the first card Bob has one orange and one apple, and after removal of second card Alice has two oranges and one apple. So, in total it is three oranges and two apples.


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值