結果
問題 | No.2589 Prepare Integers |
ユーザー | heno239 |
提出日時 | 2023-12-17 04:04:18 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 10,571 bytes |
コンパイル時間 | 2,982 ms |
コンパイル使用メモリ | 184,172 KB |
実行使用メモリ | 11,980 KB |
最終ジャッジ日時 | 2024-09-27 07:57:06 |
合計ジャッジ時間 | 10,455 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 7 ms
11,848 KB |
testcase_01 | AC | 7 ms
11,720 KB |
testcase_02 | AC | 7 ms
11,720 KB |
testcase_03 | AC | 8 ms
11,716 KB |
testcase_04 | WA | - |
testcase_05 | AC | 742 ms
11,976 KB |
testcase_06 | AC | 433 ms
11,852 KB |
testcase_07 | AC | 341 ms
11,720 KB |
testcase_08 | AC | 116 ms
11,848 KB |
testcase_09 | AC | 56 ms
11,724 KB |
testcase_10 | AC | 71 ms
11,724 KB |
testcase_11 | AC | 88 ms
11,848 KB |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | AC | 331 ms
11,724 KB |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | AC | 63 ms
11,720 KB |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | AC | 89 ms
11,848 KB |
testcase_27 | AC | 51 ms
11,852 KB |
ソースコード
#pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include<iostream> #include<string> #include<cstdio> #include<vector> #include<cmath> #include<algorithm> #include<functional> #include<iomanip> #include<queue> #include<ciso646> #include<random> #include<map> #include<set> #include<bitset> #include<stack> #include<unordered_map> #include<unordered_set> #include<utility> #include<cassert> #include<complex> #include<numeric> #include<array> #include<chrono> using namespace std; //#define int long long typedef long long ll; typedef unsigned long long ul; typedef unsigned int ui; //ll mod = 1; constexpr ll mod = 998244353; //constexpr ll mod = 1000000009; const int mod17 = 1000000007; const ll INF = (ll)mod17 * mod17; typedef pair<int, int>P; #define rep(i,n) for(int i=0;i<n;i++) #define per(i,n) for(int i=n-1;i>=0;i--) #define Rep(i,sta,n) for(int i=sta;i<n;i++) #define rep1(i,n) for(int i=1;i<=n;i++) #define per1(i,n) for(int i=n;i>=1;i--) #define Rep1(i,sta,n) for(int i=sta;i<=n;i++) #define all(v) (v).begin(),(v).end() typedef pair<ll, ll> LP; using ld = double; typedef pair<ld, ld> LDP; const ld eps = 1e-10; const ld pi = acosl(-1.0); template<typename T> void chmin(T& a, T b) { a = min(a, b); } template<typename T> void chmax(T& a, T b) { a = max(a, b); } template<typename T> vector<T> vmerge(vector<T>& a, vector<T>& b) { vector<T> res; int ida = 0, idb = 0; while (ida < a.size() || idb < b.size()) { if (idb == b.size()) { res.push_back(a[ida]); ida++; } else if (ida == a.size()) { res.push_back(b[idb]); idb++; } else { if (a[ida] < b[idb]) { res.push_back(a[ida]); ida++; } else { res.push_back(b[idb]); idb++; } } } return res; } template<typename T> void cinarray(vector<T>& v) { rep(i, v.size())cin >> v[i]; } template<typename T> void coutarray(vector<T>& v) { rep(i, v.size()) { if (i > 0)cout << " "; cout << v[i]; } cout << "\n"; } ll mod_pow(ll x, ll n, ll m = mod) { if (n < 0) { ll res = mod_pow(x, -n, m); return mod_pow(res, m - 2, m); } if (abs(x) >= m)x %= m; if (x < 0)x += m; //if (x == 0)return 0; ll res = 1; while (n) { if (n & 1)res = res * x % m; x = x * x % m; n >>= 1; } return res; } //mod should be <2^31 struct modint { int n; modint() :n(0) { ; } modint(ll m) { if (m < 0 || mod <= m) { m %= mod; if (m < 0)m += mod; } n = m; } operator int() { return n; } }; bool operator==(modint a, modint b) { return a.n == b.n; } bool operator<(modint a, modint b) { return a.n < b.n; } modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= (int)mod; return a; } modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += (int)mod; return a; } modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; } modint operator+(modint a, modint b) { return a += b; } modint operator-(modint a, modint b) { return a -= b; } modint operator*(modint a, modint b) { return a *= b; } modint operator^(modint a, ll n) { if (n == 0)return modint(1); modint res = (a * a) ^ (n / 2); if (n % 2)res = res * a; return res; } ll inv(ll a, ll p) { return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p); } modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); } modint operator/=(modint& a, modint b) { a = a / b; return a; } const int max_n = 1 << 20; modint fact[max_n], factinv[max_n]; void init_f() { fact[0] = modint(1); for (int i = 0; i < max_n - 1; i++) { fact[i + 1] = fact[i] * modint(i + 1); } factinv[max_n - 1] = modint(1) / fact[max_n - 1]; for (int i = max_n - 2; i >= 0; i--) { factinv[i] = factinv[i + 1] * modint(i + 1); } } modint comb(int a, int b) { if (a < 0 || b < 0 || a < b)return 0; return fact[a] * factinv[b] * factinv[a - b]; } modint combP(int a, int b) { if (a < 0 || b < 0 || a < b)return 0; return fact[a] * factinv[a - b]; } ll gcd(ll a, ll b) { a = abs(a); b = abs(b); if (a < b)swap(a, b); while (b) { ll r = a % b; a = b; b = r; } return a; } template<typename T> void addv(vector<T>& v, int loc, T val) { if (loc >= v.size())v.resize(loc + 1, 0); v[loc] += val; } /*const int mn = 2000005; bool isp[mn]; vector<int> ps; void init() { fill(isp + 2, isp + mn, true); for (int i = 2; i < mn; i++) { if (!isp[i])continue; ps.push_back(i); for (int j = 2 * i; j < mn; j += i) { isp[j] = false; } } }*/ //[,val) template<typename T> auto prev_itr(set<T>& st, T val) { auto res = st.lower_bound(val); if (res == st.begin())return st.end(); res--; return res; } //[val,) template<typename T> auto next_itr(set<T>& st, T val) { auto res = st.lower_bound(val); return res; } using mP = pair<modint, modint>; mP operator+(mP a, mP b) { return { a.first + b.first,a.second + b.second }; } mP operator+=(mP& a, mP b) { a = a + b; return a; } mP operator-(mP a, mP b) { return { a.first - b.first,a.second - b.second }; } mP operator-=(mP& a, mP b) { a = a - b; return a; } LP operator+(LP a, LP b) { return { a.first + b.first,a.second + b.second }; } LP operator+=(LP& a, LP b) { a = a + b; return a; } LP operator-(LP a, LP b) { return { a.first - b.first,a.second - b.second }; } LP operator-=(LP& a, LP b) { a = a - b; return a; } mt19937 mt(time(0)); const string drul = "DRUL"; string senw = "SENW"; //DRUL,or SENW //int dx[4] = { 1,0,-1,0 }; //int dy[4] = { 0,1,0,-1 }; //------------------------------------ //x,yがax+by=gcd(a,b)の解になる ll extgcd(ll a, ll b, ll& x, ll& y) { ll d = a; if (b != 0) { d = extgcd(b, a % b, y, x); y -= (a / b) * x; } else { x = 1; y = 0; } return d; } //aのmod mでの逆元を求める ll mod_inverse(ll a, ll m) { ll x, y; extgcd(a, m, x, y); return (m + x % m) % m; } void solve() { int b, q; cin >> b >> q; int sz = 0; { ll t = 1; while (t < mod17) { t *= b; sz++; } } ll sup = 1; { ll t = 1; rep(i, sz) { sup += t * (b - 1); t *= b; } sup++; } vector<vector<ll>> bs; vector<vector<ll>> lateradds; auto addlater = [&](vector<ll> v) { lateradds.push_back(v); rep(j, v.size()) { if (v[j] != 0) { ll g = gcd(v[j], b); vector<ll> nv = v; rep(i, nv.size()) { nv[i] *= b / g; nv[i] %= b; } lateradds.push_back(nv); } } }; auto add_ = [&](vector<ll> v) { int id = 0; int idv = 0; vector<vector<ll>> rads; rep(i, bs.size()) { while (bs[i][id] == 0)id++; while (idv < v.size() && v[idv] == 0)idv++; if (idv == v.size())return; if (idv < id) { //modify v ll g = gcd(b, v[idv]); ll x = mod_inverse(v[idv] / g, b / g); rep(j, v.size()) { (v[j] *= x) %= b; } assert(v[idv] == g); bs.insert(bs.begin() + i, v); addlater(v); return; } else if (idv == id) { { //modify v ll g = gcd(b, v[idv]); ll x = mod_inverse(v[idv] / g, b / g); rep(j, v.size()) { (v[j] *= x) %= b; } } ll g = gcd(bs[i][id], v[idv]); assert(b % g == 0); if (g != bs[i][id]) { ll x, y; extgcd(bs[i][id], v[idv], x, y); x %= b; y %= b; //cout << "?? " << x << " " << y << "\n"; vector<ll> nv(sz); rep(j, nv.size()) { nv[j] = bs[i][j] * x + v[j] * y; nv[j] %= b; if (nv[j] < 0)nv[j] += b; } assert(nv[id] == g); swap(bs[i], nv); addlater(nv); addlater(bs[i]); rep(j, nv.size()) { nv[j] += v[j]; nv[j] %= b; } addlater(nv); } assert(bs[i][id] == g); ll coef = v[idv] / g; Rep(j, idv, sz) { v[j] -= coef * bs[i][j]; v[j] %= b; if (v[j] < 0)v[j] += b; } addlater(v); return; } } while (idv < v.size() && v[idv] == 0)idv++; if (idv != v.size()) { //modify v ll g = gcd(b, v[idv]); ll x = mod_inverse(v[idv] / g, b / g); rep(j, v.size()) { (v[j] *= x) %= b; } assert(v[idv] == g); bs.push_back(v); addlater(v); } }; auto add = [&](vector<ll> v) { addlater(v); while (lateradds.size()) { auto v = lateradds.back(); lateradds.pop_back(); add_(v); } }; //v未満の個数 auto query_ = [&](vector<ll> v)->ll { ll res = 0; vector<int> ids(bs.size()); vector<ll> gs(bs.size()); vector<ll> rgs(bs.size() + 1, 1); { int id = 0; rep(i, bs.size()) { while (bs[i][id] == 0)id++; ids[i] = id; assert(b % bs[i][id] == 0); gs[i] = b / bs[i][id]; } per(i, (int)bs.size()) { rgs[i] = rgs[i + 1] * gs[i]; } } vector<ll> adl(sz); rep(i, ids.size()) { int le = 0; if (i > 0)le = ids[i - 1] + 1; int ri = ids[i]; Rep(j, le,ri) { if (adl[j] < v[j]) { res += rgs[i]; return res; } else if (adl[j] > v[j]) { return res; } } int id = ids[i]; ll d = adl[id] / bs[i][id]; Rep(j, id, sz) { adl[j] -= d * bs[i][j]; adl[j] %= b; if (adl[j] < 0)adl[j] += b; } assert(adl[id] < bs[i][id]); if (adl[id] > v[id])return res; d = (v[id] - adl[id]) / bs[i][id]; res += rgs[i + 1] * d; if ((v[id] - adl[id]) % bs[i][id]) { res += rgs[i + 1]; return res; } Rep(j, id, sz) { adl[j] += d * bs[i][j]; adl[j] %= b; } assert(adl[id] == v[id]); rep(j, id + 1)assert(adl[j] == v[j]); } int le = 0; if (ids.size())le = ids.back(); Rep(j, le + 1, sz) { if (adl[j] < v[j]) { res += 1; return res; } else if (adl[j] > v[j]) { return res; } } return res; }; auto trans = [&](ll x) { vector<ll> res(sz); per(i, sz) { res[i] = x % b; x /= b; } return res; }; auto query = [&](ll x) { if (x == sup) { int id = 0; ll res = 1; rep(i, bs.size()) { while (bs[i][id] == 0)id++; assert(b % bs[i][id] == 0); res *= b / bs[i][id]; } return res; } ll res = query_(trans(x)); //cout <<"val "<< x << " " << res << "\n"; return res; }; //cout << "??? " << sup << "\n"; rep(i, q) { int t; ll x; cin >> t >> x; if (t == 1) { add(trans(x)); } else if (t == 2) { x--; ll le = -1, ri = sup; while (ri - le > 1) { ll mid = (le + ri) / 2; if (query(mid) > x)ri = mid; else le = mid; } if (ri == sup)le = -1; cout << le << "\n"; } else { chmin(x, sup - 1); cout << query(x+1) << "\n"; } /*cout << "hello " << i << "\n"; rep(j, bs.size())coutarray(bs[j]); cout << "fin\n";*/ /*rep(j, 10) { cout << "!? " << j<<" "<<query(j) << "\n"; }*/ } } signed main() { ios::sync_with_stdio(false); cin.tie(0); //cout << fixed<<setprecision(10); //init_f(); //init(); //while(true) //expr(); //int t; cin >> t; rep(i, t) solve(); return 0; }