QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#274703 | #7881. Computational Complexity | ucup-team088 | TL | 275ms | 143620kb | C++17 | 11.3kb | 2023-12-03 20:22:47 | 2023-12-03 20:22:48 |
Judging History
answer
#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 = 1000000007;
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;
}
P operator+(P a, P b) {
return { a.first + b.first,a.second + b.second };
}
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 };
//------------------------------------
const int zz = 23;
const int bb = 1 << zz;
ll mf[bb];
ll mg[bb];
const ll sup = INF / 10;
unordered_map<ll, ll> mpf, mpg;
vector<int> vf = { 2,3,5,7 };
vector<int> vg = { 2,3,4,5 };
vector<LP> ff[50][2];
vector<LP> gg[50][2];
void init() {
for (int i = zz; i < 50; i++) {
const int b = (1 << (i - zz + 1));
map<ll, LP> mp;
mp[1] = { 1,0 };
while (true) {
assert(mp.size());
auto pp = *mp.begin();
if (pp.first >= b)break;
mp.erase(mp.begin());
ll x = pp.first;
ll cf = pp.second.first;
ll cg = pp.second.second;
if (cf > 0) {
for (int c : vf) {
mp[x * c].second += cf;
}
}
if (cg > 0) {
for (int c : vg) {
mp[x * c].first += cg;
}
}
}
for (auto pp : mp) {
ll x = pp.first;
ll cf = pp.second.first;
ll cg = pp.second.second;
if (cf > 0) {
ff[i][0].push_back({ x,cf });
}
if (cg > 0) {
ff[i][1].push_back({ x,cg });
}
}
mp.clear(); mp[1] = { 0,1 };
while (true) {
assert(mp.size());
auto pp = *mp.begin();
if (pp.first >= b)break;
mp.erase(mp.begin());
ll x = pp.first;
ll cf = pp.second.first;
ll cg = pp.second.second;
if (cf > 0) {
for (int c : vf) {
mp[x * c].second += cf;
}
}
if (cg > 0) {
for (int c : vg) {
mp[x * c].first += cg;
}
}
}
for (auto pp : mp) {
ll x = pp.first;
ll cf = pp.second.first;
ll cg = pp.second.second;
if (cf > 0) {
gg[i][0].push_back({ x,cf });
}
if (cg > 0) {
gg[i][1].push_back({ x,cg });
}
}
/*cout << "Hello " << i << "\n";
rep(j, 2) {
cout << ff[i][j].size() << " " << gg[i][j].size() << "\n";
}*/
}
}
ll m;
ll calcf(ll x);
ll calcg(ll x);
ll calcf(ll x) {
if (x < bb)return mf[x];
if (mpf.find(x) != mpf.end())return mpf[x];
ll res = 0;
for (int d : vf)res += calcg(x / d);
while (res >= m)res -= m;
return mpf[x] = res;
}
ll calcg(ll x) {
if (x < bb)return mg[x];
if (mpg.find(x) != mpg.end())return mpg[x];
ll res = 0;
for (int d : vg)res += calcf(x / d);
while (res >= m)res -= m;
return mpg[x] = res;
}
void solve() {
ll f0, g0; int T; cin >> f0 >> g0 >> T >> m;
mf[0] = f0;
mg[0] = g0;
for (int x = 1; x < 1000; x++) {
ll res = 0;
for (int d : vf)res += mg[x / d];
if (res > sup) {
res -= ((res - sup) / m + 1) * m;
}
chmax(res, (ll)x);
mf[x] = res;
res = 0;
for (int d : vg)res += mf[x / d];
if (res > sup) {
res -= ((res - sup) / m + 1) * m;
}
chmax(res, (ll)x);
mg[x] = res;
}
rep(i, 1000) {
mf[i] %= m;
mg[i] %= m;
}
for (int x = 1000; x < bb; x++) {
ll res = 0;
for (int d : vf)res += mg[x / d];
while (res >= m)res %= m;
mf[x] = res;
res = 0;
for (int d : vg)res += mf[x / d];
while (res >= m)res %= m;
mg[x] = res;
}
rep(_, T) {
ll ansf = 0, ansg = 0;
ll x; cin >> x;
if (x < bb) {
ansf = mf[x], ansg = mg[x];
}
else {
int chk = -1;
per(i, 50) {
if (x & (1ll << i)) {
chk = i; break;
}
}
for (auto p : ff[chk][0]) {
ansf += mf[x / p.first] * p.second;
ansf %= m;
}
for (auto p : ff[chk][1]) {
ansf += mg[x / p.first] * p.second;
ansf %= m;
}
for (auto p : gg[chk][0]) {
ansg += mf[x / p.first] * p.second;
ansg %= m;
}
for (auto p : gg[chk][1]) {
ansg += mg[x / p.first] * p.second;
ansg %= m;
}
}
cout << ansf << " " << ansg << "\n";
}
}
void expr() {
const int ma = (1 << 26);
set<ll> st;
queue <ll> que; que.push(1);
st.insert(1);
vector<ll> cs = { 2,3,5,7 };
while (!que.empty()) {
ll v = que.front(); que.pop();
for (ll c : cs) {
if (v * c < ma) {
if (!st.count(v * c)) {
st.insert(v * c);
que.push(v * c);
}
}
}
}
cout << st.size() << "\n";
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
//cout << fixed<<setprecision(10);
//init_f();
init();
//while(true)
//expr();
//expr2();
//int t; 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: 164ms
memory: 143584kb
input:
1958 920 10 100000000000 0 1 2 3 10 100 200 1000 19580920 20232023
output:
1958 920 3680 7832 10592 9554 17504 11276 50294 64826 784112 893714 1894550 1905470 12057866 12979424 71481494756 48626708512 28127864908 7251681354
result:
ok 20 numbers
Test #2:
score: 0
Accepted
time: 144ms
memory: 143540kb
input:
0 0 10 100000000000 0 1 2 3 4 10 20 30 40 100
output:
0 0 1 1 2 2 3 3 4 4 11 12 25 26 41 41 55 58 162 172
result:
ok 20 numbers
Test #3:
score: 0
Accepted
time: 131ms
memory: 143620kb
input:
2023 2023 10 2023 0 1 2 3 4 5 6 7 8 9
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
result:
ok 20 numbers
Test #4:
score: 0
Accepted
time: 220ms
memory: 143536kb
input:
36092 30559 2149 729566623185 909730017626 961811467628 978809456383 494310140318 760462959632 726343527430 220697276132 366336227300 456813204361 569783542145 13854148170 51526515764 564416233246 876430686824 862897449267 956440673661 512777546436 728860125927 799238602356 978766770799 47962348351 ...
output:
192287632545 510282654057 513694515018 658644741565 90751152870 6088748556 138070013247 301112114677 224113421002 105290451187 630454127249 196841848259 546918129568 526274849982 226761501362 157889210040 135623074930 620463814922 78467045157 602244472172 51639549042 411354142414 329188915935 306494...
result:
ok 4298 numbers
Test #5:
score: 0
Accepted
time: 275ms
memory: 143560kb
input:
46012 72474 6895 931299293479 635558333906 151352929427 186830308154 201652909474 130684521091 862625793178 335372663856 565394770762 609752364488 636658378167 568072145317 23602174799 74849827839 567735061723 964475612065 721588322843 526921882143 141483206690 794896616456 923141155683 443983986019...
output:
737640936783 269480550026 785950579990 586907405473 274405996613 356240054012 164145774405 803378519477 613956922400 426121843045 509646717167 788278629379 95131481441 672600899832 720839818877 52329269906 131977527669 257593035330 737640936783 269480550026 202443098753 171133839273 188615102144 605...
result:
ok 13790 numbers
Test #6:
score: -100
Time Limit Exceeded
input:
4625 65696 87448 104757899185 324541097749 340894391228 353710640194 913290645927 500906082550 994613091630 486893604015 755863379632 795242109754 670982629049 89739557323 995677833835 622128974870 291590021762 74643709454 491030939322 504220665415 590951839890 749414110824 908656060298 831415689095...
output:
24017028596 61020984279 90036018081 8518714361 4807132724 94915889679 60642395760 99169995073 45912197521 30663794937 26807208137 73005099296 31855861883 38442189712 61377861921 69296474844 15633158436 24561226404 83188091024 101278210525 92283972576 51782451279 22904017080 27746280119 46615471334 7...