Codeforces Round #653 (Div. 3)
A.Required Remainder
题目大意
给你x,y,n,问你最大的k(1 <= k <= n)满足k mod x = y是多少
解题思路
k = x * m + y <= n
m <= (n - y) / x
那么max(k) = [(n - y) / x] * x + y
AC代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int T;
cin >> T;
while (T--) {
ll x, y, n;
cin >> x >> y >> n;
cout <<(n - y) / x * x + y << '\n';
}
return 0;
}
B.Multiply by 2, divide by 6
题目大意
给你一个n,每次你可以将他乘以2或者除以6(如果n % 6 == 0),问你n到达1的最少次数是多少,如果无法到达,输出-1
解题思路
n * 6 -x * 2y = 1
n = 3x * 2x-y
num = x + y = 2 * x - (x - y)
所以将n分解为x个3相乘和x - y个2相乘即可
无法分解或者出现x > x - y表示num = -1
AC代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int x = 0, xy = 0;
while (n % 3 == 0) {
++x;
n /= 3;
}
while (n % 2 == 0)