結果
| 問題 |
No.3363 Two Closest Numbers
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-11-17 21:30:23 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,698 bytes |
| コンパイル時間 | 4,049 ms |
| コンパイル使用メモリ | 256,016 KB |
| 実行使用メモリ | 7,848 KB |
| 最終ジャッジ日時 | 2025-11-17 21:30:30 |
| 合計ジャッジ時間 | 6,751 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 48 WA * 11 |
ソースコード
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using isize = size_t;
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
using f64 = long double;
using p2 = pair<i64, i64>;
using el = tuple<i64, i64, i64>;
using mint = atcoder::modint998244353;
void _main();
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(18);
_main();
}
void _main() {
i64 n;
cin >> n;
vector<i64> c(n);
for (i64 i = 0; i < n; i++) {
cin >> c[i];
}
if (n % 2 == 1) {
sort(c.begin(), c.end());
i64 a = (n + 1) / 2;
mint x = 0, y = 0;
for (i64 i = 0; i < a; i++) {
x = x * 10 + c[i];
}
for (i64 i = n - 1; i >= a; i--) {
y = y * 10 + c[i];
}
cout << (x - y).val() << "\n";
return;
}
vector<i64> cnt(10, 0);
for (i64 c : c) cnt[c] ^= 1;
vector<i64> d;
for (i64 i = 0; i < 10; i++) if (cnt[i] > 0) d.push_back(i);
i64 ans = 1e18;
for (i64 i = 0; i < d.size(); i++) {
for (i64 j = i + 1; j < d.size(); j++) {
for (i64 xx = 0; xx < 1 << d.size(); xx++) {
i64 x = xx;
i64 y = (1 << d.size()) - x - 1;
x &= (1 << d.size()) - 1 - (1 << i) - (1 << j);
y &= (1 << d.size()) - 1 - (1 << i) - (1 << j);
if (__builtin_popcountll(x) != __builtin_popcountll(y)) continue;
i64 u = d[i], v = d[j];
for (i64 k = d.size() - 1; k >= 0; k--) if (x >> k & 1) u = u * 10 + d[k];
for (i64 k = 0; k < d.size(); k++) if (y >> k & 1) v = v * 10 + d[k];
ans = min(ans, v - u);
}
}
}
cout << mint(ans).val() << "\n";
}