QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#348651 | #8340. 3 Sum | ucup-team1600# | AC ✓ | 200ms | 27660kb | C++17 | 5.9kb | 2024-03-09 20:17:18 | 2024-10-13 18:48:25 |
Judging History
answer
//#pragma GCC optimize("Ofast", "unroll-loops")
//#pragma GCC target("sse", "sse2", "sse3", "ssse3", "sse4")
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <unordered_map>
#include <iomanip>
#include <cmath>
#include <queue>
#include <bitset>
#include <numeric>
#include <array>
#include <cstring>
#include <random>
#include <chrono>
#include <cassert>
#define all(a) a.begin(),a.end()
#define len(a) (int)(a.size())
#define mp make_pair
#define pb push_back
#define fir first
#define sec second
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
template<typename T>
bool umin(T &a, T b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template<typename T>
bool umax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
#ifdef KIVI
#define DEBUG for (bool _FLAG = true; _FLAG; _FLAG = false)
#define LOG(...) print(#__VA_ARGS__" ::", __VA_ARGS__) << endl
template<class ...Ts>
auto &print(Ts ...ts) { return ((cerr << ts << " "), ...); }
#else
#define DEBUG while (false)
#define LOG(...)
#endif
mt19937 rng(4242);
const int max_n = 1e5 + 11, inf = 1000111222;
const bool DBG = false;
const int mod1 = 1e9 + 7, mod2 = 1e9 + 9;
int now[max_n];
void solve() {
int n, k;
cin >> n >> k;
vector<string> a(n);
for(auto& x : a) {
if(DBG) {
for (int i = 0; i < 100000; i++) x += (char) (rng() % 10 + '0');
// x = to_string(rng() % 1000000000);
} else {
cin >> x;
}
}
//take a mod 10^k - 1 = 9...9 (k times)
string MODS = string(k, '9');
int am_zeroes = 0;
for (auto &i : a) {
string was = i;
int cc = 0;
int l = 0, r = len(i) - 1;
for (int j = 0; j < len(i); j++) {
now[j] = i[j] - '0';
}
while (true) {
for (int j = l; j + k <= r; j++) {
now[j + k] += now[j];
now[j] = 0;
}
int carry = 0;
for (int j = r; j >= l; j--) {
int val = now[j] + carry;
carry = val / 10;
now[j] = val % 10;
}
while (l <= r && now[l] == 0) {
++l;
}
if (r - l + 1 <= k) {
break;
}
}
if (r - l + 1 == 0) {
i = "0";
}
else {
i = string(r - l + 1, '0');
for (int j = l; j <= r; j++) {
i[j - l] = char('0' + now[j]);
}
}
if(i.empty()) i = "0";
if(i == MODS) i = "0";
if(count(all(i), '0') == len(i)) am_zeroes++;
// cout << stoi(i) << ' ' << stoi(was) << ' ' << stoi(was) % 99 << endl;
// if(stoi(i) != stoi(was) % 999) exit(-1);
// assert(stoi(i) == stoi(was) % 99);
}
ll ans = (am_zeroes + am_zeroes * 1ll * (am_zeroes - 1) * (am_zeroes - 2) / 6 + am_zeroes * (am_zeroes - 1) / 2 * 2);
string need1S = string(k, '9');
string need2S = string(1, '1') + string(k - 1, '9') + string(1, '8');
pair<int, int> need1 = {0, 0}, need2 = {0, 0};
{
for (int j = 0; j < len(need1S); j++) {
need1.fi = (need1.fi * 1ll * 10 + need1S[j] - '0') % mod1;
need1.se = (need1.se * 1ll * 10 + need1S[j] - '0') % mod2;
}
for (int j = 0; j < len(need2S); j++) {
need2.fi = (need2.fi * 1ll * 10 + need2S[j] - '0') % mod1;
need2.se = (need2.se * 1ll * 10 + need2S[j] - '0') % mod2;
}
}
vector<pair<int, int> > rem(n, {0, 0});
for(int guy = 0; guy < n; guy++) {
for(int i = 0; i < len(a[guy]); i++) {
rem[guy].fi = (rem[guy].fi * 1ll * 10 + (a[guy][i] - '0')) % mod1;
rem[guy].se = (rem[guy].se * 1ll * 10 + (a[guy][i] - '0')) % mod2;
}
}
map<pair<int, int>, int> am;
for(int i = 0; i < n; i++) {
// for(int j = i; j < n; j++)
// for(int K = j; K < n; K++) {
// ans += (rem[i].fi + rem[j].fi + rem[K].fi) % mod1 == need1.fi && (rem[i].se + rem[j].se + rem[K].se) % mod2 == need1.se;
// ans += (rem[i].fi + rem[j].fi + rem[K].fi) % mod1 == need2.fi && (rem[i].se + rem[j].se + rem[K].se) % mod2 == need2.se;
// }
for(int j = i; j < n; j++) am[rem[j]]++;
auto cneed1 = pii{need1.fi - rem[i].fi, need1.se - rem[i].se};
if(cneed1.fi < 0) cneed1.fi += mod1;
if(cneed1.se < 0) cneed1.se += mod2;
auto cneed2 = pii{need2.fi - rem[i].fi, need2.se - rem[i].se};
if(cneed2.fi < 0) cneed2.fi += mod1;
if(cneed2.se < 0) cneed2.se += mod2;
for(int j = i; j < n; j++) {
auto ccneed1 = pii{cneed1.fi - rem[j].fi, cneed1.se - rem[j].se};
if(ccneed1.fi < 0) ccneed1.fi += mod1;
if(ccneed1.se < 0) ccneed1.se += mod2;
auto ccneed2 = pii{cneed2.fi - rem[j].fi, cneed2.se - rem[j].se};
if(ccneed2.fi < 0) ccneed2.fi += mod1;
if(ccneed2.se < 0) ccneed2.se += mod2;
ans += am[ccneed1];
ans += am[ccneed2];
am[rem[j]]--;
}
}
cout << ans << '\n';
// ll ans2 = 0;
// for(int i = 0; i < n; i++)
// for(int j = i; j < n; j++)
// for(int K = j; K < n; K++)
// if((stoi(a[i]) + stoi(a[j]) + stoi(a[K])) % 999 == 0) {
// ans2++;
// }
// cout << ans2 << '\n';
}
int main() {
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
while (t--) solve();
}
/*
*
*/
这程序好像有点Bug,我给组数据试试?
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3696kb
input:
4 1 0 1 10 17
output:
3
result:
ok 1 number(s): "3"
Test #2:
score: 0
Accepted
time: 141ms
memory: 19812kb
input:
500 859 7118711592236878297922359501613604144948355616986970837340677671376753603836852811886591300370143151943368529129749813118476151865844255212534355441611481420938483178075143062691345257288242460282715389758789648541099090735875617822348551942134616963557723055980260082230902505269975518146286...
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 200ms
memory: 27660kb
input:
500 17336 11871159223687829792235950161360414494835561698697083734067767137675360383685281188659130037014315194336852912974981311847615186584425521253435544161148142093848317807514306269134525728824246028271538975878964854109909073587561782234855194213461696355772305598026008223090250526997551814628...
output:
0
result:
ok 1 number(s): "0"
Test #4:
score: 0
Accepted
time: 68ms
memory: 15100kb
input:
500 1 751324443898124078584847834484321089092662321556147445230263526014359393841194947303407593948729802551881289193716611867931891257925091769456350249725997883453296895094445731130479434019358742162771547784250401546380268386074363779242500860317042151185119666027858022664683818314351285215150806...
output:
2327631
result:
ok 1 number(s): "2327631"
Test #5:
score: 0
Accepted
time: 62ms
memory: 15032kb
input:
500 2 408542968136435277974575411503179002415404345446801430469044749372949272333801248935236224652806667129749035002588943020176263162139056819871274824302889304493205266143688886696157147111754418731401856424401766968832165255416237731963027205324149660112574729610391396555581935236134531799950318...
output:
212002
result:
ok 1 number(s): "212002"
Test #6:
score: 0
Accepted
time: 112ms
memory: 16076kb
input:
500 11500 75411775796562109942642493394321873284995260953010112281856775261847503626737348402159485133662757032091519863427156582689971229143089317472838196453888261138079171290535429921921548971897026706656838415620603757605079012541561774699628665865662183868374645956694140356716037674688084770628...
output:
7675
result:
ok 1 number(s): "7675"
Test #7:
score: 0
Accepted
time: 135ms
memory: 16048kb
input:
500 11500 85355036663164764459816544518601485185320972076300982726542821424439713703229669576802138231401047471351087455159512255765325323540671792953715169122669767368905391325060775725733157611188832204902997772518104188947349204726490597030311894441123834099315122116302203972018409854605418988681...
output:
1070
result:
ok 1 number(s): "1070"
Test #8:
score: 0
Accepted
time: 1ms
memory: 3712kb
input:
1 11500 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...
output:
1
result:
ok 1 number(s): "1"
Extra Test:
score: 0
Extra Test Passed