QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#734637 | #8340. 3 Sum | Vegetable_Dog | TL | 103ms | 9860kb | C++23 | 3.3kb | 2024-11-11 13:32:26 | 2024-11-11 13:32:27 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < (b); i++)
#define Rep(i, a, b) for (int i = a; i >= (b); i--)
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fi first
#define se second
#define SZ(x) (int)(x.size())
#define lowbit(x) ((x) & -(x))
using db = double;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using i128 = __int128_t;
// Think twice, code once
using BigInt = vector<ll>;
const int N = 505, base = 131, mod = 998244353;
int n, k, h1[N], h2[N];
BigInt nums[N];
map<int, int> mp;
int get_hash(BigInt a) {
int h = 0;
reverse(all(a));
for (auto x : a) {
h = (1ll * h * base % mod + x % mod) % mod;
}
return h;
}
void get_mod(vi &a) {
while (SZ(a) > k) {
Rep(i, SZ(a) - 1, k) {
int t = a[i];
a[i - k] += t;
a.pop_back();
}
int carry = 0;
rep(i, 0, SZ(a)) {
int cur = a[i] + carry;
a[i] = cur % 10;
carry = cur / 10;
}
while (carry) {
a.pb(carry % 10);
carry /= 10;
}
}
}
void print(BigInt a) {
reverse(all(a));
for (auto x : a) cout << x;
cout << endl;
}
BigInt trans(vi a) {
while (SZ(a) % 18) a.pb(0);
BigInt ret;
while (!a.empty()) {
ll x = 0;
rep(i, 0, 18) {
x = x * 10 + a.back();
a.pop_back();
}
ret.pb(x);
}
return ret;
}
void trans(string s, int i) {
vi a;
for (auto c : s) a.pb(c - '0');
reverse(all(a));
get_mod(a);
nums[i] = trans(a);
vi b = a;
assert(SZ(a) <= k);
b.resize(k);
rep(i, 0, k) b[i] = 9 - b[i];
h1[i] = get_hash(trans(b));
int carry = 0;
rep(i, 0, k) {
int cur = b[i] + carry + 9;
b[i] = cur % 10;
carry = cur / 10;
}
while (carry) {
b.pb(carry % 10);
carry /= 10;
}
h2[i] = get_hash(trans(b));
}
const ll P = 1e18;
void solve() {
cin >> n >> k;
int len = 0;
rep(i, 0, n) {
string s; cin >> s;
trans(s, i);
mp[h1[i]]++;
mp[h2[i]]++;
len = max(len, SZ(nums[i]));
if (s == "0") mp[0]++;
// print(a[i]);
// cout << h1[i] << ' ' << h2[i] << endl;
}
rep(i, 0, n) nums[i].resize(len);
BigInt a(len);
int ans = 0;
rep(i, 0, n) {
rep(j, 0, i + 1) {
int carry = 0;
a.resize(len);
rep(k, 0, len) {
ll cur = nums[i][k] + nums[j][k] + carry;
a[k] = cur % P;
carry = cur / P;
}
while (carry) {
a.pb(carry % P);
carry /= P;
}
int h = get_hash(a);
// cout << i << ' ' << j << ' ' << h << endl;
ans += mp[h];
}
mp[h1[i]]--;
mp[h2[i]]--;
}
cout << ans << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
int T = 1;
// cin >> T;
while (T--) solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3828kb
input:
4 1 0 1 10 17
output:
3
result:
ok 1 number(s): "3"
Test #2:
score: 0
Accepted
time: 103ms
memory: 9860kb
input:
500 859 7118711592236878297922359501613604144948355616986970837340677671376753603836852811886591300370143151943368529129749813118476151865844255212534355441611481420938483178075143062691345257288242460282715389758789648541099090735875617822348551942134616963557723055980260082230902505269975518146286...
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: -100
Time Limit Exceeded
input:
500 17336 11871159223687829792235950161360414494835561698697083734067767137675360383685281188659130037014315194336852912974981311847615186584425521253435544161148142093848317807514306269134525728824246028271538975878964854109909073587561782234855194213461696355772305598026008223090250526997551814628...