QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#349706 | #8340. 3 Sum | ucup-team2894# | TL | 373ms | 7636kb | C++20 | 7.5kb | 2024-03-10 04:25:52 | 2024-03-10 04:25:52 |
Judging History
answer
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define fr first
#define sc second
#define all(a) (a).begin(), (a).end()
#define unique(a) a.resize(unique(a.begin(), a.end()) - a.begin())
using namespace std;
#ifdef ONPC
mt19937 rnd(223);
#else
mt19937 rnd(chrono::high_resolution_clock::now()
.time_since_epoch().count());
#endif
#define TIME (clock() * 1.0 / CLOCKS_PER_SEC)
using ll = __int128;
#define int ll
using ld = double;
using me = long long;
const int basen = 36;
const int base = pow(10, me(basen));
// uncomment to multiply large numbers with fft
// basen should be even, at most 8
// with doubles in fft and basen = 8 works up to Big.v.size() = 1e6 (but fails for 1e6 + 5e4)
// #define BIGINT_USE_FFT
// division works in n^2 * log(base), where n = Big.v.size()
struct Big {
vector<ll> v;
bool minus = false;
Big() {}
Big(long long k) {
if (k < 0) {
minus = true;
k = -k;
}
while (k) {
v.push_back(k % base);
k /= base;
}
}
__int128 read128(string s) {
__int128 res = 0;
for(char c : s) res = res * 10 + (c-'0');
return res;
}
Big(string s) {
if (s[0] == '-') {
s.erase(s.begin());
minus = true;
}
reverse(s.begin(), s.end());
while (s.size() % basen != 0)
s.push_back('0');
reverse(s.begin(), s.end());
for (int i = 0; i < s.size(); i += basen)
v.push_back(read128(s.substr(i, basen)));
reverse(v.begin(), v.end());
norm();
}
Big &operator += (const Big &other) {
if (minus == other.minus) {
_add_(v, other.v);
} else {
// if (_comp_(other.v, v)) {
_sub_(v, other.v);
// } else {
// _sub2_(v, other.v);
// minus ^= 1;
// }
}
norm();
return *this;
}
Big operator + (const Big &other) const {
auto res = *this;
return res += other;
}
Big operator - () const {
Big res = *this;
if (!v.empty()) res.minus ^= 1;
return res;
}
Big &operator -= (const Big &other) {
return *this += -other;
}
Big operator - (const Big &other) const {
auto res = *this;
return res -= other;
}
void norm() {
while (!v.empty() && v.back() == 0)
v.pop_back();
if (v.empty())
minus = false;
}
bool operator < (const Big &other) const {
if (minus != other.minus) return minus;
if (minus) return _comp_(other.v, v);
else return _comp_(v, other.v);
}
bool operator > (const Big &other) const {
return other < *this;
}
bool operator <= (const Big &other) const {
return !(other < *this);
}
bool operator >= (const Big &other) const {
return !(*this < other);
}
bool operator == (const Big &other) const {
return minus == other.minus && v == other.v;
}
bool operator != (const Big &other) const {
return !(*this == other);
}
private:
static void _sub_(vector<int> &a, const vector<int> &b) {
a.resize(max(a.size(), b.size()) + 1, 0);
for (int i = 0; i < b.size(); ++i)
a[i] -= b[i];
for (int i = 0; i + 1 < b.size() || a[i] < 0; ++i) {
if (a[i] < 0) {
a[i] += base;
--a[i + 1];
}
}
assert(a.back() >= 0);
while (!a.empty() && a.back() == 0)
a.pop_back();
}
static void _sub2_(vector<int> &a, const vector<int> &b) {
a.resize(max(a.size(), b.size()) + 1, 0);
for (int i = 0; i < a.size(); ++i)
a[i] = (i < b.size() ? b[i] : 0) - a[i];
for (int i = 0; i + 1 < a.size(); ++i) {
if (a[i] < 0) {
a[i] += base;
--a[i + 1];
}
}
assert(a.back() >= 0);
while (!a.empty() && a.back() == 0)
a.pop_back();
}
static void _add_(vector<int> &a, const vector<int> &b) {
a.resize(max(a.size(), b.size()) + 1, 0);
for (int i = 0; i < b.size(); ++i)
a[i] += b[i];
for (int i = 0; i + 1 < b.size() || a[i] >= base; ++i) {
if (a[i] >= base) {
a[i] -= base;
++a[i + 1];
}
}
while (!a.empty() && a.back() == 0)
a.pop_back();
}
static bool _comp_(const vector<int> &a, const vector<int> &b) {
if (a.size() != b.size())
return a.size() < b.size();
for (int i = (int)a.size() - 1; i >= 0; --i)
if (a[i] != b[i])
return a[i] < b[i];
return false;
}
};
istream &operator >> (istream &i, Big &b) {
string s;
i >> s;
b = Big(s);
return i;
}
const int maxn = 1e5 + 100, inf = 1e9 + 100;
Big getmod(string s, int k, const Big&M) {
vector<Big> v;
Big sum = 0;
for(int i=0;i<s.size();i+=k){
string sub = s.substr(i,k);
reverse(all(sub));
Big num = Big(sub);
sum += num;
if(sum >= M) {
sum -= M;
}
}
return sum;
}
ll sum3(const vector<Big>& uni, const vector<int>&num, const Big& M) {
ll ans = 0;
for(int i=0;i<uni.size();i++){
Big tar = M - uni[i];
for(int j=i,k=uni.size()-1;j<=k;j++){
if(uni[j] > tar) break;
Big tar2 = tar-uni[j];
while(k>j&&uni[k]>tar2)k--;
if(uni[k]==tar2) {
// cerr << i << " " << j << " " << k << endl;
if(i==j&&j==k){
ans += ll(num[i])*(num[i]+1)*(num[i]+2)/6;
}
else if(i==j){
ans += ll(num[i])*(num[i]+1)/2*num[k];
}
else if(j==k){
ans += ll(num[i])*num[j]*(num[j]+1)/2;
}
else {
ans += ll(num[i])*num[j]*num[k];
}
}
}
}
return ans;
}
#define cin cin
Big a[501];
void solve() {
me n,k;
n = 500;
k = 20000;
cin >> n >> k;
string ms(k,'9');
const Big M = Big(ms);
for(int i=0;i<n;i++){
string s;
s = string(20000,'0');
for(int i=0;i<s.size();i++)s[i] = '0' + rnd()%10;
cin >> s;
// cerr << s << endl;
reverse(all(s));
a[i] = getmod(s,k,M);
// cerr << a[i] << endl;
}
sort(a,a+n);
vector<Big> uni;
vector<int> num;
for(int i=0;i<n;i++){
if(uni.size() && uni.back() == a[i]){
num.back() ++;
}
else {
uni.push_back(move(a[i]));
num.push_back(1);
}
}
ll ans = 0;
if(uni[0] == Big(0)) {
ans += ll(num[0]) * (num[0]+1) * (num[0]+2) / 6;
}
cerr << TIME << endl;
// cerr << ans << endl;
ans += sum3(uni,num,M);
// cerr << ans << endl;
ans += sum3(uni,num,M+M);
cout << me(ans) << "\n";
}
signed main() {
#ifdef ONPC
freopen("../a.in", "r", stdin);
// freopen("../a.out", "w", stdout);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
cout << fixed;
cout.precision(20);
solve();
cerr << "\n\nConsumed " << TIME << endl;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3900kb
input:
4 1 0 1 10 17
output:
3
result:
ok 1 number(s): "3"
Test #2:
score: 0
Accepted
time: 55ms
memory: 4224kb
input:
500 859 7118711592236878297922359501613604144948355616986970837340677671376753603836852811886591300370143151943368529129749813118476151865844255212534355441611481420938483178075143062691345257288242460282715389758789648541099090735875617822348551942134616963557723055980260082230902505269975518146286...
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 373ms
memory: 7636kb
input:
500 17336 11871159223687829792235950161360414494835561698697083734067767137675360383685281188659130037014315194336852912974981311847615186584425521253435544161148142093848317807514306269134525728824246028271538975878964854109909073587561782234855194213461696355772305598026008223090250526997551814628...
output:
0
result:
ok 1 number(s): "0"
Test #4:
score: -100
Time Limit Exceeded
input:
500 1 751324443898124078584847834484321089092662321556147445230263526014359393841194947303407593948729802551881289193716611867931891257925091769456350249725997883453296895094445731130479434019358742162771547784250401546380268386074363779242500860317042151185119666027858022664683818314351285215150806...