QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#246444#7613. Inverse ProblemJWRuixiAC ✓26041ms140216kbC++234.7kb2023-11-10 20:27:152023-11-10 20:28:33

Judging History

你现在查看的是最新测评结果

  • [2023-11-10 20:28:33]
  • 评测
  • 测评结果:AC
  • 用时:26041ms
  • 内存:140216kb
  • [2023-11-10 20:27:15]
  • 提交

answer

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2")
// #define ATC
#define LL long long
#define eb emplace_back
#define writesp(x) write(x), putchar(' ')
#define writeln(x) write(x), putchar('\n')
#define FIO(FILENAME) freopen(FILENAME".in", "r", stdin), freopen(FILENAME".out", "w", stdout)
using namespace std;

#ifdef ATC
#include <atcoder/all>
using namespace atcoder;
#endif

namespace IO {
	char ibuf[(1 << 20) + 1], *iS, *iT;
#if ONLINE_JUDGE
#define gh() (iS == iT ? iT = (iS = ibuf) + fread(ibuf, 1, (1 << 20) + 1, stdin), (iS == iT ? EOF : *iS++) : *iS++)
#else
#define gh() getchar()
#endif
	inline long long read() {
		char ch = gh();
		long long x = 0;
		bool t = 0;
		while (ch < '0' || ch > '9') t |= ch == '-', ch = gh();
		while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = gh();
		return t ? ~(x - 1) : x;
	}
	template<typename _Tp>
	inline void write(_Tp x) {
		static char stk[64], *top = stk;
		if (x < 0) {
			x = ~(x - 1);
			putchar('-');
		}
		do *top++ = x % 10, x /= 10;
		while (x);
		while (top != stk) putchar((*--top) | 48);
	}
}

using IO::read;
using IO::write;

using pii = pair<int, int>;
using vi = vector<int>;
#define fi first
#define se second
#define MP make_pair

constexpr int N = 1 << 7, M = 12, mod = 1e9 + 7, B = 5;
int T, fac[N], ifac[N], inv[N];
bitset<mod> s;
vector<pii> q;
vi as[M];

inline int ksm (int b, int k) {
	int r = 1;
	for (; k; k >>= 1, b = (LL)b * b % mod) if (k & 1) r = (LL)r * b % mod;
	return r;
}

inline int Binom (int i, int j) {
	if (i < j || i < 0 || j < 0) return 0;
	return (LL)fac[i] * ifac[j] % mod * ifac[i - j] % mod;
}

void init () {
	fac[0] = 1;
	for (int i = 1; i < N; i++) fac[i] = (LL)fac[i - 1] * i % mod;
	ifac[N - 1] = ksm(fac[N - 1], mod - 2);
	for (int i = N - 2; ~i; i--) ifac[i] = (LL)ifac[i + 1] * (i + 1) % mod;
    for (int i = 1; i < N; i++) inv[i] = (LL)ifac[i] * fac[i - 1] % mod;
}

int v[N], iv[N], stk[N];
vi L, R;

bool fl;
int Goal;
vi P;

inline void dfsL (int d, int l, int u, int ml) {
	if (!u) {
        if (fl) {
            if (ml == Goal) {
                for (int i = 0; i < d; i++) P.eb(stk[i]);
            }
            return;
        }
		L.eb(ml);
		return;
	}
	for (int i = 1; i <= min(l, u); i++) {
		stk[d] = i;
		dfsL(d + 1, i, u - i, (LL)ml * iv[i] % mod);
	}
}

inline void dfsR (int d, int l, int u, int ml) {
	if (!u) {
        if (fl) {
            if (ml == Goal) {
                for (int i = 0; i < d; i++) P.eb(stk[i]);
            }
            return;
        }
		R.eb(ml);
		return;
	}
	for (int i = l; i <= u; i++) {
		stk[d] = i;
		dfsR(d + 1, i, u - i, (LL)ml * v[i] % mod);
	}
}

