QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#424328 | #8340. 3 Sum | Mobed | AC ✓ | 438ms | 27376kb | C++14 | 4.7kb | 2024-05-29 06:45:05 | 2024-05-29 06:45:06 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int MOD = 1000696969;
int powmod(int a, int b, int m) {
int res = 1;
while (b) {
if (b & 1) res = (1ll * res * a) % m;
a = (1ll * a * a) % m;
b >>= 1;
}
return res;
}
struct mod {
const static int k = 2;
const static int p[k];
int x[k];
mod() {
memset(x, 0, sizeof x);
}
mod(int val) {
for (int i = 0; i < k; i++) x[i] = val % p[i];
}
mod operator+=(const mod& a) {
for (int i = 0; i < k; i++) {
x[i] = (0ll + x[i] + a.x[i]) % p[i];
if (x[i] < 0)
x[i] += p[i];
}
return *this;
}
mod operator-=(const mod& a) {
for (int i = 0; i < k; i++) {
x[i] = (0ll + x[i] - a.x[i] + p[i]) % p[i];
}
return *this;
}
mod operator*=(const mod& a) {
for (int i = 0; i < k; i++) {
x[i] = (1ll * x[i] * a.x[i]) % p[i];
}
return *this;
}
mod operator/=(const mod& a) {
mod inv = mod(a).inverse();
return *this *= inv;
}
mod inverse() const {
int inv[k];
for (int i = 0; i < k; i++) {
inv[i] = powmod(x[i], p[i] - 2, p[i]);
}
mod result;
for (int i = 0; i < k; i++) {
result.x[i] = inv[i];
}
return result;
}
bool operator==(const mod& other) const {
for (int i = 0; i < k; i++) {
if (x[i] != other.x[i]) {
return false;
}
}
return true;
}
bool operator!=(const mod& other) const {
return !(*this == other);
}
bool operator<(const mod& other) const {
for (int i = 0; i < k; i++) {
if (x[i] < other.x[i]) {
return true;
} else if (x[i] > other.x[i]) {
return false;
}
}
return false;
}
bool operator<=(const mod& other) const {
return *this == other || *this < other;
}
bool operator>(const mod& other) const {
return !(*this <= other);
}
bool operator>=(const mod& other) const {
return !(*this < other);
}
};
const int mod::p[mod::k] = {1000000007, 1000000009};
mod operator+(mod a, const mod& b) { return a += b; }
mod operator-(mod a, const mod& b) { return a -= b; }
mod operator*(mod a, const mod& b) { return a *= b; }
mod operator/(mod a, const mod& b) { return a /= b; }
vector<int> get_digits(string s) {
vector<int> digits;
for (char c : s) digits.push_back(c - '0');
reverse(digits.begin(), digits.end());
return digits;
}
mod Hash(vector<int> &digits) {
mod res = mod();
for (int i = digits.size() - 1; i >= 0; i--) {
res = res * mod(10) + mod(digits[i]);
}
return res;
}
void add(vector<mod> &a, vector<int> &digits, int k) {
int n = digits.size();
vector<int> num(k);
for (int i = 0; i < n; i++) {
num[i%k] += digits[i];
}
for (int i = 0; i < num.size(); i++) {
if (num[i] < 10) continue;
if (i + 1 == num.size()) num.push_back(0);
num[i + 1] += num[i] / 10;
num[i] %= 10;
}
if (num.size() == k) {
bool flag = 1;
for (int i = 0; i < k; i++) if (num[i] != 9) flag = 0;
if (flag)
for (int i = 0; i < k; i++) num[i] = 0;
a.push_back(Hash(num));
} else {
add(a, num, k);
}
}
map<mod, int> cnt;
int threesum(vector<mod> &a, mod s) {
int n = a.size();
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) if (i != j) {
mod rem = s - (a[i] + a[j]);
if (rem == a[i]) ans--;
if (rem == a[j]) ans--;
ans += cnt[rem];
}
}
ans /= 6;
for (int i = 0; i < n; i++) {
mod rem = s - (a[i] + a[i]);
ans += cnt[rem];
}
return ans;
}
void Solve() {
int n, k; cin >> n >> k;
vector<mod> a;
for (int i = 0; i < n; i++) {
string s; cin >> s;
vector<int> digits = get_digits(s);
add(a, digits, k);
}
for (int i = 0; i < n; i++) cnt[a[i]]++;
int ans = 0;
ans += threesum(a, 0);
vector<int> d;
for (int i = 0; i < k; i++) d.push_back(9);
mod h = Hash(d);
ans += threesum(a, h);
d.clear();
d.push_back(8);
for (int i = 0; i < k - 1; i++) d.push_back(9);
d.push_back(1);
h = Hash(d);
ans += threesum(a, h);
cout << ans << '\n';
}
int main () {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1; //cin >> t;
while (t--) Solve();
}
这程序好像有点Bug,我给组数据试试?
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3860kb
input:
4 1 0 1 10 17
output:
3
result:
ok 1 number(s): "3"
Test #2:
score: 0
Accepted
time: 308ms
memory: 27376kb
input:
500 859 7118711592236878297922359501613604144948355616986970837340677671376753603836852811886591300370143151943368529129749813118476151865844255212534355441611481420938483178075143062691345257288242460282715389758789648541099090735875617822348551942134616963557723055980260082230902505269975518146286...
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 438ms
memory: 27180kb
input:
500 17336 11871159223687829792235950161360414494835561698697083734067767137675360383685281188659130037014315194336852912974981311847615186584425521253435544161148142093848317807514306269134525728824246028271538975878964854109909073587561782234855194213461696355772305598026008223090250526997551814628...
output:
0
result:
ok 1 number(s): "0"
Test #4:
score: 0
Accepted
time: 71ms
memory: 3724kb
input:
500 1 751324443898124078584847834484321089092662321556147445230263526014359393841194947303407593948729802551881289193716611867931891257925091769456350249725997883453296895094445731130479434019358742162771547784250401546380268386074363779242500860317042151185119666027858022664683818314351285215150806...
output:
2327631
result:
ok 1 number(s): "2327631"
Test #5:
score: 0
Accepted
time: 72ms
memory: 3732kb
input:
500 2 408542968136435277974575411503179002415404345446801430469044749372949272333801248935236224652806667129749035002588943020176263162139056819871274824302889304493205266143688886696157147111754418731401856424401766968832165255416237731963027205324149660112574729610391396555581935236134531799950318...
output:
212002
result:
ok 1 number(s): "212002"
Test #6:
score: 0
Accepted
time: 192ms
memory: 4600kb
input:
500 11500 75411775796562109942642493394321873284995260953010112281856775261847503626737348402159485133662757032091519863427156582689971229143089317472838196453888261138079171290535429921921548971897026706656838415620603757605079012541561774699628665865662183868374645956694140356716037674688084770628...
output:
7675
result:
ok 1 number(s): "7675"
Test #7:
score: 0
Accepted
time: 283ms
memory: 10368kb
input:
500 11500 85355036663164764459816544518601485185320972076300982726542821424439713703229669576802138231401047471351087455159512255765325323540671792953715169122669767368905391325060775725733157611188832204902997772518104188947349204726490597030311894441123834099315122116302203972018409854605418988681...
output:
1070
result:
ok 1 number(s): "1070"
Test #8:
score: 0
Accepted
time: 1ms
memory: 3684kb
input:
1 11500 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...
output:
1
result:
ok 1 number(s): "1"
Extra Test:
score: 0
Extra Test Passed