QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#604222 | #8340. 3 Sum | tarjen | TL | 129ms | 4076kb | C++20 | 3.9kb | 2024-10-02 01:58:39 | 2024-10-02 01:58:40 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int p1=31,p2=131;
const int mod1=1e9+7,mod2=1e9+9;
struct hs{
// int x,y;
// bool operator<(hs h1)const{
// if(x==h1.x)return y<h1.y;
// else return x<h1.x;
// }
// bool operator==(hs h1)const{
// return x==h1.x&&y==h1.y;
// }
int x;
bool operator<(hs h1)const{
return x<h1.x;
}
bool operator==(hs h1)const{
return x==h1.x;
}
};
// const hs p = hs{p1,p2};
const hs p = hs{p1};
hs &operator+=(hs &a, hs b) {
a.x=(a.x+b.x)%mod1;
// a.y=(a.y+b.y)%mod2;
return a;
}
hs operator+(hs a, hs b) { return a += b; }
hs &operator-=(hs &a, hs b) {
a.x=(a.x-b.x+mod1)%mod1;
// a.y=(a.y-b.y+mod2)%mod2;
return a;
}
hs operator-(hs a, hs b) { return a -= b; }
hs &operator*=(hs &a, hs b) {
a.x=(1ll*a.x*b.x)%mod1;
// a.y=(1ll*a.y*b.y)%mod2;
return a;
}
hs operator*(hs a, hs b) { return a *= b; }
typedef vector<int> Bigint;
int K;
const int N=1000000000;
const int M=9;
int Pow10[10];
Bigint all;
int topbit(Bigint &a){
int g=(int)a.size()-1;
g*=M;
for(int i=1;i<M;i++)if(a.back()>=Pow10[i])g++;
// for(auto it:a)cout<<it<<"//";;cout<<"\n";
// cout<<"topbit="<<g<<"\n";
return g;
}
void down(Bigint &a){
int t=topbit(a);
int x=a.back()/Pow10[t%M];
// cout<<"t="<<t<<" a.back()="<<a.back()<<"\n";
a.back()%=Pow10[t%M];
// cout<<"t="<<t<<" a.back()="<<a.back()<<"\n";
t%=K;
if(a.back()==0&&(int)a.size()>1)a.pop_back();
a[t/M]+=x*Pow10[t%M];
}
void check(Bigint &a){
while(true){
// cout<<"??\n";
if(topbit(a)<K){
// cout<<"ok\n";
break;
}
while(topbit(a)>=K)down(a);
for(int i=0;i<(int)a.size();i++){
if(i==(int)a.size()-1&&a[i]>=N){
a.push_back(a[i]/N);
a[i]%=N;
continue;
}
a[i+1]+=a[i]/N;
a[i]%=N;
}
}
if(a==all)a=vector<int>{0};
}
Bigint operator+(Bigint a,Bigint b){
if(a.size()<b.size())swap(a,b);
for(int i=0;i<(int)b.size();i++)a[i]+=b[i];
check(a);
return a;
}
Bigint operator-(Bigint a,Bigint b){
for(int i=0;i<(int)b.size();i++)a[i]-=b[i];
check(a);
return a;
}
hs gethash(Bigint a){
hs now=hs{0};
for(int i=(int)a.size()-1;i>=0;i--)now=now*p+hs{a[i]};
return now;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin>>n>>K;
vector<Bigint> a(n);
vector<hs>has(n);
map<hs,int> ma;
Pow10[0]=1;
for(int i=1;i<10;i++)Pow10[i]=Pow10[i-1]*10;
{
int z=K;
while(z>=M){
all.push_back(Pow10[M]-1);
z-=M;
}
if(z>0)all.push_back(Pow10[z]-1);
}
for(int i=0;i<n;i++){
string s;cin>>s;
reverse(s.begin(),s.end());
for(int j=0;j<(int)s.size();j++){
if(j%M==0){
a[i].push_back(0);
}
a[i].back()+=(s[j]-'0')*Pow10[j%M];
}
check(a[i]);
has[i]=gethash(a[i]);
ma[has[i]]++;
// for(auto it:a[i])cout<<it<<"//";;cout<<"\n";
}
int ans=0;
hs hall=gethash(all);
for(int i=0;i<n;i++){
for(int j=i;j>=0;j--){
hs h1=gethash(a[i]+a[j]);
hs h=hall-h1;
if(h==hall)h=hs{0};
// cout<<"i="<<i<<" j="<<j<<" "<<h.x<<" "<<h.y<<"\n";
if(ma.find(h)!=ma.end()){
ans+=ma[h];
// cout<<"i="<<i<<" j="<<j<<" ans="<<ma[h]<<"\n";
// for(auto it:a[i])cout<<it;;cout<<"\n";
// for(auto it:a[j])cout<<it;;cout<<"\n";
// for(auto it:(a[i]+a[j]))cout<<it;;cout<<"\n";
}
}
ma[has[i]]--;
}
cout<<ans<<"\n";
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3548kb
input:
4 1 0 1 10 17
output:
3
result:
ok 1 number(s): "3"
Test #2:
score: 0
Accepted
time: 129ms
memory: 4076kb
input:
500 859 7118711592236878297922359501613604144948355616986970837340677671376753603836852811886591300370143151943368529129749813118476151865844255212534355441611481420938483178075143062691345257288242460282715389758789648541099090735875617822348551942134616963557723055980260082230902505269975518146286...
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: -100
Time Limit Exceeded
input:
500 17336 11871159223687829792235950161360414494835561698697083734067767137675360383685281188659130037014315194336852912974981311847615186584425521253435544161148142093848317807514306269134525728824246028271538975878964854109909073587561782234855194213461696355772305598026008223090250526997551814628...