QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#417686 | #8340. 3 Sum | Mobed | TL | 1ms | 3472kb | C++14 | 3.8kb | 2024-05-22 20:46:41 | 2024-05-22 20:46:42 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 1;
const int MOD = 1e9 + 7;
/*
const int N = 1e5 + 5;
int fac[N], inv[N];
int sum(int a, int b) { return (a + b + MOD + MOD) % MOD; }
int mul(int a, int b) { return 1ll * a * b % MOD; }
int Pow(int a, int b) {
int res = 1;
for (; b; b>>=1, a = mul(a, a)) if (b&1) {
res = mul(res, a);
}
return res;
}
int C(int n, int k) {
return mul(fac[n], mul(inv[k], inv[n - k]));
}
void prep() {
fac[0] = 1;
for (int i = 1; i < N; i++) fac[i] = mul(fac[i - 1], i);
inv[N - 1] = Pow(fac[N - 1], MOD - 2);
for (int i = N - 2; i >= 0; i--) inv[i] = mul(inv[i + 1], i + 1);
}
*/
struct num {
vector<int> a;
int k;
num(int _k) {
k = _k;
a.resize(k);
}
num(int _k, int x) {
k = _k;
a.resize(k);
a[0] = x;
}
void set_digits(vector<int> digits) {
a = digits;
a.resize(k);
mod();
}
bool operator==(const num &b) const {
if (k != b.k) {
return false;
}
for (int i = 0; i < k; i++) {
if (a[i] != b.a[i]) {
return false;
}
}
return true;
}
void mod() {
for (int i = 0; i < k; i++) {
if (a[i] != 9) return;
}
vector<int> zeros(k);
a = zeros;
}
void print() {
for (int i = k - 1; i >= 0; i--) {
cout << a[i];
}
cout << "\n";
}
};
num operator+(const num &a, const num &b) {
num res(a.k);
int carry = 0;
for (int i = 0; i < a.k; i++) {
res.a[i] = a.a[i] + b.a[i] + carry;
carry = res.a[i] / 10;
res.a[i] %= 10;
}
if (carry) {
res = res + num(a.k, carry);
}
res.mod();
return res;
}
int n, k;
map<int, int> cnt;
int Hash(num x) {
int res = 0;
for (int i = 0; i < x.k; i++) {
res = (res * 10 + x.a[i]) % MOD;
}
return res;
}
void add(vector<num> &nums, string s) {
num res(k);
reverse(s.begin(), s.end());
int n = s.size();
//cout << "Adding:" << s << "\n";
for (int i = 0; i < n; i += k) {
vector<int> digits;
for (int j = i; j < i + k; j++) {
if (j < n) {
digits.push_back(s[j] - '0');
} else {
digits.push_back(0);
}
}
num x(k);
x.set_digits(digits);
//cout << "Adding:";
// x.print();
res = res + x;
}
//cout << "Result:";
// res.print();
cnt[Hash(res)]++;
nums.push_back(res);
}
num calc_rem(num x) {
num res(x.k);
for (int i = 0; i < x.k; i++) {
res.a[i] = 9 - x.a[i];
}
res.mod();
return res;
}
void Solve() {
cin >> n >> k;
vector<num> nums;
for (int i = 0; i < n; i++) {
string s; cin >> s;
add(nums, s);
}
int ans = 0;
// for (int i = 0; i < n; i++) nums[i].print();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) if (i != j) {
num sum = nums[i] + nums[j];
num rem = calc_rem(sum);
if (rem == nums[i]) ans--;
if (rem == nums[j]) ans--;
ans += cnt[Hash(rem)];
}
}
// cout << ans << "\n";
ans /= 6;
for (int i = 0; i < n; i++) {
num sum = nums[i] + nums[i];
num rem = calc_rem(sum);
ans += cnt[Hash(rem)];
}
cout << ans << "\n";
}
int main() {
// prep();
// cout << C(5, 4) << endl;
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1; // cin >> t;
while (t--) {
Solve();
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3472kb
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...