結果

問題 No.2254 Reverse Only
ユーザー 沙耶花沙耶花
提出日時 2023-03-24 21:39:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 164 ms / 2,000 ms
コード長 2,697 bytes
コンパイル時間 4,422 ms
コンパイル使用メモリ 265,748 KB
実行使用メモリ 7,976 KB
最終ジャッジ日時 2023-10-18 20:52:57
合計ジャッジ時間 10,322 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 151 ms
6,536 KB
testcase_09 AC 164 ms
7,976 KB
testcase_10 AC 126 ms
4,952 KB
testcase_11 AC 152 ms
6,536 KB
testcase_12 AC 152 ms
6,536 KB
testcase_13 AC 119 ms
4,952 KB
testcase_14 AC 150 ms
6,536 KB
testcase_15 AC 152 ms
6,536 KB
testcase_16 AC 159 ms
7,976 KB
testcase_17 AC 159 ms
7,976 KB
testcase_18 AC 150 ms
6,536 KB
testcase_19 AC 143 ms
6,536 KB
testcase_20 AC 155 ms
7,976 KB
testcase_21 AC 155 ms
7,976 KB
testcase_22 AC 78 ms
7,976 KB
testcase_23 AC 142 ms
7,976 KB
testcase_24 AC 126 ms
7,976 KB
testcase_25 AC 136 ms
7,976 KB
testcase_26 AC 137 ms
7,976 KB
testcase_27 AC 6 ms
4,348 KB
testcase_28 AC 33 ms
4,348 KB
testcase_29 AC 71 ms
4,952 KB
testcase_30 AC 61 ms
4,688 KB
testcase_31 AC 36 ms
4,348 KB
testcase_32 AC 40 ms
4,348 KB
testcase_33 AC 19 ms
4,348 KB
testcase_34 AC 90 ms
5,216 KB
testcase_35 AC 58 ms
4,688 KB
testcase_36 AC 128 ms
6,008 KB
testcase_37 AC 134 ms
6,272 KB
testcase_38 AC 28 ms
4,348 KB
testcase_39 AC 119 ms
5,744 KB
testcase_40 AC 6 ms
4,348 KB
testcase_41 AC 53 ms
4,424 KB
testcase_42 AC 30 ms
4,348 KB
testcase_43 AC 96 ms
5,480 KB
testcase_44 AC 142 ms
6,272 KB
testcase_45 AC 88 ms
5,216 KB
testcase_46 AC 5 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using namespace atcoder;
using mint = modint998244353;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf32 1000000001
#define Inf64 4000000000000000001

struct rolling_hash{

	long long t_hash;
	static inline vector<long long> power;
	static const long long MOD = (1LL<<61)-1;
	static const long long b = 123456;
	int sz;
	
	rolling_hash(){		
		sz = 0;
		t_hash = 0;
	}
	
	rolling_hash(long long c){
		sz = 1;
		t_hash = b*c;
	}

	long long mul(__int128 x,__int128 y){
		__int128 t = x*y;
		t = (t>>61) + (t&MOD);
		
		if(t>=MOD)t -= MOD;
		return t;
	}
	
	long long get_pow(int sz){
		if(power.size()>sz)return power[sz];
		
		while(power.size()<=sz){
			if(power.size()==0)power.push_back(1);
			else power.push_back(mul(power.back(),b));
		}
		return power.back();
		
	}
	
	rolling_hash &operator+=(const rolling_hash &another){
		
		(*this).t_hash = mul((*this).t_hash,get_pow(another.sz));
		(*this).t_hash += another.t_hash;
		if((*this).t_hash>=MOD)(*this).t_hash -= MOD;
			
		(*this).sz += another.sz;
		
		return (*this);
	}
	
	rolling_hash operator+(const rolling_hash &another)const{
		return (rolling_hash(*this)+=another);
	}
	
	rolling_hash &operator-=(const rolling_hash &another){

		(*this).t_hash += MOD - mul(another.t_hash,get_pow((*this).sz-another.sz));
		if((*this).t_hash>=MOD)(*this).t_hash -= MOD;
			
		(*this).sz -= another.sz;

		return (*this);
	}
	
	rolling_hash operator-(const rolling_hash &another)const{
		return (rolling_hash(*this)-=another);
	}
	
	bool operator<(const rolling_hash &another)const{
		if((*this).t_hash!=another.t_hash)return (*this).t_hash<another.t_hash;
		return (*this).sz<another.sz;
	}
	
	bool operator==(const rolling_hash &another)const{
		return ((*this).t_hash==another.t_hash && (*this).sz==another.sz);
	}

	
};


int main(){
	
	int N,K;
	cin>>N>>K;
	
	vector<int> a(N),b(N);
	rep(i,N)cin>>a[i];
	rep(i,N)cin>>b[i];
	
	if(a==b){
		cout<<"Yes"<<endl;
		return 0;
	}
	{
		auto c = a,d = b;
		sort(c.begin(),c.end());
		sort(d.begin(),d.end());
		if(c!=d){
			cout<<"No"<<endl;
			return 0;
		}
	}
	if(K>N){
		cout<<"No"<<endl;
		return 0;
	}
	
	if(K<=N-2){
		cout<<"Yes"<<endl;
		return 0;
	}
	if(K==N){
		reverse(a.begin(),a.end());
		if(a==b){
			cout<<"Yes"<<endl;
		}
		else{
			cout<<"No"<<endl;
			return 0;
		}
		return 0;
	}
	
	rep(_,2){
		rolling_hash x,y;
		rep(i,N)x = x+rolling_hash(a[i]);
		rep(i,N)y = y+rolling_hash(b[i]);
		rep(i,N){
			y = y - rolling_hash(b[i]);
			y = y+rolling_hash(b[i]);
			if(x==y){
				cout<<"Yes"<<endl;
				return 0;
			}
		}
		reverse(b.begin(),b.end());
	}
	cout<<"No"<<endl;
	
	return 0;
}
0