Codeforces 585C Alice, Bob, Oranges and Apples
2015-10-17 21:04
645 查看
C. 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 by3AB2A3B. 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
output
input
output
input
output
好像是按gcd那么模拟 比着写一遍也过了
但是没有想明白原理啊。。。
#include<bits/stdc++.h>
using namespace std;
long long x,y;
long long m,n;
void gcd(long long x,long long y)
{
//cout<<x<<" "<<y<<endl;
if(__gcd(x,y)!=1)
{
printf("Impossible\n");
return;
}
if(x==y)
{
if(x==1 && y==1)
return;
else
{
printf("Impossible\n");
return;
}
}
else
if(x>y)
{
long long k=x/y;
if(x%y==0)
k--;
printf("%lldA",k);
gcd(x-k*y,y);
}
else
{
long long k=y/x;
if(y%x==0)
k--;
printf("%lldB",k);
gcd(x,y-k*x);
}
}
int main()
{
cin>>x>>y;
gcd(x,y);
return 0;
}
等想明白了再来补上吧
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 by3AB2A3B. 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
好像是按gcd那么模拟 比着写一遍也过了
但是没有想明白原理啊。。。
#include<bits/stdc++.h>
using namespace std;
long long x,y;
long long m,n;
void gcd(long long x,long long y)
{
//cout<<x<<" "<<y<<endl;
if(__gcd(x,y)!=1)
{
printf("Impossible\n");
return;
}
if(x==y)
{
if(x==1 && y==1)
return;
else
{
printf("Impossible\n");
return;
}
}
else
if(x>y)
{
long long k=x/y;
if(x%y==0)
k--;
printf("%lldA",k);
gcd(x-k*y,y);
}
else
{
long long k=y/x;
if(y%x==0)
k--;
printf("%lldB",k);
gcd(x,y-k*x);
}
}
int main()
{
cin>>x>>y;
gcd(x,y);
return 0;
}
等想明白了再来补上吧
相关文章推荐
- Codeforces Round #197 (Div. 2)
- Codeforces Round #198 (Div. 1)
- Codeforces 405E Codeforces Round #238 (Div. 2)E
- Codeforces 407C Codeforces Round #239 (Div. 1)C
- CodeForces 449A - Jzzhu and Chocolate
- CodeForces 449 B. Jzzhu and Cities
- Codeforces Round #265 (Div. 2)
- Codeforces #310 div2 C. Case of Matryoshkas
- 状态压缩DP codeforces 244 Problem C. The Brand New Function 和 codeforces 165 E. Compatible Numbers
- codeforces 16 Problem E fish
- Codeforces round #247 for Div. 2
- Codeforces Round #246 (Div. 2)
- Codeforces #264(div 2)D.Gargari and Permutations
- Codeforces Round #236 (Div. 2)------A,B
- codeforces 257 div2 B
- Codeforces Gym100571A Cursed Query
- Codeforces Gym100342E Minima
- Codeforces Gym100342J Triatrip
- Codeforces Gym100286B Blind Walk (dfs)
- Codeforces Gym100342J Triatrip