Algorithm
problem Link : https://onlinejudge.org/index.php?option=onlinejudge&Itemid=8&page=show_problem&problem=1821
“As a special bonus, if they order by midnight, what would they receive? Colin: They’d receive it earlier than if they’d ordered it later. Songs of the Butcher Colin and Ryan had a party. They baked C cookies and invited G guests. Each guest ate Q cookies, and R cookies were left (R < Q). Input The first line of input gives the number of cases, N. N test cases follow. Each one is a line containing C and R (at most 2000000000). Output For each test case, output one line containing ‘Case #x:’ followed by Q — the number of cookies each guest ate. If there are multiple answers, print them in increasing order, separated by spaces. Do not print trailing spaces. Print a ‘0’ in the case when R = C.
Sample Input 4 10 0 13 2 300 98 1000 997
Sample Output Case #1: 1 2 5 10 Case #2: 11 Case #3: 101 202 Case #
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <iostream>
#include <fstream>
#include <sstream>
#include <cmath>
#include <stdio.h>
#include <limits.h>
#include <string.h>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
using namespace std;
int main() {
int n;
int c,r;
int cases = 1;
scanf("%d\n",&n);
while(n--){
scanf("%d %d",&c, &r);
set < int> res;
// c-r = g*q, find all possible multiplier q
int diff = c-r;
cout << "Case #" << cases++ << ":";
if(diff == 0){
cout << " 0" << endl;
continue;
}
for(int i=1;i*i<=diff;i++)
if(diff%i == 0){
res.insert(diff/i);
res.insert(i);
}
for(auto i : res>
if(i > r) cout << " " << i;
cout << endl;
}
}
Copy The Code &
Try With Live Editor
Input
10 0
13 2
300 98
1000 997
Output
Case #2: 11
Case #3: 101 202
Case #4:
Demonstration
UVA Online Judge solution - 10880-Colin and Ryan - UVA Online Judge solution in C,C++,java