QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#348388 | #8340. 3 Sum | ucup-team1191# | TL | 38ms | 15188kb | C++20 | 26.5kb | 2024-03-09 18:10:08 | 2024-03-09 18:10:09 |
Judging History
answer
/*
author: Maksim1744
created: 09.03.2024 12:03:07
*/
#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// NOLINTBEGIN
// clang-format off
// DO NOT REMOVE THIS MESSAGE. The mess that follows is a minified build of
// https://github.com/purplesyringa/blazingio. Refer to the repository for
// a human-readable version and documentation.
// Options: cbfoiedrhWLMXaIaAn
#define M$(x,...)_mm256_##x##_epi8(__VA_ARGS__)
#define $u(...)__VA_ARGS__
#if __APPLE__
#define $m(A,B)A
#else
#define $m(A,B)B
#endif
#if _WIN32
#define $w(A,B)A
#else
#define $w(A,B)B
#endif
#if __i386__|_M_IX86
#define $H(A,B)A
#else
#define $H(A,B)B
#endif
#if __aarch64__
#define $a(A,B)A
#else
#define $a(A,B)B
#endif
#define $P(x)void F(x K){
#define $T template<$c T
#define $c class
#define $C constexpr
#define $R return
#define $O operator
#define $r $R*this;
#include<array>
#include<bitset>
#include<complex>
#include<cstring>
#include $a(<arm_neon.h>,<immintrin.h>)
#include<stdint.h>
#include $w(<windows.h>,<sys/mman.h>)
#include<sys/stat.h>
#include $w(<io.h>,<unistd.h>)
#include $w(<ios>,<sys/resource.h>)
#ifdef _MSC_VER
#define __builtin_add_overflow(a,b,c)_addcarry_u64(0,a,b,c)
#define $s
#else
$H(,uint64_t _umul128(uint64_t a,uint64_t b,uint64_t*D){auto x=(__uint128_t)a*b;*D=x>>64;$R x;})
#define $s $a(,__attribute__((target("avx2"))))
#endif
#define $z $a(16,32)
#define $t $a(uint8x16_t,__m256i)
#define $I $w(__forceinline,__attribute__((always_inline)))
#define $F M(),
#define E$(x)if(!(x))abort();
$w(LONG WINAPI $x(_EXCEPTION_POINTERS*);,)namespace $f{using namespace std;struct B{enum $c A:char{};A c;B&$O=(char x){c=A{x};$r}$O char(){$R(char)c;}};$C uint64_t C=~0ULL/255;struct D{string&K;};static B E[65568];template<int F>struct G{B*H;B*I;void K(off_t C){$w(char*D=(char*)VirtualAlloc(0,(C+8191)&-4096,8192,1);E$(D)E$(VirtualFree(D,0,32768))DWORD A=C&-65536;if(A)E$(MapViewOfFileEx(CreateFileMapping(GetStdHandle(-10),0,2,0,A,0),4,0,0,0,D)==D)E$(VirtualAlloc(D+A,65536,12288,4)==D+A)E$(~_lseek(0,A,0))DWORD E=0;ReadFile(GetStdHandle(-10),D+A,65536,&E,0);,int A=getpagesize();char*D=(char*)mmap(0,C+A,3,2,0,0);E$(D!=(void*)-1)E$(mmap(D+((C+A-1)&-A),A,3,$m(4114,50),-1,0)!=(void*)-1))H=(B*)D+C;*H=10;H[1]=48;H[2]=0;I=(B*)D;}void L(){H=I=E;}$I void M(){if(F&&I==H){$w(DWORD A=0;ReadFile(GetStdHandle(-10),I=E,65536,&A,0);,$a($u(register long A asm("x0")=0,D asm("x1")=(long)E,G asm("x2")=65536,C asm($m("x16","x8"))=$m(3,63);asm volatile("svc 0"$m("x80",):"+r"(A),"+r"(D):"r"(C),"r"(G));I=launder(E);),off_t A=$H(3,$m(33554435,0));B*D=E;asm volatile($H("int $128","syscall"):"+a"(A),$H("+c"(D):"b","+S"(D):"D")(0),"d"(65536)$H(,$u(:"rcx","r11")));I=D;))H=I+A;*H=10;if(!A)E[1]=48,E[2]=0,H=0;}}$T>$I void N(T&x){while($F(*I&240)==48)x=x*10+(*I++-48);}$T>$I decltype((void)~T{1})O(T&x){int A=is_signed_v<T>&&($F*I==45);I+=A;N(x=0);x=A?1+~x:x;}$T>$I decltype((void)T{1.})O(T&x){int A=($F*I==45);I+=A;$F I+=*I==43;uint64_t n=0;int i=0;for(;i<18&&($F*I&240)==48;i++)n=n*10+*I++-48;int B=20;int C=*I==46;I+=C;for(;i<18&&($F*I&240)==48;i++)n=n*10+*I++-48,B-=C;x=(T)n;while(($F*I&240)==48)x=x*10+*I++-48,B-=C;if(*I==46)I++,C=1;while(($F*I&240)==48)x=x*10+*I++-48,B-=C;int D;if((*I|32)==101)I++,$F I+=*I==43,O(D),B+=D;static $C auto E=[](){array<T,41>E{};T x=1;for(int i=21;i--;)E[40-i]=x,E[i]=1/x,x*=10;$R E;}();while(B>40)x*=(T)1e10,B-=10;while(B<0)x*=(T)1e-10,B+=10;x*=E[B];x=A?-x:x;}$I void O(bool&x){$F x=*I++==49;}$I void O(char&x){$F x=*I++;}$I void O(uint8_t&x){$F x=*I++;}$I void O(int8_t&x){$F x=*I++;}$T>$s void P(string&K,T C){M();B*G=I;C();K.assign((const char*)G,I-G);while(F&&I==H){if($F!H){H=I;break;}C();K.append(E,I);}}$s void O(string&K){P(K,[&]()$s{$t*p=($t*)I;$w(ULONG R;,)$a(uint64x2_t A;while(A=(uint64x2_t)(*p<33),!(A[0]|A[1]))p++;I=(B*)p+(A[0]?0:8)+$w((_BitScanForward64(&R,A[0]?:A[1]),R),__builtin_ctzll(A[0]?:A[1]))/8;,int J;__m256i C=M$(set1,32);while(!(J=M$(movemask,M$(cmpeq,C,_mm256_max_epu8(C,_mm256_loadu_si256(p))))))p++;I=(B*)p+$w((_BitScanForward(&R,J),R),__builtin_ctz(J));)});}$s void O(D&A){P(A.K,[&](){I=(B*)memchr(I,10,H-I+1);});if(A.K.size()&&A.K.back()==13)A.K.pop_back();if(A.K.empty()||I<H)I+=*I==10;}$T>$I void O(complex<T>&K){T A,B{};if($F*I==40){I++;O(A);if($F*I++==44)Q(B),I++;}else O(A);K={A,B};}template<size_t N>$s void O(bitset<N>&K){if(N>4095&&!*this)$R;ptrdiff_t i=N;while(i)if($F i%$z||H-I<$z)K[--i]=*I++==49;else{$t*p=($t*)I;for(int64_t j=0;j<min(i,H-I)/$z;j++){i-=$z;$a(auto B=(uint8x16_t)vdupq_n_u64(~2ULL/254)&(48-*p++);auto C=vzip_u8(vget_high_u8(B),vget_low_u8(B));((uint16_t*)&K)[i/16]=vaddvq_u16((uint16x8_t)vcombine_u8(C.val[0],C.val[1]));,uint64_t a=~0ULL/65025;((uint32_t*)&K)[i/32]=$w(_byteswap_ulong,__builtin_bswap32)(M$(movemask,M$(shuffle,_mm256_slli_epi32(_mm256_loadu_si256(p++),7),_mm256_set_epi64x(a+C*24,a+C*16,a+C*8,a))));)}I=(B*)p;}}$T>$I void Q(T&K){if(!is_same_v<T,D>)while($F 0<=*I&&*I<33)I++;O(K);}$O bool(){$R!!*this;}bool $O!(){$R I>H;}};struct $i{G<0>A;G<1>B;$i(){struct stat D;E$(~fstat(0,&D))(D.st_mode>>12)==8?A.K(D.st_size):B.L();}$i*tie(nullptr_t){$R this;}void sync_with_stdio(bool){}$T>$I $i&$O>>(T&K){A.I?A.Q(K):B.Q(K);$r}$O bool(){$R!!*this;}bool $O!(){$R A.I?!A:!B;}};uint16_t A[100];char L[64]{1};struct
$o{char*D;B*I;int J;$o(){$w(E$(D=(char*)VirtualAlloc(0,536870912,8192,4))E$(VirtualAlloc(D,4096,4096,260))AddVectoredExceptionHandler(1,$x);,size_t C=536870912;$m(,struct rlimit E;getrlimit(RLIMIT_AS,&E);if(~E.rlim_cur)C=25165824;)D=(char*)mmap(0,C,3,$m(4162,16418),-1,0);E$(D!=(void*)-1))I=(B*)D;for(int i=0;i<100;i++)A[i]=(48+i/10)|((48+i%10)<<8);for(int i=1;i<64;i++)L[i]=L[i-1]+(0x8922489224892249>>i&1);}~$o(){flush(!J);}void flush(int F=0){$w(J=1;auto E=GetStdHandle(-11);auto C=F?ReOpenFile(E,1073741824,7,2684354560):(void*)-1;DWORD A;E$(C==(void*)-1?WriteFile(E,D,DWORD((char*)I-D),&A,0):(WriteFile(C,D,DWORD(((char*)I-D+4095)&-4096),&A,0)&&~_chsize(1,int((char*)I-D)))),auto G=D;ssize_t A;while((A=write(1,G,(char*)I-G))>0)G+=A;E$(~A))I=(B*)D;}$P(char)*I++=K;}$P(uint8_t)*I++=K;}$P(int8_t)*I++=K;}$P(bool)*I++=48+K;}$T>decltype((void)~T{1})F(T K){using D=make_unsigned_t<T>;D C=K;if(K<0)F('-'),C=1+~C;static $C auto N=[](){array<D,5*sizeof(T)/2>N{};D n=1;for(size_t i=1;i<N.size();i++)n*=10,N[i]=n;$R N;}();$w(ULONG M;,)int G=L[$w(($H(_BitScanReverse(&M,ULONG((int64_t)C>>32))?M+=32:_BitScanReverse(&M,(ULONG)C|1),_BitScanReverse64(&M,C|1)),M),63^__builtin_clzll(C|1))];G-=C<N[G-1];uint16_t H[20];if $C(sizeof(T)==2){auto n=33555U*C-C/2;uint64_t H=A[n>>25];n=(n&33554431)*25;H|=A[n>>23]<<16;H|=uint64_t(48+((n&8388607)*5>>22))<<32;H>>=40-G*8;memcpy(I,&H,8);}else if $C(sizeof(T)==4){auto n=1441151881ULL*C;$H(n>>=25;n++;for(int i=0;i<5;i++){H[i]=A[n>>32];n=(n&~0U)*100;},int K=57;auto J=~0ULL>>7;for(int i=0;i<5;i++){H[i]=A[n>>K];n=(n&J)*25;K-=2;J/=4;})memcpy(I,(B*)H+10-G,16);}else{$H($u(if(C<(1ULL<<32)){$R F((uint32_t)C);}auto J=(uint64_t)1e10;auto x=C/J,y=C%J;int K=100000,b[]{int(x/K),int(x%K),int(y/K),int(y%K)};B H[40];for(int i=0;i<4;i++){uint32_t n=(429497ULL*b[i]>>7)+1;B*p=H+i*5;*p=48+(n>>25);n=(n&~0U>>7)*25;memcpy(p+1,A+(n>>23),2);memcpy(p+3,A+((n&~0U>>9)*25>>21),2);}),$u(uint64_t D,E=_umul128(18,C,&D),F;_umul128(0x725dd1d243aba0e8,C,&F);D+=__builtin_add_overflow(E,F+1,&E);for(int i=0;i<10;i++)H[i]=A[D],E=_umul128(100,E,&D);))memcpy(I,(B*)H+20-G,20);}I+=G;}$T>decltype((void)T{1.})F(T K){if(K<0)F('-'),K=-K;auto G=[&](){auto x=uint64_t(K*1e12);$H($u(x-=x==1000000000000;uint32_t n[]{uint32_t(x/1000000*429497>>7)+1,uint32_t(x%1000000*429497>>7)+1};int K=25,J=~0U>>7;for(int i=0;i<3;i++){for(int j=0;j<2;j++)memcpy(I+i*2+j*6,A+(n[j]>>K),2),n[j]=(n[j]&J)*25;K-=2;J/=4;}I+=12;),$u(uint64_t D,E=_umul128(472236648287,x,&D)>>8;E|=D<<56;D>>=8;E++;for(int i=0;i<6;i++)memcpy(I,A+D,2),I+=2,E=_umul128(100,E,&D);))};if(!K)$R F('0');if(K>=1e16){K*=(T)1e-16;int B=16;while(K>=1)K*=(T).1,B++;F("0.");G();F('e');F(B);}else if(K>=1){auto B=(uint64_t)K;F(B);if(K-=B)F('.'),G();}else F("0."),G();}$P(const char*)$w(size_t A=strlen(K);memcpy((char*)I,K,A);I+=A;,I=(B*)stpcpy((char*)I,K);)}$P(const uint8_t*)F((char*)K);}$P(const int8_t*)F((char*)K);}$P(string_view)memcpy(I,K.data(),K.size());I+=K.size();}$T>$P(complex<T>)*this<<'('<<K.real()<<','<<K.imag()<<')';}template<size_t N>$s $P(const bitset<N>&)auto i=N;while(i%$z)*I++=48+K[--i];$t*p=($t*)I;i/=$z;while(i){$a(auto A=(uint8x8_t)vdup_n_u16(((uint16_t*)&K)[--i]);*p++=48-vtstq_u8(vcombine_u8(vuzp2_u8(A,A),vuzp1_u8(A,A)),(uint8x16_t)vdupq_n_u64(~2ULL/254));,auto b=_mm256_set1_epi64x(~2ULL/254);_mm256_storeu_si256(p++,M$(sub,M$(set1,48),M$(cmpeq,_mm256_and_si256(M$(shuffle,_mm256_set1_epi32(((uint32_t*)&K)[--i]),_mm256_set_epi64x(0,C,C*2,C*3)),b),b)));)}I=(B*)p;}$T>$o&$O<<(const T&K){F(K);$r}$o&$O<<($o&(*A)($o&)){$R A(*this);}};struct $e{$T>$e&$O<<(const T&K){$r}$e&$O<<($e&(*A)($e&)){$R A(*this);}};}namespace std{$f::$i i$;$f::$o o$;$f::$e e$;$f::$i&getline($f::$i&B,string&K){$f::D A{K};$R B>>A;}$f::$o&flush($f::$o&B){if(!i$.A.I)B.flush();$R B;}$f::$o&endl($f::$o&B){$R B<<'\n'<<flush;}$f::$e&endl($f::$e&B){$R B;}$f::$e&flush($f::$e&B){$R B;}}$w(LONG WINAPI $x(_EXCEPTION_POINTERS*A){auto C=A->ExceptionRecord;char*B=(char*)C->ExceptionInformation[1];if(C->ExceptionCode==2147483649&&(size_t)(B-std::o$.D)<0x40000000){E$(VirtualAlloc(B,16777216,4096,4)&&VirtualAlloc(B+16777216,4096,4096,260))$R-1;}$R 0;},)
#define freopen(...)if(freopen(__VA_ARGS__)==stdin)std::i$=$f::$i{}
#define cin i$
#define cout o$
#ifdef ONLINE_JUDGE
#define cerr e$
#define clog e$
#endif
// End of blazingio
// NOLINTEND
// clang-format on
using ll = long long;
using ld = long double;
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define sum(a) ( accumulate ((a).begin(), (a).end(), 0ll))
#define mine(a) (*min_element((a).begin(), (a).end()))
#define maxe(a) (*max_element((a).begin(), (a).end()))
#define mini(a) ( min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a) ( max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin())
template<typename T> vector<T>& operator-- (vector<T> &v){for (auto& i : v) --i; return v;}
template<typename T> vector<T>& operator++ (vector<T> &v){for (auto& i : v) ++i; return v;}
template<typename T> istream& operator>>(istream& is, vector<T> &v){for (auto& i : v) is >> i; return is;}
template<typename T> ostream& operator<<(ostream& os, vector<T> v){for (auto& i : v) os << i << ' '; return os;}
template<typename T, typename U> pair<T,U>& operator-- (pair<T, U> &p){--p.first; --p.second; return p;}
template<typename T, typename U> pair<T,U>& operator++ (pair<T, U> &p){++p.first; ++p.second; return p;}
template<typename T, typename U> istream& operator>>(istream& is, pair<T, U> &p){is >> p.first >> p.second; return is;}
template<typename T, typename U> ostream& operator<<(ostream& os, pair<T, U> p){os << p.first << ' ' << p.second; return os;}
template<typename T, typename U> pair<T,U> operator-(pair<T,U> a, pair<T,U> b){return mp(a.first-b.first, a.second-b.second);}
template<typename T, typename U> pair<T,U> operator+(pair<T,U> a, pair<T,U> b){return mp(a.first+b.first, a.second+b.second);}
template<typename T, typename U> void umin(T& a, U b){if (a > b) a = b;}
template<typename T, typename U> void umax(T& a, U b){if (a < b) a = b;}
#ifdef HOME
#define SHOW_COLORS
#include "/mnt/c/Libs/tools/print.cpp"
#else
#define show(...) void(0)
#define debugf(fun) fun
#define debugv(var) var
#define mclock void(0)
#define shows void(0)
#define debug if (false)
#define OSTREAM(...) ;
#define OSTREAM0(...) ;
#endif
const int basen = 18;
const ll base = 1e18;
// uncomment to multiply large numbers with fft
// basen should be even, at most 8
// with doubles in fft and basen = 8 works up to Big.v.size() = 1e6 (but fails for 1e6 + 5e4)
// #define BIGINT_USE_FFT
// division works in n^2 * log(base), where n = Big.v.size()
using T = ll;
array<ll, 19> pow10;
struct Big {
vector<ll> v;
bool minus = false;
Big() {}
Big(long long k) {
if (k < 0) {
minus = true;
k = -k;
}
while (k) {
v.push_back(k % base);
k /= base;
}
}
Big(string s) {
if (s[0] == '-') {
s.erase(s.begin());
minus = true;
}
reverse(s.begin(), s.end());
while (s.size() % basen != 0)
s.push_back('0');
reverse(s.begin(), s.end());
for (int i = 0; i < s.size(); i += basen)
v.push_back(stoll(s.substr(i, basen)));
reverse(v.begin(), v.end());
norm();
}
void modk(int k) {
auto push_at = [&](int i) {
while (i + 1 < v.size() && v[i] >= base) {
v[i] -= base;
v[i + 1]++;
++i;
}
};
if (k <= basen) {
while (v.size() > 1) {
v.back() %= pow10[k] - 1;
v[v.size() - 2] += v.back() * pow10[basen - k];
v.back() = 0;
push_at(v.size() - 2);
if (v.back() == 0) v.pop_back();
}
norm();
v[0] %= pow10[k] - 1;
return;
}
while (v.size() > k / basen + 1) {
ll x = v.back();
v.back() -= x;
int add_at = ((int)v.size() - 1) * basen % k;
if (add_at % basen == 0) {
v[add_at / basen] += x;
push_at(add_at / basen);
} else {
v[add_at / basen] += pow10[add_at % basen] * (x % pow10[basen - add_at % basen]);
v[add_at / basen + 1] += x / pow10[basen - add_at % basen];
push_at(add_at / basen);
push_at(add_at / basen + 1);
}
if (v.back() == 0) {
v.pop_back();
}
}
while (v.size() == k / basen + 1 && v.back() >= pow10[k - (v.size() - 1) * basen]) {
ll x = v.back() / pow10[k - (v.size() - 1) * basen];
v.back() %= pow10[k - (v.size() - 1) * basen];
v[0] += x;
push_at(0);
}
norm();
}
void mulint(int x) {
for (ll& u : v)
u *= x;
v.pb(0);
for (int i = 0; i + 1 < v.size(); ++i) {
v[i + 1] += v[i] / base;
v[i] %= base;
}
norm();
}
int modint(int x) {
ll basemod = base % x;
ll ans = 0;
ll pw = 1;
for (ll u : v) {
ans = (ans + u % x * pw) % x;
pw = (pw * basemod) % x;
}
return ans;
}
Big &operator += (const Big &other) {
if (minus == other.minus) {
_add_(v, other.v);
} else {
if (_comp_(other.v, v)) {
_sub_(v, other.v);
} else {
_sub2_(v, other.v);
minus ^= 1;
}
}
norm();
return *this;
}
Big operator + (const Big &other) const {
auto res = *this;
return res += other;
}
Big operator - () const {
Big res = *this;
if (!v.empty()) res.minus ^= 1;
return res;
}
Big &operator -= (const Big &other) {
return *this += -other;
}
Big operator - (const Big &other) const {
auto res = *this;
return res -= other;
}
Big operator * (const Big &other) const {
if (v.empty() || other.v.empty()) return 0;
Big res;
res.v = _mult_(v, other.v);
res.minus = minus ^ other.minus;
return res;
}
Big &operator *= (const Big &other) {
return *this = *this * other;
}
Big operator / (const Big &other) const {
Big res;
res.v = _div_(v, other.v).first;
res.minus = minus ^ other.minus;
res.norm();
return res;
}
Big &operator /= (const Big &other) {
return *this = *this / other;
}
Big operator % (const Big &other) const {
Big res;
res.v = _div_(v, other.v).second;
res.minus = minus ^ other.minus;
res.norm();
return res;
}
Big &operator %= (const Big &other) {
return *this = *this % other;
}
int operator % (int m) const {
long long p = 1;
long long res = 0;
for (int k : v) {
res += k * p % m;
p = p * base % m;
}
return res % m;
}
void norm() {
while (!v.empty() && v.back() == 0)
v.pop_back();
if (v.empty()) {
minus = false;
v.pb(0);
}
}
bool operator < (const Big &other) const {
if (minus != other.minus) return minus;
if (minus) return _comp_(other.v, v);
else return _comp_(v, other.v);
}
bool operator > (const Big &other) const {
return other < *this;
}
bool operator <= (const Big &other) const {
return !(other < *this);
}
bool operator >= (const Big &other) const {
return !(*this < other);
}
bool operator == (const Big &other) const {
return minus == other.minus && v == other.v;
}
bool operator != (const Big &other) const {
return !(*this == other);
}
private:
static void _sub_(vector<T> &a, const vector<T> &b) {
a.resize(max(a.size(), b.size()) + 1, 0);
for (int i = 0; i < b.size(); ++i)
a[i] -= b[i];
for (int i = 0; i + 1 < b.size() || a[i] < 0; ++i) {
if (a[i] < 0) {
a[i] += base;
--a[i + 1];
}
}
assert(a.back() >= 0);
while (!a.empty() && a.back() == 0)
a.pop_back();
}
static void _sub2_(vector<T> &a, const vector<T> &b) {
a.resize(max(a.size(), b.size()) + 1, 0);
for (int i = 0; i < a.size(); ++i)
a[i] = (i < b.size() ? b[i] : 0) - a[i];
for (int i = 0; i + 1 < a.size(); ++i) {
if (a[i] < 0) {
a[i] += base;
--a[i + 1];
}
}
assert(a.back() >= 0);
while (!a.empty() && a.back() == 0)
a.pop_back();
}
static void _add_(vector<T> &a, const vector<T> &b) {
a.resize(max(a.size(), b.size()) + 1, 0);
for (int i = 0; i < b.size(); ++i)
a[i] += b[i];
for (int i = 0; i + 1 < b.size() || a[i] >= base; ++i) {
if (a[i] >= base) {
a[i] -= base;
++a[i + 1];
}
}
while (!a.empty() && a.back() == 0)
a.pop_back();
}
static bool _comp_(const vector<T> &a, const vector<T> &b) {
if (a.size() != b.size())
return a.size() < b.size();
for (int i = (int)a.size() - 1; i >= 0; --i)
if (a[i] != b[i])
return a[i] < b[i];
return false;
}
static vector<T> _mult_(const vector<T> &a, const vector<T> &b) {
#ifdef BIGINT_USE_FFT
// tested on a.v.size() = 1e6, b.v.size() = C, fft is better on C > ~500 : https://ideone.com/kSYLd8
// if a.v.size() = b.v.size() = C, it's 380 : https://ideone.com/MJTo1Y
if (min(a.size(), b.size()) > 380) {
return _fft_mult_(a, b);
}
#endif
return _slow_mult_(a, b);
}
static vector<T> _slow_mult_(const vector<T> &a, const vector<T> &b) {
vector<long long> tmp(a.size() + b.size() + 1, 0);
for (int i = 0; i < a.size(); ++i) {
for (int j = 0; j < b.size(); ++j) {
long long prod = 1ll * a[i] * b[j];
long long div = prod / base;
tmp[i + j] += prod - base * div;
tmp[i + j + 1] += div;
}
}
for (int i = 0; i + 1 < tmp.size(); ++i) {
long long div = tmp[i] / base;
tmp[i + 1] += div;
tmp[i] -= div * base;
}
while (!tmp.empty() && tmp.back() == 0)
tmp.pop_back();
return vector<T>(tmp.begin(), tmp.end());
}
#ifdef BIGINT_USE_FFT
static vector<int> _fft_mult_(const vector<int> &a, const vector<int> &b) {
vector<int> ta(a.size() * 2), tb(b.size() * 2);
static_assert(basen % 2 == 0, "basen has to be even");
const static int M = pow(10, basen / 2);
for (int i = 0; i < a.size(); ++i) {
ta[i * 2] = a[i] % M;
ta[i * 2 + 1] = a[i] / M;
}
for (int i = 0; i < b.size(); ++i) {
tb[i * 2] = b[i] % M;
tb[i * 2 + 1] = b[i] / M;
}
auto tc = fft::multiply(ta, tb);
tc.resize(tc.size() / 2 * 2 + 10, 0);
for (int i = 0; i + 1 < tc.size(); ++i) {
tc[i + 1] += tc[i] / M;
tc[i] %= M;
}
vector<int> res(tc.size() / 2);
for (int i = 0; i < res.size(); ++i)
res[i] = tc[i * 2] + tc[i * 2 + 1] * M;
while (!res.empty() && res.back() == 0)
res.pop_back();
return res;
}
#endif
static pair<vector<T>, vector<T>> _div_(vector<T> a, vector<T> b) {
if (a.size() < b.size()) {
return {{}, a};
}
vector<T> res;
vector<T> c, c2;
for (int i = (int)a.size() - b.size(); i >= 0; --i) {
c.resize(b.size() + i);
for (int j = 0; j < b.size(); ++j) {
c[i + j] = b[j];
}
T L = 0, R = base;
while (R - L > 1) {
T C = (L + R) / 2;
c2 = _mult_(c, {C});
if (_comp_(a, c2)) {
R = C;
} else {
L = C;
}
}
c = _mult_(c, {L});
_sub_(a, c);
res.push_back(L);
}
reverse(res.begin(), res.end());
return {res, a};
}
};
string to_string(const Big &b) {
if (b.v.empty()) return "0";
string res;
for (int i = (int)b.v.size() - 1; i >= 0; --i) {
string t = to_string(b.v[i]);
if (!res.empty())
t = string(basen - t.size(), '0') + t;
res += t;
}
if (b.minus)
res.insert(res.begin(), '-');
return res;
}
ostream &operator << (ostream &o, const Big &b) {
return o << to_string(b);
};
istream &operator >> (istream &i, Big &b) {
string s;
i >> s;
b = Big(s);
return i;
}
Big gcd(Big a, Big b) {
while (b != 0) {
a %= b;
swap(a, b);
}
return a;
}
const array<int, 10> primes = {1038315533, 1070784149, 1152186131, 1226633593, 1383110039, 1811889307, 1863415583, 1871039267, 1893815057, 1925743987};
struct hash_my {
size_t operator()(int x) const {
x = ((x >> 16) ^ x) * 0x119de1f3;
x = ((x >> 16) ^ x) * 0x119de1f3;
x = (x >> 16) ^ x;
return x;
}
size_t operator()(long long x) const {
x = (x ^ (x >> 30)) * (0xbf58476d1ce4e5b9ll);
x = (x ^ (x >> 27)) * (0x94d049bb133111ebll);
x = x ^ (x >> 31);
return x;
}
template<typename T, typename U>
size_t operator()(const pair<T, U> &p) const {
long long h1 = (*this)(p.first);
long long h2 = (*this)(p.second);
return (*this)(h1 ^ (h2 << 32) ^ (h2 >> 32));
}
};
auto start_time = clock();
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
pow10[0] = 1;
for (int i = 1; i < pow10.size(); ++i)
pow10[i] = pow10[i - 1] * 10;
// int t;
// cin >> t;
// while (t--) {
// Big a;
// int k;
// cin >> a >> k;
// Big b;
// cin >> b;
// a.modk(k);
// if (a != b) {
// cout << a << endl;
// cout << b << endl;
// show(a.v);
// show(b.v);
// assert(false);
// }
// cerr << "ok" << endl;
// }
// map<array<int, 10>, int> cnts;
int n, k;
cin >> n >> k;
vector<Big> v;
for (int i = 0; i < n; ++i) {
string s;
cin >> s;
Big x(s);
x.modk(k);
v.pb(std::move(x));
}
show(v);
Big b;
Big mk(string(k, '9'));
show(mk);
Big b2;
using namespace __gnu_pbds;
vector<gp_hash_table<ll, int>> to;
vector<int> res;
to.eb();
res.pb(0);
auto add_vec = [&](const vector<ll>& v) {
int cur = 0;
for (auto x : v) {
auto it = to[cur].find(x);
if (it != to[cur].end()) {
cur = it->second;
} else {
int ccur = to.size();
to.eb();
res.pb(0);
to[cur][x] = ccur;
cur = ccur;
}
}
res[cur]++;
};
auto ask_vec = [&](const vector<ll>& v) {
int cur = 0;
for (auto x : v) {
auto it = to[cur].find(x);
if (it != to[cur].end()) {
cur = it->second;
} else {
return 0;
}
}
return res[cur];
};
cerr << (double)(clock() - start_time) / CLOCKS_PER_SEC << endl;
int ans = 0;
for (int i = 0; i < n; ++i) {
// {
// array<int, 10> rems;
// for (int pi = 0; pi < 10; ++pi) {
// rems[pi] = v[i].modint(primes[pi]);
// }
// cnts[rems]++;
// }
add_vec(v[i].v);
for (int j = i; j < n; ++j) {
b.v.assign(v[i].v.begin(), v[i].v.end());
b.minus = false;
b += v[j];
b.modk(k);
b2.v.assign(mk.v.begin(), mk.v.end());
b2.minus = false;
b2 -= b;
b2.modk(k);
b2.norm();
ans += ask_vec(b2.v);
// {
// array<int, 10> rems;
// for (int pi = 0; pi < 10; ++pi) {
// rems[pi] = v[i].modint(primes[pi]);
// }
// cnts[rems]++;
// }
}
}
cout << ans << '\n';
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3732kb
input:
4 1 0 1 10 17
output:
3
result:
ok 1 number(s): "3"
Test #2:
score: 0
Accepted
time: 38ms
memory: 15188kb
input:
500 859 7118711592236878297922359501613604144948355616986970837340677671376753603836852811886591300370143151943368529129749813118476151865844255212534355441611481420938483178075143062691345257288242460282715389758789648541099090735875617822348551942134616963557723055980260082230902505269975518146286...
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: -100
Time Limit Exceeded
input:
500 17336 11871159223687829792235950161360414494835561698697083734067767137675360383685281188659130037014315194336852912974981311847615186584425521253435544161148142093848317807514306269134525728824246028271538975878964854109909073587561782234855194213461696355772305598026008223090250526997551814628...