QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#849562 | #9922. Mah-jong | ucup-team3161# | TL | 0ms | 17868kb | C++14 | 4.1kb | 2025-01-09 16:18:06 | 2025-01-09 16:18:12 |
Judging History
answer
// what is matter? never mind.
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,sse4,popcnt,abm,mmx,avx,avx2")
#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
#define ll long long
//#define ull unsigned long long
//#define int long long
#define SZ(x) ((int)((x).size()))
#define ALL(x) (x).begin(),(x).end()
using namespace std;
inline int read()
{
char c=getchar();int x=0;bool f=0;
for(;!isdigit(c);c=getchar())f^=!(c^45);
for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
return f?-x:x;
}
#define mod 998244353
struct modint{
unsigned int x;
modint(int o=0){x=o;}
modint &operator = (int o){return x=o,*this;}
modint &operator +=(modint o){return x=x+o.x>=mod?x+o.x-mod:x+o.x,*this;}
modint &operator -=(modint o){return x=x<o.x?x-o.x+mod:x-o.x,*this;}
modint &operator *=(modint o){return x=1ll*x*o.x%mod,*this;}
modint &operator ^=(int b){
modint a=*this,c=1;
for(;b;b>>=1,a*=a)if(b&1)c*=a;
return x=c.x,*this;
}
modint &operator /=(modint o){return *this *=o^=mod-2;}
friend modint operator +(modint a,modint b){return a+=b;}
friend modint operator -(modint a,modint b){return a-=b;}
friend modint operator *(modint a,modint b){return a*=b;}
friend modint operator /(modint a,modint b){return a/=b;}
friend modint operator ^(modint a,int b){return a^=b;}
friend bool operator ==(modint a,modint b){return a.x==b.x;}
friend bool operator !=(modint a,modint b){return a.x!=b.x;}
bool operator ! () {return !x;}
modint operator - () {return x?mod-x:0;}
bool operator <(const modint&b)const{return x<b.x;}
};
inline modint qpow(modint x,int y){return x^y;}
vector<modint> fac,ifac,iv;
inline void initC(int n)
{
if(iv.empty())fac=ifac=iv=vector<modint>(2,1);
int m=iv.size(); ++n;
if(m>=n)return;
iv.resize(n),fac.resize(n),ifac.resize(n);
For(i,m,n-1){
iv[i]=iv[mod%i]*(mod-mod/i);
fac[i]=fac[i-1]*i,ifac[i]=ifac[i-1]*iv[i];
}
}
inline modint C(int n,int m){
if(m<0||n<m)return 0;
return initC(n),fac[n]*ifac[m]*ifac[n-m];
}
inline modint sign(int n){return (n&1)?(mod-1):(1);}
#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;
#define maxn 1000005
#define inf 0x3f3f3f3f
int n,a[maxn],cnt[maxn][9];
int pw[233];
deque<int>q[10005];
int now[9];
vector<vi> qwq[10050];
int get(vi o){
int sta=0;
For(j,1,8) sta=sta*3+(o[j-1]%3);
return sta;
}
vector<vi>qq;
void chk(){
int sta=0;
For(j,1,8) sta=sta*3+(now[j]%3);
vi os;
For(j,1,8) os.pb(now[j]);
qwq[sta].pb(os);
qq.pb(os);
}
bool isok(vi os){
// cout<<"isok: "; for(int x:os) cout<<x<<" "; cout<<"\n";
int sta=0;
For(j,1,8) sta=sta*3+(os[j-1]%3);
for(auto it:qwq[sta]){
bool ok=1;
For(j,0,7) {
if(os[j]>=it[j]); else ok=0;
if(!ok) break;
}
if(ok) return 1;
}
return 0;
}
void dfs(int u){
if(u==7){
chk();
return;
}
For(i,0,2){
For(j,0,2)now[u+j]+=i;
dfs(u+1);
For(j,0,2)now[u+j]-=i;
}
}
void prew(){
dfs(1);
}
int lst[maxn];
ll res[maxn];
void work()
{
n=read();
For(i,1,n)a[i]=read();
For(i,1,n){
For(j,1,8)cnt[i][j]=cnt[i-1][j];
cnt[i][a[i]]++;
}
For(j,0,pw[8]) q[j].clear(),lst[j]=-1,res[j]=0;
ll sum=0;
For(i,0,n){
int sta=0;
For(j,1,8)sta=sta*3+(cnt[i][j]%3);
// cout<<"sta "<<sta<<"\n";
for(auto vs:qq){
int j=0;
For(k,0,7){
int c=(cnt[i][k+1]-vs[k]+9)%3;
j=j*3+c;
}
if(lst[j]!=-1){
int l=lst[j];
vi is;
For(k,1,8) is.pb(cnt[i][k]-cnt[l][k]);
if(!isok(is)) continue;
}
while(q[j].size()){
int l=q[j].front();
vi is;
For(k,1,8) is.pb(cnt[i][k]-cnt[l][k]);
// cout<<"IS\n";
if(isok(is)) lst[j]=l,q[j].pop_front(),++res[j];
else break;
}
sum+=res[j];
}
q[sta].pb(i);
}
cout<<sum<<"\n";
}
signed main()
{
pw[0]=1;
For(i,1,8) pw[i]=pw[i-1]*3;
// cout<<pw[8]<<endl;
prew();
int T=read();
while(T--)work();
return 0;
}
/*
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 17868kb
input:
5 4 1 1 1 1 6 1 2 3 1 2 3 7 6 5 8 7 6 3 2 8 1 2 1 2 1 2 1 3 9 2 2 4 4 1 1 1 3 3
output:
2 5 1 3 2
result:
ok 5 number(s): "2 5 1 3 2"
Test #2:
score: -100
Time Limit Exceeded
input:
100 992 8 1 8 1 2 3 6 6 1 3 1 8 7 7 4 7 7 1 6 6 4 8 3 7 3 5 1 4 4 7 5 7 5 7 4 3 7 5 2 8 7 1 6 3 6 2 4 3 2 3 1 6 3 1 3 2 6 6 7 4 6 1 1 4 6 4 7 7 8 5 6 4 1 5 4 8 2 4 4 2 1 3 5 7 6 8 3 7 6 6 5 6 4 2 5 4 3 7 3 5 5 3 3 2 7 8 2 7 2 4 4 3 4 1 1 3 5 5 4 6 3 3 3 2 6 1 2 6 4 8 8 6 6 8 7 3 1 1 8 8 7 2 5 6 3 5 ...