inline void BF (int n) {
    if (q.empty()) return;
	int o = n * (n - 1);
    o = ksm(o, mod - 2);
	for (int i = 1; i < n - 1; i++) {
        v[i] = (LL)fac[n - 2] * ifac[n - 2 - i] % mod;
        iv[i] = (LL)ifac[n - 2] * fac[n - 2 - i] % mod;
    }
	for (int i = 0; i < n - 1; i++) {
        if (q.empty()) return;
		vi().swap(L), vi().swap(R);
		dfsL(0, B, i, 1);
		dfsR(0, B + 1, n - 2 - i, 1);
        // cout << n << ' ' << i << ' ' << L.size() << ' ' << R.size() << '\n';
        for (int j : R) s.set(j);
		for (int j : L) {
			for (int k = 0; k < q.size(); k++) {
				auto [goal, id] = q[k];
				goal = (LL)goal * o % mod * j % mod;
				if (!s.test(goal)) continue;
                fl = 1;
                vi().swap(P);
                Goal = j;
                dfsL(0, B, i, 1);
                as[id].insert(as[id].end(), P.begin(), P.end());
                vi().swap(P);
                Goal = goal;
                dfsR(0, B + 1, n - 2 - i, 1);
                fl = 0;
                as[id].insert(as[id].end(), P.begin(), P.end());
				as[id].resize(n);
				q.erase(q.begin() + k);
				--k;
			}
		}
        for (int j : R) s.set(j, 0);
	}
}

inline void __out (vector<int> v) {
    cout << v.size() << '\n';
    sort(v.begin(), v.end(), greater<>());
    ++v[0];
    int s = 1;
    for (int i = 0; i < v.size(); i++) {
        for (int j = s; j < s + v[i]; j++) {
            cout << (i + 1) << ' ' << (j + 1) << '\n';
        }
        s += v[i];
    }
}

int main() {
    cout.tie(0) -> sync_with_stdio(0);
	T = read();
	for (int i = 1; i <= T; i++) {
		int r = read();
        if (r == 1) {
            as[i] = {-1};
            continue;
        }
		q.eb(r, i);
	}
	init();
	for (int i = 2; i < N; i++) 
        BF(i);
    assert(q.empty());
	for (int i = 1; i <= T; i++) {
        __out(as[i]);
	}
}
// I love WHQ!

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3452kb

input:

4
2
360
1
509949433

output:

2
1 2
5
1 2
1 3
1 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: 0
Accepted
time: 19781ms
memory: 140216kb

input:

9
185396120
468170792
837583517
696626231
338497514
762842660
800028852
928391161
733524004

output:

14
1 2
1 3
1 4
2 5
2 6
3 7
4 8
5 9
6 10
7 11
8 12
9 13
10 14
122
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
1 22
1 23
1 24
1 25
1 26
1 27
1 28
1 29
1 30
1 31
2 32
2 33
2 34
2 35
2 36
2 37
2 38
2 39
2 40
2 41
2 42
2 43
2 44
2 45
3 46
3 47
3 48
3 49
3 5...

result:

ok OK (9 test cases)

Test #3:

score: 0
Accepted
time: 26041ms
memory: 140204kb

input:

10
338497514
733524004
447182954
415993605
453460670
50499055
648088551
986982752
907925397
315315230

output:

124
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
1 22
1 23
1 24
1 25
1 26
1 27
1 28
1 29
1 30
1 31
1 32
1 33
1 34
1 35
1 36
2 37
2 38
2 39
2 40
2 41
2 42
2 43
2 44
2 45
2 46
2 47
2 48
2 49
2 50
2 51
3 52
3 53
3 54
3 55
3 56
3 57
3 58
3 59
3 60
4 61
4 62...

result:

ok OK (10 test cases)

Test #4:

score: 0
Accepted
time: 5159ms
memory: 127228kb

input:

10
1
2
3
4
5
6
7
8
9
10

output:

1
2
1 2
102
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
1 22
1 23
1 24
1 25
2 26
2 27
2 28
2 29
2 30
2 31
2 32
2 33
2 34
2 35
2 36
2 37
2 38
2 39
2 40
2 41
2 42
2 43
3 44
3 45
3 46
3 47
3 48
3 49
3 50
3 51
3 52
3 53
3 54
3 55
3 56
4 57
4 58
4 59
4 60
4...

result:

ok OK (10 test cases)

Test #5:

score: 0
Accepted
time: 1672ms
memory: 126012kb

input:

10
269199917
392009324
753889928
751355133
472639410
132096559
331333826
40414701
72847302
475706026

output:

55
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
1 22
1 23
1 24
1 25
1 26
1 27
1 28
1 29
1 30
1 31
1 32
1 33
1 34
1 35
1 36
1 37
1 38
1 39
1 40
1 41
1 42
1 43
1 44
1 45
1 46
1 47
1 48
1 49
1 50
1 51
1 52
1 53
1 54
1 55
84
1 2
1 3
1 4
1 5
1 6
1 7
1 8
2 9
...

result:

ok OK (10 test cases)

Extra Test:

score: 0
Extra Test Passed