QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#609226 | #8340. 3 Sum | yangrun | TL | 0ms | 3620kb | C++14 | 3.5kb | 2024-10-04 11:19:05 | 2024-10-04 11:19:08 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int, int>
#define fi first
#define se second
const int N = 2e5 + 10;
const int P = 13333331;
const int b1 = 107, b2 = 233;
const int mod1 = 1e9 + 7, mod2 = 233333333;
// 高精度运算
int cmp(const string& u, const string& v) {
if(u.size() != v.size()) {
if(u.size() < v.size()) return -1;
return 1;
}
for(int i = 0; i < u.size(); i++) {
if(u[i] != v[i]) {
if(u[i] < v[i]) return -1;
return 1;
}
}
return 0;
}
string add(const string& u, const string& v) {
string a = u, b = v;
reverse(a.begin(), a.end()), reverse(b.begin(), b.end());
string res;
int carry = 0;
for(int i = 0; i < max(a.size(), b.size()) || carry; ++i) {
int x = i < a.size() ? a[i] - '0' : 0;
int y = i < b.size() ? b[i] - '0' : 0;
int tmp = x + y + carry;
res += (tmp % 10) + '0';
carry= tmp / 10;
}
reverse(res.begin(), res.end());
return res;
}
string mul(const string& u, const string& v) {
string a = u, b = v;
reverse(a.begin(), a.end()), reverse(b.begin(), b.end());
string res;
int carry = 0;
for(int i = 0; i < b.size(); ++i) {
string now;
for(int j = 0; j < a.size(); ++j) {
int tmp = (b[i] - '0') * (a[j] - '0') + carry;
carry = tmp / 10;
now += (tmp % 10) + '0';
}
if(carry) now += carry + '0';
res = add(res, now + string(i, '0'));
}
reverse(res.begin(), res.end());
return res;
}
// 减法
string sub(const string& u, const string& v) {
string res;
string a = u, b = v;
reverse(a.begin(), a.end()), reverse(b.begin(), b.end());
int carry = 0;
for(int i = 0; i < a.size(); ++i) {
int x = a[i] - '0';
int y = i < b.size() ? b[i] - '0' : 0;
int tmp = x - y - carry;
if(tmp < 0) {
tmp += 10;
carry = 1;
} else {
carry =0;
}
res += tmp + '0';
}
while(res.size() > 1 && res.back() == '0') res.pop_back();
reverse(res.begin(), res.end());
return res;
}
// Divide
string dvi(const string& u, const string& v, string& r) {
string res;
string base;
int tmp;
for(int i = 0; i < u.size(); ++i) {
tmp = 0;
base += u[i];
while(cmp(base, v) >= 0) {
tmp++;
base = sub(base, v);
}
res += tmp + '0';
}
while(res.size() > 1 && res[0] == '0') res.erase(res.begin());
r = base;
return res;
}
pii in[N];
pii hk[7];
pair<int, int> getHash(string s) {
int r1 = 0, r2 = 0;
for(int i = 0; s[i]; ++i) {
r1 = ((r1 * 10) + (s[i] - '0')) % mod1;
r2 = ((r2 * 10) + (s[i] - '0')) % mod2;
}
return {r1, r2};
}
pii add(pii a, pii b) {
return {(a.fi + b.fi) % mod1, (a.se + b.se) % mod2};
}
signed main() {
// cout << getHash("4").fi << '\n';
// cout << getHash("6").fi << '\n';
// cout << getHash("10").fi << '\n';
int n, k;
cin >> n >> k;
string s, sk(k, '9');
pii hk0 = getHash(sk), cur = {0, 0};
for(int i = 0; i <= 2; i++) {
hk[i] = cur;
cur = add(cur, hk0);
}
for(int i = 1; i <= n; i++) {
cin >> s;
dvi(s, sk, s);
in[i] = getHash(s);
}
int res = 0;
for(int i = 1; i <= n; i++) {
for(int j = i; j <= n; j++) {
for(int k = j; k <= n; k++) {
int flg = 0;
cur = add(add(in[i], in[j]), in[k]);
for(int l = 0; l <= 2; l++) {
if(cur == hk[l]) flg = 1;
}
res += flg;
}
}
}
cout << res << '\n';
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3620kb
input:
4 1 0 1 10 17
output:
3
result:
ok 1 number(s): "3"
Test #2:
score: -100
Time Limit Exceeded
input:
500 859 7118711592236878297922359501613604144948355616986970837340677671376753603836852811886591300370143151943368529129749813118476151865844255212534355441611481420938483178075143062691345257288242460282715389758789648541099090735875617822348551942134616963557723055980260082230902505269975518146286...