QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#54268#4888. Decoding The Messageflower_and_qingyu#ML 3ms3744kbC++233.1kb2022-10-07 18:25:552022-10-07 18:25:56

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-10-07 18:25:56]
  • 评测
  • 测评结果:ML
  • 用时:3ms
  • 内存:3744kb
  • [2022-10-07 18:25:55]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
constexpr int N = 300, mod1 = 255, mod2 = 257, phi1 = 128, phi2 = 256;
int fpw(int a, int b, int mod) {
	int ans = 1;
	for(; b; b >>= 1, a = 1ll * a * a % mod) if(b & 1) ans = 1ll * ans * a % mod;
	return ans;
}
int cnt[256];
bitset<513> f[257], g[257];
int gogo() {
	int n = 0, mp = 0;
	for (int i = 0; i < 256; ++i) {
		n += cnt[i];
		if (cnt[i] > cnt[mp]) {
			mp = i;
		}
	}
	int mv = cnt[mp];
	assert(mv == *max_element(cnt, cnt + 256));
	if (n >= 512 && n - mv >= 256)
		return 0;
	for (int i = 0; i < 256; ++i)
		f[i].reset();
	f[0].set(0);
	auto gogogo = [&](int k) {
		for (int i = 0; i < 257; ++i)
			g[i] = f[(i + k) % 257] << 1;
		for (int i = 0; i < 257; ++i)
			f[i] |= g[i];
	};
	for (int i = 0; i < 256; ++i)
		if (i != mp) {
			for (int j = 0; j < cnt[i]; ++j) {
				gogogo((257 - i) % 257);
			}
		}
	int sum = 0;
	for (int i = 0; i < 256; ++i)
		sum = (sum + 1ll * cnt[i] * i) % 257;
	for (int i = 0; i < 257; ++i)
		for (int j = 0; j <= n - mv; ++j)
			if (f[i][j]) {
				int k = n / 2 - j;
				if (k >= 0 && k <= mv && (2 * (i + 1ll * k * mp)) % 257 == sum)
					return 0;
			}
	return 1;
}
int main() {
	// freopen("in.txt", "r", stdin);
	ios::sync_with_stdio(false), cin.tie(0);
	int T;
	for(cin >> T; T; T --) {
		int k, s = 0, n = 0;
		cin >> k;
		memset(cnt, 0, sizeof cnt);
		vector<int> vec;
		for(int i = 0; i < k; i ++) {
			int di, ci;
			cin >> di >> ci;
			cnt[di] = ci;
			s = (s + 1ll * di * ci) % mod1;
			n += ci;
			for(int i = 0; i < ci; i ++) vec.emplace_back(di);
		}
		int facn = 1;
		if(n < 8) {
			for(int i = 1; i <= n; i ++){
				facn = facn * i;
			}
		}
		else facn = 2 * phi1;
		int ans1 = fpw(s, facn, mod1), ans2 = 1;
		// cout << ans1 << '\n';
		if (!gogo()) {
			ans2 = 0;
		}
		else if(n < 12) {
			int m = (n + 1) / 2;
			vector<vector<vector<int>>> f(n + 1, vector<vector<int>> (m + 1, vector<int> (mod2)));
			f[0][0][0] = 1;
			// cout << "-----------\n";
			for(int i = 0; i < n; i ++) {
				for(int j = 0; j <= m; j ++) {
					for(int s = 0; s < mod2; s ++) if(f[i][j][s]) {
						if(j < m) f[i + 1][j + 1][(s + vec[i]) % mod2] += f[i][j][s];
						f[i + 1][j][(s - vec[i] + mod2) % mod2] += f[i][j][s];
					}
				}
			}
			int w = 1;
			for(int i = 1; i <= m; i ++) w = w * i;
			for(int i = 1; i <= n - m; i ++) w = w * i;
			// cout << w << '\n';
			for(int i = 0; i < mod2; i ++) if(f[n][m][i]) {
				// cout << i << ": " << w << ' ' << f[n][m][i] << '\n';
				ans2 = 1ll * ans2 * fpw(i, 1ll * f[n][m][i] * w % phi2, mod2) % mod2;
			}
		}
		else {
			ans2 = 1;
		}
		auto crt = [&] (int m1, int a1, int m2, int a2) {
			int d = a2 - a1, m = m1 * m2;
			int x, y;
			function<void(int, int, int&, int &)> exgcd =[&] (int a, int b, int &x, int &y) {
				if(!b) return x = 1, y = 0, void();
				exgcd(b, a % b, y, x), y -= a / b * x;
			};
			exgcd(m1, m2, x, y);
			x *= d;
			return ((a1 + m1 * x) % m + m) % m;
		};
		cout << crt(mod1, ans1, mod2, ans2) << '\n';
	}
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 3ms
memory: 3620kb

input:

5
1
42 1
2
0 1
1 1
1
239 2
2
1 1
2 1
3
1 1
2 2
3 2

output:

42
256
514
1284
61726

result:

ok 5 number(s): "42 256 514 1284 61726"

Test #2:

score: 0
Accepted
time: 2ms
memory: 3744kb

input:

100
1
213 79
1
54 69
1
218 55
1
248 80
1
101 8
1
153 79
1
240 45
1
112 70
1
217 5
1
208 64
1
48 30
1
0 19
1
53 40
1
63 17
1
179 65
1
221 22
1
135 84
1
138 20
1
54 29
1
114 19
1
253 94
1
240 36
1
40 94
1
244 93
1
239 24
1
133 8
1
105 91
1
45 43
1
241 74
1
206 17
1
100 73
1
133 44
1
57 70
1
56 72
1
47...

output:

21846
21846
26215
59110
32896
6426
48060
59110
43570
32896
15420
0
59110
6426
26215
17476
15420
15420
21846
21846
32896
15420
59110
21846
54741
32896
48060
48060
32896
50116
26215
32896
15420
54741
6426
17476
15420
21846
54741
39321
54741
54741
6426
54741
1
59110
59110
26215
54741
15420
15420
22101
...

result:

ok 100 numbers

Test #3:

score: -100
Memory Limit Exceeded

input:

100
1
208 74003161
1
108 37880052
1
102 289342450
1
190 16254190
1
20 507132853
1
1 842472902
1
212 226854721
1
33 797732105
1
213 114087750
1
128 914592259
1
27 779924279
1
203 425018504
1
217 458915584
1
139 710603120
1
226 84538604
1
50 602470204
1
150 228443262
1
48 593328022
1
24 35949157
1
148...

output:


result: