QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#93917#4422. Two Permutationskatoli#AC ✓1068ms50108kbC++142.9kb2023-04-03 22:01:492023-04-03 22:01:51

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-03 22:01:51]
  • 评测
  • 测评结果:AC
  • 用时:1068ms
  • 内存:50108kb
  • [2023-04-03 22:01:49]
  • 提交

answer

#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
using ll=long long;

const int MOD=998244353;
struct modint{
	int x;
	modint(ll x=0):x(x%MOD){}
	int get(){return x;}
	modint operator+(const modint &p)const{return modint(x+p.x);}
	modint operator-(const modint &p)const{return modint(x-p.x+MOD);}
	modint operator*(const modint &p)const{return modint((ll)x*p.x);}
	modint operator/(const modint &p)const{return *this*p.inverse();}
	void operator+=(const modint &p){x+=p.x;if(x>=MOD)x-=MOD;}
	void operator-=(const modint &p){x-=p.x;if(x<0)x+=MOD;}
	void operator*=(const modint &p){x=(ll)x*p.x%MOD;}
	void operator/=(const modint &p){*this=*this/p;}
	modint inverse()const{
		int a=x,b=MOD,x=1,y=0;
		while(b){
		int t=a/b;
		a-=t*b;swap(a,b);
		x-=t*y;swap(x,y);
		}
		if(x<0)x+=MOD;
		return x;
	}
};

const int N=6e5+5;
struct strHash{
  const int p1=13331,p2=131;
  const int M1=1e9+7,M2=998244353;
  ll hash1[N],hash2[N];
  ll pw1[N]={1},pw2[N]={1};
  void inpw(){
    for(int i=1;i<N;++i){
      pw1[i]=(pw1[i-1]*p1)%M1;
      pw2[i]=(pw2[i-1]*p2)%M2;
    }
  }
  void init(string &s){
    int n=s.size();
    for(int i=0;i<n;++i){
      hash1[i+1]=(hash1[i]*p1%M1+s[i]-'0'+1)%M1;
      hash2[i+1]=(hash2[i]*p2%M2+s[i]-'0'+1)%M2;
    }
  }
  pair<ll,ll> gao(int l,int r){
    return {(hash1[r+1]+M1-hash1[l]*pw1[r-l+1]%M1)%M1,(hash2[r+1]+M2-hash2[l]*pw2[r-l+1]%M2)%M2};
  }
}hq,hr;
int n;
int check(int ql,int qr,int rl,int rr){
	if(rl>rr)return 1;
	if(ql<0||qr>=n||rl<0||rr>=2*n)return 0;
	return hq.gao(ql,qr)==hr.gao(rl,rr);
}
signed main() {
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	hq.inpw(),hr.inpw();
	int T;cin>>T;
	for(;T;T--){
		cin>>n;
		vector<int>P(n),Q(n),R(2*n);
		for(auto &it:P)cin>>it;
		for(auto &it:Q)cin>>it;
		for(auto &it:R)cin>>it;
		string s;
		for(auto it:Q)s+=it+'0';
		hq.init(s);
		s="";
		for(auto it:R)s+=it+'0';
		hr.init(s);
		modint ans;
		vector<vector<int>>occurR(2,vector<int>(n+1,-1));
		vector<vector<modint>>dp(2,vector<modint>(n,(modint)0));
		int ind;
		for(int i=0;i<2*n;++i){
			int x=R[i];
			if(occurR[0][x]==-1)occurR[0][x]=i;
			else if(occurR[1][x]==-1)occurR[1][x]=i;
			else{goto end;}
		}
		ind=occurR[0][P[0]]-1;
		if(ind<0||(ind<n&&hq.gao(0,ind)==hr.gao(0,ind)))dp[0][0]=1;
		ind=occurR[1][P[0]]-1;
		if(ind<0||(ind<n&&hq.gao(0,ind)==hr.gao(0,ind)))dp[1][0]=1;
		for(int i=1;i<n;++i){
			for(int j=0;j<2;++j){
				int r=occurR[j][P[i]];
				for(int k=0;k<2;++k){
					int l=occurR[k][P[i-1]];
					if(l>=r)continue;
					if(check(l-i+1,r-i-1,l+1,r-1))
						dp[j][i]+=dp[k][i-1];
				}
			}
		}
		for(int i=0;i<2;++i){
			int l=occurR[i][P[n-1]],r=2*n-1;
			if(check(l-n+1,n-1,l+1,r)){
				ans+=dp[i][n-1];
			}
		}
		end:
		cout<<ans.x<<endl;
	}
	return 0;
}
// init?
// var->0?
// infinite dfs?
// out of bound?
// max_element / min_element?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1068ms
memory: 50108kb

input:

282
1000
434 697 735 906 614 835 227 800 116 98 776 601 110 371 262 452 608 368 719 717 241 572 203 410 440 749 604 457 516 637 488 691 841 493 418 307 745 499 833 789 819 179 357 288 129 954 29 391 80 389 771 613 653 747 928 570 518 1000 547 587 727 778 669 554 426 899 256 681 515 532 409 677 533 3...

output:

4
2
16
2
2
2
1
1
8
8
4
4
2
2
2
1
2
2
2
2
2
1
1
2
8
1
2
2
4
2
2
1
2
2
8
2
2
2
4
4
1
4
1
4
4
1
1
8
4
0
4
4
1
2
4
2
2
2
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0...

result:

ok 282 lines