본문 바로가기
알고리즘/Codeforces

Codeforces Round #653 (Div. 3) ABC

by 유시은 2020. 8. 6.
#include <iostream>
using namespace std;
 
int T, x, y, n;
 
int main() {
	cin.tie(NULL);
	cout.tie(NULL);
	ios::sync_with_stdio(0);
 
	cin >> T;
	while (T--) {
		cin >> x >> y >> n;
		int k;
		if ((n / x) * x + y <= n) cout << (n / x) * x + y << "\n";
		else cout << (n / x - 1) * x + y << "\n";
	}
 
	return 0;
}

 

A - Required Remainder

 

#include <iostream>
using namespace std;
 
int T, N;
 
int main() {
	cin.tie(NULL);
	cout.tie(NULL);
	ios::sync_with_stdio(0);
 
	cin >> T;
	while (T--) {
		cin >> N;
		int tr = 0;
		if (N == 0) {
			cout << "0\n";
			continue;
		}
 
		unsigned long long n = N;
		while (n != 1) {
			if (n % 6 == 0) {
				tr += 1;
				n /= 6;
			}
			else if (n % 3 == 0) {
				tr += 2;
				n /= 3;
			}
			else {
				tr = -1;
				break;
			}
		}
		cout << tr << "\n";
	}
 
	return 0;
}

 

B - Multiply by 2, divide by 6

 

#include <iostream>
#include <string>
using namespace std;
 
int T, L;
 
 
int main() {
	cin.tie(NULL);
	cout.tie(NULL);
	ios::sync_with_stdio(0);
 
	cin >> T;
	while (T--) {
		cin >> L;
		string s = "";
		for (int i = 0; i < L; ++i) {
			char input;
			cin >> input;
			s += input;
		}
		int cnt = 0, res = 0;
		for (int i = 0; i < L; ++i) {
			if (s[i] == '(') cnt++;
			else {
				if (cnt == 0) res++;
				else cnt--;
			}
		}
		cout << res << "\n";
	}
 
	return 0;
}

 

C - Move Brackets

댓글