| Task: | Wipe Your Whiteboards |
| Sender: | Antti Röyskö |
| Submission time: | 2017-10-17 18:33:53 +0300 |
| Language: | C++ |
| Status: | READY |
| Result: | ACCEPTED |
| test | verdict | time | |
|---|---|---|---|
| #1 | ACCEPTED | 0.04 s | details |
| #2 | ACCEPTED | 0.04 s | details |
| #3 | ACCEPTED | 0.04 s | details |
| #4 | ACCEPTED | 0.04 s | details |
Code
#include <iostream>
#include <utility>
typedef long long ll;
// return gcd(a, b), and c and d for which ac + bd = gcd(a, b)
std::pair<ll, std::pair<long long, long long>> ext_euc(ll a, ll b) {
if (a < b) {
auto res = ext_euc(b, a);
return {res.first,{res.second.second, res.second.first}};
} else {
ll k = a / b;
if (a == k * b) {
return {b, {0, 1}};
} else {
auto sub = ext_euc(b, a - k * b);
return {sub.first, {sub.second.second, sub.second.first - k * sub.second.second}};
}
}
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(0);
ll t;
std::cin >> t;
for (ll j = 0; j < t; ++j) {
ll r, s, q;
std::cin >> r >> s >> q;
// find a and b such that
// ar + bs = q
// and a>=1 and b>=1 and they are minimal
auto res = ext_euc(r, -s);
ll gcd = res.first;
ll a = res.second.first;
ll b = res.second.second;
a *= q / gcd;
b *= q / gcd;
// ar - bs = q
ll bg = r / gcd;
ll ag = -s / gcd;
//std::cout << a << ' ' << b << ' ' << ag << ' ' << bg << '\n';
ll k = (ag - a) / ag;
a += ag * k;
b -= bg * k;
k = (a-1) / ag;
a -= k * ag;
b += k * bg;
if (b >= 0) {
k = 1 + (b / bg);
a += k * ag;
b -= k * bg;
}
std::cout << a << ' ' << -b << '\n';
}
}
Test details
Test 1
Verdict: ACCEPTED
| input |
|---|
| 243 2 -2 2 2 -2 4 2 -2 6 2 -3 1 ... |
| correct output |
|---|
| 2 1 3 1 4 1 2 1 4 2 ... |
| user output |
|---|
| 2 1 3 1 4 1 2 1 4 2 ... |
Test 2
Verdict: ACCEPTED
| input |
|---|
| 100 37519548 -81935448 71983128 12441731 -99044784 98614084 99608264 -5674831 20315968 7288065 -67360366 2498082 ... |
| correct output |
|---|
| 4207392 1926631 64596284 8114405 3401796 59710496 21767778 2355168 4956464 1069368 ... |
| user output |
|---|
| 4207392 1926631 64596284 8114405 3401796 59710496 21767778 2355168 4956464 1069368 ... |
Test 3
Verdict: ACCEPTED
| input |
|---|
| 50 6387854 -19137400 28428 8306928 -18469944 46440 7145568 -427420 25800 4298766 -20849718 63474 ... |
| correct output |
|---|
| 48282 16116 37565 16895 2265 37866 11427 2356 43311 40524 ... |
| user output |
|---|
| 48282 16116 37565 16895 2265 37866 11427 2356 43311 40524 ... |
Test 4
Verdict: ACCEPTED
| input |
|---|
| 6 2 -2 2 2 -2 10 3 -2 1 3 -2 4124 ... |
| correct output |
|---|
| 2 1 6 1 1 1 1376 2 1 2 ... |
| user output |
|---|
| 2 1 6 1 1 1 1376 2 1 2 ... |
