QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#861785 | #9981. Collatz Conjecture | ucup-team4435# | TL | 0ms | 3584kb | C++20 | 2.7kb | 2025-01-18 19:53:49 | 2025-01-18 19:53:49 |
Judging History
answer
#pragma GCC optimize("Ofast")
#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i < (n); ++i)
#define rep1n(i, n) for (int i = 1; i <= (n); ++i)
#define repr(i, n) for (int i = (n) - 1; i >= 0; --i)
//#define pb push_back
#define eb emplace_back
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define each(x, a) for (auto &x : a)
#define ar array
#define vec vector
#define range(i, n) rep(i, n)
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vpi = vector<pair<int, int>>;
using vvi = vector<vi>;
int Bit(int mask, int b) { return (mask >> b) & 1; }
template<class T>
bool ckmin(T &a, const T &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template<class T>
bool ckmax(T &a, const T &b) {
if (b > a) {
a = b;
return true;
}
return false;
}
// [l, r)
template<typename T, typename F>
T FindFirstTrue(T l, T r, const F &predicat) {
--l;
while (r - l > 1) {
T mid = l + (r - l) / 2;
if (predicat(mid)) {
r = mid;
} else {
l = mid;
}
}
return r;
}
template<typename T, typename F>
T FindLastFalse(T l, T r, const F &predicat) {
return FindFirstTrue(l, r, predicat) - 1;
}
const int INFi = 2e9;
const ll INF = 2e18;
long long inv(long long a, long long b){
return 1<a ? b - inv(b%a,a)*b/a : 1;
}
void solve() {
ll A, B, n; cin >> A >> B >> n;
set<ll> was;
ll iB = inv(B, A) % A;
ll x = n;
while (true) {
if (x % A == 0) {
x /= A;
if (x == n) {
cout << "Yes\n";
return;
}
} else {
// x + B * k = 0
// -x * iB
ll v = (-x * iB) % A;
if (v < 0) v += A;
if (n > x && (n - x) % B == 0) {
ll to = (n - x) / B;
if (to <= v) {
cout << "Yes\n";
return;
}
}
x += B * v;
assert(x % A == 0);
}
if (was.count(x)) {
cout << "No\n";
return;
}
was.insert(x);
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(12) << fixed;
int t = 1;
cin >> t;
rep(i, t) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3584kb
input:
7 2 1 1 2 1 2 2 1 3 2 1 100 314 159 265 314 159 2653 314 159 26535
output:
Yes Yes No No Yes Yes No
result:
ok 7 tokens
Test #2:
score: -100
Time Limit Exceeded
input:
100000 123 457 1 123 457 2 123 457 3 123 457 4 123 457 5 123 457 6 123 457 7 123 457 8 123 457 9 123 457 10 123 457 11 123 457 12 123 457 13 123 457 14 123 457 15 123 457 16 123 457 17 123 457 18 123 457 19 123 457 20 123 457 21 123 457 22 123 457 23 123 457 24 123 457 25 123 457 26 123 457 27 123 4...
output:
Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes ...