QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#705677 | #8340. 3 Sum | nhuang685 | RE | 0ms | 0kb | C++23 | 9.1kb | 2024-11-03 00:34:21 | 2024-11-03 00:34:21 |
answer
/**
* @author n685
* @brief
* @date 2024-11-02 10:54:24
*
*
*/
#include "bits/stdc++.h"
#ifdef LOCAL
#include "dd/debug.h"
#else
#define dbg(...) 42
#define dbg_proj(...) 420
#define dbg_rproj(...) 420420
void nline() {}
void bar() {}
void start_clock() {}
void end_clock() {}
#endif
namespace rs = std::ranges;
namespace rv = std::views;
inline namespace bigint {
// based on https://cp-algorithms.com/algebra/big-integer.html
struct BigInt {
static constexpr int LG = 1;
static constexpr int BASE = []() -> int {
int ans = 1;
for (int i = 0; i < LG; ++i) {
ans *= 10;
}
return ans;
}();
std::vector<int> a;
BigInt() = default;
explicit BigInt(std::integral auto val) {
while (val > 0) {
a.push_back(static_cast<int>(val % BASE));
val /= BASE;
}
}
explicit BigInt(const std::string& s) {
for (int i = static_cast<int>(s.size()) - 1; i >= 0; i -= LG) {
if (i < LG) {
a.push_back(std::stoi(s.substr(0, i + 1)));
} else {
a.push_back(std::stoi(s.substr(i - LG + 1, LG)));
}
}
trim();
}
explicit BigInt(const std::vector<int>& v) : a(v) {}
friend std::istream& operator>>(std::istream& in, BigInt& val) {
std::string s;
in >> s;
val = BigInt{s};
return in;
}
friend std::ostream& operator<<(std::ostream& out, const BigInt& val) {
for (int i : val.a | rv::reverse) {
out << i;
}
return out;
}
void trim() {
while (!a.empty() && a.back() == 0) {
a.pop_back();
}
}
BigInt& operator+=(const BigInt& rhs) {
int carry = 0;
for (int i = 0; i < std::max(std::ssize(a), std::ssize(rhs.a)); ++i) {
if (i == std::ssize(a)) {
a.push_back(0);
}
a[i] += carry + (i < std::ssize(rhs.a) ? rhs.a[i] : 0);
carry = (a[i] >= BASE) ? 1 : 0;
if (a[i] >= BASE) {
a[i] -= BASE;
}
}
return *this;
}
BigInt& operator-=(const BigInt& rhs) {
int carry = 0;
for (int i = 0; i < std::ssize(rhs.a) || carry > 0; ++i) {
a[i] -= carry + (i < std::ssize(rhs.a) ? rhs.a[i] : 0);
carry = (a[i] < 0) ? 1 : 0;
if (a[i] < 0) {
a[i] += BASE;
}
}
trim();
return *this;
}
BigInt& operator*=(int rhs) {
assert(rhs < BASE);
int carry = 0;
for (int i = 0; i < std::ssize(a) || carry > 0; ++i) {
if (i == std::ssize(a)) {
a.push_back(0);
}
int64_t cur = carry + int64_t{a[i]} * rhs;
a[i] = static_cast<int>(cur % BASE);
carry = static_cast<int>(cur / BASE);
}
return *this;
}
friend BigInt operator*(const BigInt& lhs, const BigInt& rhs) {
BigInt ans;
ans.a.resize(lhs.a.size() + rhs.a.size());
for (int i = 0; i < std::ssize(lhs.a); ++i) {
for (int j = 0, carry = 0; j < std::ssize(rhs.a) || carry > 0; ++j) {
int64_t cur
= ans.a[i + j] + int64_t{lhs.a[i]} * (j < std::ssize(rhs.a) ? rhs.a[j] : 0) + carry;
ans.a[i + j] = static_cast<int>(cur % BASE);
carry = static_cast<int>(cur / BASE);
}
}
ans.trim();
return ans;
}
BigInt& operator*=(const BigInt& rhs) { return *this = *this * rhs; }
int div_mod(int rhs) {
int carry = 0;
for (int i = static_cast<int>(a.size()) - 1; i >= 0; --i) {
int64_t cur = a[i] + int64_t{carry} * BASE;
a[i] = static_cast<int>(cur / rhs);
carry = static_cast<int>(cur % rhs);
}
trim();
return carry;
}
BigInt& operator/=(int rhs) {
div_mod(rhs);
return *this;
}
int operator%(int rhs) {
int carry = 0;
for (int i = static_cast<int>(a.size()) - 1; i >= 0; --i) {
carry = static_cast<int>((a[i] + int64_t{carry} * BASE) % rhs);
}
return carry;
}
bool operator==(const BigInt& rhs) const = default;
bool operator!=(const BigInt& rhs) const = default;
auto operator<=>(const BigInt& rhs) const {
if (a.size() != rhs.a.size()) {
return a.size() <=> rhs.a.size();
}
for (int i = static_cast<int>(a.size()) - 1; i >= 0; --i) {
if (a[i] != rhs.a[i]) {
return a[i] <=> rhs.a[i];
}
}
return std::strong_ordering::equal;
}
};
BigInt operator+(BigInt a, const BigInt& b) { return a += b; }
BigInt operator-(BigInt a, const BigInt& b) { return a -= b; }
BigInt operator*(BigInt a, int b) { return a *= b; }
BigInt operator*(int b, BigInt a) { return a *= b; }
} // namespace bigint
#if defined(LOCAL) && __cplusplus >= 202302L
template <> struct std::formatter<BigInt, char> {
std::formatter<std::string, char> underlying;
constexpr formatter() {
if constexpr (requires { underlying.set_debug_format(); }) {
underlying.set_debug_format();
}
}
template <class ParseContext> constexpr auto parse(ParseContext& ctx) {
return underlying.parse(ctx);
}
template <class FormatContext> auto format(const BigInt& val, FormatContext& ctx) const {
return underlying.format(std::stringstream{} << val, ctx);
}
};
#endif
namespace multihash {
template <class T> constexpr std::pair<T, T> ex_eucl(T a, T b) {
if (a < b) {
auto [x, y] = ex_eucl(b, a);
return {y, x};
}
if (b == 0) {
assert(a == 1);
return {1, 0};
}
auto [x, y] = ex_eucl(b, a % b);
return {y, x - (a / b) * y};
}
template <int NUM> struct MultiHash {
static constexpr int MAX = 1'000'000'007;
static inline const std::array<int, NUM> mods = []() {
std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count());
std::uniform_int_distribution<int> rnd(10000, MAX);
std::array<int, NUM> ans{};
for (int& v : ans) {
v = rnd(rng);
}
return ans;
}();
template <class T> static constexpr T normalize(std::integral auto val, T mod) {
using U = decltype(mod + val);
U uval = static_cast<U>(val);
U umd = static_cast<U>(mod);
if (uval <= -umd || umd <= uval) {
uval %= umd;
}
if (val < 0) {
uval += umd;
}
return static_cast<T>(uval);
}
std::array<int, NUM> vals{};
MultiHash() = default;
explicit MultiHash(std::integral auto val) {
for (int i = 0; i < NUM; ++i) {
vals[i] = normalize(val, mods[i]);
}
}
MultiHash& operator+=(const MultiHash& rhs) {
for (int i = 0; i < NUM; ++i) {
vals[i] += rhs.vals[i];
if (vals[i] >= mods[i]) {
vals[i] -= mods[i];
}
}
return *this;
}
friend MultiHash operator+(MultiHash lhs, const MultiHash& rhs) { return lhs += rhs; }
MultiHash& operator-=(const MultiHash& rhs) {
for (int i = 0; i < NUM; ++i) {
vals[i] -= rhs.vals[i];
if (vals[i] < 0) {
vals[i] += mods[i];
}
}
return *this;
}
MultiHash operator-() const {
MultiHash ans;
for (int i = 0; i < NUM; ++i) {
ans.vals[i] = normalize(-vals[i], mods[i]);
}
return ans;
}
friend MultiHash operator-(MultiHash lhs, const MultiHash& rhs) { return lhs -= rhs; }
MultiHash& operator*=(const MultiHash& rhs) {
for (int i = 0; i < NUM; ++i) {
vals[i] = static_cast<int>(int64_t{vals[i]} * rhs.vals[i] % mods[i]);
}
return *this;
}
friend MultiHash operator*(MultiHash lhs, const MultiHash& rhs) { return lhs *= rhs; }
MultiHash binpow(std::integral auto b) const {
MultiHash res{1}, a = *this;
while (b > 0) {
if (b % 2 == 1) {
res *= a;
}
a *= a;
b /= 2;
}
return res;
}
MultiHash inv() const {
MultiHash ans;
for (int i = 0; i < NUM; ++i) {
ans.vals[i] = normalize(ex_eucl<int64_t>(vals[i], mods[i]).first);
}
return ans;
}
bool operator==(const MultiHash& rhs) const = default;
auto operator<=>(const MultiHash& rhs) const = default;
};
} // namespace multihash
using Hash = multihash::MultiHash<3>;
constexpr int MX = 10;
const std::array<Hash, MX + 1> digits = []() {
std::array<Hash, MX + 1> ans{};
for (int i = 0; i <= 10; ++i) {
ans[i] = Hash{i};
}
return ans;
}();
int main() {
#ifndef LOCAL
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
#endif
int n, k;
std::cin >> n >> k;
BigInt m{std::vector(k, 9)};
Hash hm;
for (int j : m.a | rv::reverse) {
hm = digits[10] * hm + Hash{j};
}
std::vector<Hash> a(n);
for (int i = 0; i < n; ++i) {
BigInt v;
std::cin >> v;
while (v > m) {
BigInt nv;
for (int j = 0; j < std::ssize(v.a); j += k) {
nv += BigInt{
std::vector(v.a.begin() + j, v.a.begin() + std::min(j + k, static_cast<int>(v.a.size())))
};
}
v = nv;
}
if (v == m) {
v = BigInt{0};
}
for (int j : v.a | rv::reverse) {
a[i] = digits[10] * a[i] + Hash{j};
}
}
std::map<Hash, int> freq;
int ans = 0;
for (int i = 0; i < n; ++i) {
++freq[-a[i]];
++freq[hm - a[i]];
++freq[digits[2] * hm - a[i]];
for (int j = i; j < n; ++j) {
ans += freq[a[i] + a[j]];
}
}
std::cout << ans << '\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
4 1 0 1 10 17