QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#468690 | #8340. 3 Sum | real_sigma_team# | WA | 372ms | 45536kb | C++23 | 3.5kb | 2024-07-08 22:53:59 | 2024-07-08 22:54:00 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
vector<int> operator+(vector<int> a, vector<int> b) {
int d = 0;
int sz = max(a.size(), b.size());
a.resize(sz, 0);
b.resize(sz, 0);
for (int i = 0; i < sz; ++i) {
a[i] += b[i] + d;
if (a[i] < 10) d = 0;
else d = 1, a[i] -= 10;
}
if (d) a.push_back(d);
while (a.size() && a.back() == 0) a.pop_back();
return a;
}
uint64_t mod = (1ll << 61) - 1;
uint64_t base = 13;
uint64_t add(uint64_t a, uint64_t b) {
return a + b >= mod ? a + b - mod : a + b;
}
uint64_t sub(uint64_t a, uint64_t b) {
return a >= b ? a - b : a + mod - b;
}
uint64_t mul(uint64_t a, uint64_t b) {
return (__int128)a * b % mod;
}
bool lss(vector<int> a, vector<int> b) {
int sz = max(a.size(), b.size());
a.resize(sz, 0);
b.resize(sz, 0);
for (int i = sz - 1; i >= 0; --i) {
if (a[i] != b[i]) return a[i] < b[i];
}
return false;
}
uint64_t nhash(vector<int> a) {
uint64_t pw = 1;
uint64_t hsh = 0;
for (auto i : a) {
hsh = add(hsh, mul(i, pw));
pw = mul(pw, base);
}
return hsh;
}
int nxt() {
int x = 0;
while (true) {
auto c = getchar();
if ('0' <= c && c <= '9') {
x *= 10;
x += c - '0';
} else {
break;
}
}
return x;
}
int main() {
int n = nxt(), K = nxt();
vector<vector<int>> num(n);
for (int i = 0; i < n; ++i) {
string s;
while (true) {
auto c = getchar();
if ('0' <= c && c <= '9') s.push_back(c);
else break;
}
reverse(s.begin(), s.end());
vector<int> a(s.size());
for (int j = 0; j < s.size(); ++j) a[j] = s[j] - '0';
vector<int> b(K, 0);
for (int j = 0; j < K; ++j) {
for (int c = j; c < a.size(); c += K) {
b[j] += a[c];
}
}
int d = 0;
while (true) {
for (int j = 0; j < K; ++j) {
b[j] += d;
d = b[j] / 10;
b[j] %= 10;
}
if (d == 0) break;
}
swap(a, b);
if (*min_element(a.begin(), a.end()) == 9) fill(a.begin(), a.end(), 0);
a.resize(K, 0);
num[i] = a;
}
vector<int> MOD(K, 9);
auto hmod = nhash(MOD);
vector<int> p(n);
iota(p.begin(), p.end(), 0);
for (int i = 0; i < K; ++i) {
vector<int> cnt(10);
for (auto j : p) {
cnt[num[j][i]]++;
}
for (int j = 1; j < 10; ++j) cnt[j] += cnt[j - 1];
vector<int> q(n);
reverse(p.begin(), p.end());
for (auto j : p) {
q[--cnt[num[j][i]]] = j;
}
p = q;
}
for (int i = 1; i < n; ++i) assert(lss(num[p[i - 1]], num[p[i]]) || num[p[i]] == num[p[i - 1]]);
vector<int> ha(n);
for (int i = 0; i < n; ++i) ha[i] = nhash(num[p[i]]);
map<uint64_t, int> cnt;
int ret = 0;
for (int i = 0, k = n - 1; i < n; ++i) {
auto hha = ha[i];
if (*max_element(num[p[i]].begin(), num[p[i]].end()) != 0) hha = sub(hmod, hha);
cnt[hha]++;
while (k >= 0 && !lss(num[p[i]] + num[p[k]], MOD)) k--;
for (int j = i; j < n; ++j) {
uint64_t hh = add(ha[i], ha[j]);
if (j > k) hh = sub(hh, hmod);
ret += cnt[hh];
}
}
cout << ret;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3612kb
input:
4 1 0 1 10 17
output:
3
result:
ok 1 number(s): "3"
Test #2:
score: 0
Accepted
time: 48ms
memory: 13056kb
input:
500 859 7118711592236878297922359501613604144948355616986970837340677671376753603836852811886591300370143151943368529129749813118476151865844255212534355441611481420938483178075143062691345257288242460282715389758789648541099090735875617822348551942134616963557723055980260082230902505269975518146286...
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 372ms
memory: 45536kb
input:
500 17336 11871159223687829792235950161360414494835561698697083734067767137675360383685281188659130037014315194336852912974981311847615186584425521253435544161148142093848317807514306269134525728824246028271538975878964854109909073587561782234855194213461696355772305598026008223090250526997551814628...
output:
0
result:
ok 1 number(s): "0"
Test #4:
score: 0
Accepted
time: 40ms
memory: 3756kb
input:
500 1 751324443898124078584847834484321089092662321556147445230263526014359393841194947303407593948729802551881289193716611867931891257925091769456350249725997883453296895094445731130479434019358742162771547784250401546380268386074363779242500860317042151185119666027858022664683818314351285215150806...
output:
2327631
result:
ok 1 number(s): "2327631"
Test #5:
score: -100
Wrong Answer
time: 50ms
memory: 3756kb
input:
500 2 408542968136435277974575411503179002415404345446801430469044749372949272333801248935236224652806667129749035002588943020176263162139056819871274824302889304493205266143688886696157147111754418731401856424401766968832165255416237731963027205324149660112574729610391396555581935236134531799950318...
output:
163629
result:
wrong answer 1st numbers differ - expected: '212002', found: '163629'