QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#405458 | #7613. Inverse Problem | ucup-team3215 | TL | 1ms | 3684kb | C++20 | 2.1kb | 2024-05-05 23:28:53 | 2024-05-05 23:28:54 |
Judging History
answer
#include <algorithm>
#include <cstdint>
#include <iostream>
#include <numeric>
#include <unordered_map>
#include <vector>
using namespace std;
constexpr int mod = 1e9 + 7;
auto& mul(auto&& a, auto b) { return a = a * (uint64_t) b % mod; }
int inv(int a) { for (int r = 1, b = mod - 2; ; b /= 2, mul(a, a)) if (!b) return r; else if (b % 2) mul(r, a); }
using namespace std;
vector<unordered_map<int, vector<int>>> even(1);
vector<int> inv_(1);
void gen_even(int n0, int n, int c, int p, auto&& cur) {
even[n0 - n].insert({p, cur});
for (; c > 0; c -= 2) if (c <= n) {
int np = p;
for (int i = 0; i < c; ++i) mul(np, inv_[n0 - i]);
cur.push_back(c);
gen_even(n0, n - c, c, np, cur);
cur.pop_back();
}
}
vector<int> qs;
vector<vector<int>> ans;
void solve(int n0, int n, int c, int p, auto&& cur) {
if (n % 2 == 0)
for (int i = 0; i < qs.size(); ++i) if (ans[i].empty() && even[n].find(mul(+p, qs[i])) != even[n].end()) {
ans[i] = cur;
auto& t = even[n][mul(+p, qs[i])];
ans[i].insert(ans[i].end(), t.begin(), t.end());
}
for (; c > 0; c -= 2) if (c <= n) {
int np = p;
for (int i = 0; i < c; ++i) mul(np, n0 - i);
cur.push_back(c);
solve(n0, n - c, c, np, cur);
cur.pop_back();
}
}
int main() {
int tc; cin >> tc;
qs.resize(tc);
ans.resize(tc);
for (int i = 0; i < tc; ++i) {
cin >> qs[i];
if (qs[i] <= 2) ans[i] = {-1};
qs[i] = inv(qs[i]);
}
for (int n = 1, done = 0; !done++; ++n) {
inv_.push_back(inv(n));
even.push_back({});
for (auto& x: even) x.clear(), x.max_load_factor(.2);
gen_even(n, n, n + n % 2, 1, vector<int>{});
solve(n, n, n - n % 2 + 1, mul(n + 1, n + 2), vector<int>{});
for (int i = 0; i < tc; ++i) done &= !ans[i].empty();
}
for (int i = 0; i < tc; ++i) {
if (qs[i] == 1) cout << "1\n";
else {
if (ans[i][0] == -1) ans[i] = {};
cout << accumulate(ans[i].begin(), ans[i].end(), 0) + 2 << '\n' << "1 2\n";
for (int j = 0, k = 2; j < ans[i].size(); ++j)
while (ans[i][j]--) cout << j + 1 << ' ' << ++k << '\n';
}
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3684kb
input:
4 2 360 1 509949433
output:
2 1 2 5 1 2 1 3 2 4 2 5 1 10 1 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 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