[URI ONLINE JUDGE] – 1028 – Collectable Cards

Richard and Vicent are crazy about football collectable cards. In his spare time they’re up a way to play some game involving such cards. Both also have the habit of exchanging the repeated cards with their friends and one day they thought about a different game. They called all their friends and proposed the following. With the cards in hand, each one tried to make an exchange with his closest friend following this simple rule: each one must count how many cards he owned and after he had to split these cards into stacks, all of it with the same size, as large as it was possible for both players. So then each one choose one of the friend card stacks to receive. For example, if Ricardo and Vicente would change the figures with respectively 8 and 12 figures each, both must put his cards in stacks of four cards (Ricardo would have two stacks and Vincent had three stacks), and both choose a stack from his friend to receive it.


The first line of the input contains a single integer N (1 ≤ N ≤ 3000), indicating the number of test cases. Each test case contains two integer numbers F1 (1 ≤ F1 ≤ 1000) and F2 (1 ≤ F2 ≤ 1000)  indicating, respectly, the among of collectable cards that Richard and Vicent have to change.


For each test case there will be an integer number at the output, representing the maximum size of the stack of cards which can be exchanged between two players.

Sample Input Sample Output
8 12
9 27
259 111

my solution:

#include <iostream>

using namespace std;

int gcd(int a, int b){
int x;
while (b > 0){
x = b;
b = a % b;
a = x;
return a;

int main(){
int n, a, b;

cin >> n;

while (n--){
cin >> a >> b;
cout << gcd(a, b) << endl;

return 0;


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s