CSES - BAPC 2015 - Results
Submission details
Task:Wipe Your Whiteboards
Sender:KnowYourArchitecture
Submission time:2017-10-17 18:25:28 +0300
Language:C++
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.03 sdetails
#2ACCEPTED0.05 sdetails
#3ACCEPTED0.03 sdetails
#4ACCEPTED0.04 sdetails

Code

#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
typedef long long ll;
ll ee(ll a,ll b, ll ca, ll cb, ll xa, ll xb, ll&x,ll&y){
	if(cb==0){
		x=xa;
		if(b==0)y=0;
		else y=(ca-a*xa)/b;
		return ca;
	}
	else return ee(a,b,cb,ca%cb,xb,xa-(ca/cb)*xb,x,y);
}

pair<ll,pair<ll,ll>> solve(ll a,ll b,ll c){
	if(c==0)return {1,{0,0}};
	if(a==0&&b==0)return {0,{0,0}};
	ll x,y;
	ll g=ee(a,b,a,b,1,0,x,y);
	if(abs(c)%g>0)return {0,{0,0}};
	return {g,{x*(c/g),y*(c/g)}};
}

int main(){
	int Tn;
	cin>>Tn;
	while(Tn--){
		ll a,b,c;
		cin>>a>>b>>c;
		auto v=solve(a,b,c);
		ll g = abs(v.F);
		b/=g;
		a/=g;
		ll A = v.S.F;
		ll B = v.S.S;
		ll k=-A/b;
		A+=k*b;
		B-=k*a;
		while(A<=0)A-=b,B+=a;
		if(B<=0){
			k=B/a;
			A+=k*b;
			B-=k*a;
		}
		while(B<=0)A-=b,B+=a;
		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
...