QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#226138 | #7613. Inverse Problem | hos_lyric | TL | 198ms | 126060kb | C++14 | 7.3kb | 2023-10-25 16:40:12 | 2023-10-25 16:40:14 |
Judging History
answer
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")
////////////////////////////////////////////////////////////////////////////////
template <unsigned M_> struct ModInt {
static constexpr unsigned M = M_;
unsigned x;
constexpr ModInt() : x(0U) {}
constexpr ModInt(unsigned x_) : x(x_ % M) {}
constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
ModInt pow(long long e) const {
if (e < 0) return inv().pow(-e);
ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
}
ModInt inv() const {
unsigned a = M, b = x; int y = 0, z = 1;
for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
assert(a == 1U); return ModInt(y);
}
ModInt operator+() const { return *this; }
ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
explicit operator bool() const { return x; }
bool operator==(const ModInt &a) const { return (x == a.x); }
bool operator!=(const ModInt &a) const { return (x != a.x); }
friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////
constexpr unsigned MO = 1000000007;
using Mint = ModInt<MO>;
using Mint1 = ModInt<MO - 1>;
constexpr int S = ceil(sqrt(MO));
constexpr Mint G = 5;
Mint H;
pair<unsigned, int> baby[S + 1];
void init() {
H = 1;
for (int i = 0; i < S; ++i) {
baby[i] = make_pair(H.x, i);
H *= G;
}
sort(baby, baby + S);
baby[S] = make_pair(~0U, -1);
H = H.inv();
}
int modLog(Mint a) {
for (int i = 0; i < S; ++i) {
const int pos = lower_bound(baby, baby + S, make_pair(a.x, -1)) - baby;
if (baby[pos].first == a.x) {
return i * S + baby[pos].second;
}
a *= H;
}
assert(false);
}
constexpr int LIM = 210;
int lpf[LIM];
Mint1 LOG[LIM];
Mint R;
int N;
Mint1 W[LIM];
constexpr int K = 5;
bitset<MO - 1> has;
vector<pair<unsigned, __int128>> small[LIM];
#ifdef LOCAL
int counterSmall, counterLarge;
#endif
void dfsSmall(int m, int last, Mint1 w, __int128 p) {
#ifdef LOCAL
++counterSmall;
#endif
has.set(w.x);
small[N - 2 - m].emplace_back(w.x, p);
for (int a = min(m, last); a >= 1; --a) {
dfsSmall(m - a, a, w + W[a], p << a | 1);
}
}
__int128 P, Q;
bool dfsLarge(int m, int last, Mint1 w, __int128 p) {
#ifdef LOCAL
++counterLarge;
#endif
if (has[w.x]) {
auto it = lower_bound(small[m].begin(), small[m].end(), make_pair(w.x, (__int128)-1));
if (it != small[m].end() && it->first == w.x) {
P = it->second;
Q = p;
return true;
}
}
for (int a = min(m, last); a > K; --a) {
if (dfsLarge(m - a, a, w - W[a], p << a | 1)) {
return true;
}
}
return false;
}
bool solve() {
if (N == 1) {
if (R == 1) {
puts("1");
return true;
}
return false;
}
const Mint1 tar = modLog(R / Mint(N) / Mint(N - 1));
// rooted degree d: (N-2)(N-3)...
W[0] = 0;
for (int d = 0; d < N - 2; ++d) {
W[d + 1] = W[d] + LOG[N - 2 - d];
}
#ifdef LOCAL
counterSmall=counterLarge=0;
#endif
has.reset();
for (int n = 0; n <= N - 2; ++n) {
small[n].clear();
}
dfsSmall(N - 2, K, 0, 1);
for (int n = 0; n <= N - 2; ++n) {
sort(small[n].begin(), small[n].end());
}
P = Q = 0;
if (dfsLarge(N - 2, N - 2, tar, 1)) {
vector<int> ds;
auto check = [&](__int128 p) -> void {
int a = 0;
for (; p >>= 1; ) {
++a;
if (p & 1) {
ds.push_back(a);
a = 0;
}
}
};
check(P);
check(Q);
sort(ds.begin(), ds.end(), greater<int>{});
ds.resize(N - 2, 0);
// cerr<<"ds = "<<ds<<endl;
printf("%d\n", N);
int b = 1;
for (int a = 0; a < N - 1; ++a) {
const int deg = a ? ds[a - 1] : 1;
for (int j = 0; j < deg; ++j) {
printf("%d %d\n", a + 1, b + 1);
++b;
}
}
assert(b == N);
return true;
}
#ifdef LOCAL
cerr<<"N = "<<N<<": counterSmall = "<<counterSmall<<", counterLarge = "<<counterLarge<<endl;
#endif
return false;
}
int main() {
init();
for (int p = 2; p < LIM; ++p) lpf[p] = p;
for (int p = 2; p < LIM; ++p) if (lpf[p] == p) {
for (int n = p; n < LIM; n += p) chmin(lpf[n], p);
}
LOG[1] = 0;
for (int n = 2; n < LIM; ++n) {
const int p = lpf[n];
LOG[n] = (n == p) ? modLog(n) : (LOG[p] + LOG[n / p]);
}
// for(int n=1;n<LIM;++n)assert(G.pow(LOG[n].x)==n);
for (int numCases; ~scanf("%d", &numCases); ) { for (int caseId = 1; caseId <= numCases; ++caseId) {
scanf("%u", &R.x);
for (N = 1; N <= 129; ++N) {
if (solve()) {
goto found;
}
}
assert(false);
found:{}
}
#ifndef LOCAL
break;
#endif
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 198ms
memory: 126060kb
input:
4 2 360 1 509949433
output:
2 1 2 5 1 2 2 3 2 4 3 5 1 10 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10
result:
ok OK (4 test cases)
Test #2:
score: -100
Time Limit Exceeded
input:
9 185396120 468170792 837583517 696626231 338497514 762842660 800028852 928391161 733524004