結果

問題 No.2254 Reverse Only
ユーザー umezoumezo
提出日時 2023-03-24 23:43:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 282 ms / 2,000 ms
コード長 1,681 bytes
コンパイル時間 2,223 ms
コンパイル使用メモリ 209,500 KB
実行使用メモリ 28,136 KB
最終ジャッジ日時 2023-10-18 21:41:48
合計ジャッジ時間 10,458 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 1 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 260 ms
25,036 KB
testcase_09 AC 263 ms
28,136 KB
testcase_10 AC 152 ms
25,036 KB
testcase_11 AC 267 ms
25,036 KB
testcase_12 AC 219 ms
25,036 KB
testcase_13 AC 227 ms
25,036 KB
testcase_14 AC 231 ms
25,036 KB
testcase_15 AC 266 ms
25,036 KB
testcase_16 AC 282 ms
28,136 KB
testcase_17 AC 282 ms
28,136 KB
testcase_18 AC 250 ms
25,036 KB
testcase_19 AC 215 ms
25,036 KB
testcase_20 AC 231 ms
28,136 KB
testcase_21 AC 230 ms
28,136 KB
testcase_22 AC 158 ms
28,136 KB
testcase_23 AC 171 ms
28,136 KB
testcase_24 AC 177 ms
28,136 KB
testcase_25 AC 181 ms
28,136 KB
testcase_26 AC 177 ms
28,136 KB
testcase_27 AC 5 ms
4,348 KB
testcase_28 AC 37 ms
7,876 KB
testcase_29 AC 81 ms
13,156 KB
testcase_30 AC 83 ms
11,836 KB
testcase_31 AC 35 ms
8,140 KB
testcase_32 AC 42 ms
8,932 KB
testcase_33 AC 17 ms
5,796 KB
testcase_34 AC 114 ms
16,060 KB
testcase_35 AC 64 ms
11,572 KB
testcase_36 AC 207 ms
21,604 KB
testcase_37 AC 216 ms
22,396 KB
testcase_38 AC 28 ms
7,084 KB
testcase_39 AC 189 ms
20,284 KB
testcase_40 AC 6 ms
4,348 KB
testcase_41 AC 59 ms
10,780 KB
testcase_42 AC 30 ms
7,348 KB
testcase_43 AC 126 ms
17,116 KB
testcase_44 AC 224 ms
23,716 KB
testcase_45 AC 121 ms
15,796 KB
testcase_46 AC 4 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;
 
#include<bits/stdc++.h>
using namespace std;

const ll MOD1=1e9+7;
const ll MOD2=1e9+9;
const ll B1=1007;
const ll B2=1009;

bool rolling_hush(vector<ll> &a, vector<ll> &b){
  int al=a.size(),bl=b.size();
  if(al>bl) return false;
  
  ll t1=1,t2=1;
  rep(i,al){
    t1*=B1;
    t2*=B2;
    t1%=MOD1;
    t2%=MOD2;
  }
  
  ll ah=0,bh=0,ch=0,dh=0;
  rep(i,al){
    ah=(ah*B1%MOD1+a[i])%MOD1;
    bh=(bh*B1%MOD1+b[i])%MOD1;
    ch=(ch*B2%MOD2+a[i])%MOD2;
    dh=(dh*B2%MOD2+b[i])%MOD2;
  }
  
  for(int i=0;i+al<=bl;i++){
    if(ah==bh && ch==dh) return true;
    if(i+al<bl){
      bh=(bh*B1%MOD1+b[i+al]-b[i]*t1%MOD1+MOD1)%MOD1;
      dh=(dh*B2%MOD2+b[i+al]-b[i]*t2%MOD2+MOD2)%MOD2;
    }
  }
  return false;
}

int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
 
  int n,k;
  cin>>n>>k;
  vector<ll> A(n),B(n);
  multiset<ll> c,d;
  bool b=true;
  rep(i,n){
    cin>>A[i];
    c.insert(A[i]);
  }
  rep(i,n){
    cin>>B[i];
    if(B[i]!=A[i]) b=false;
    d.insert(B[i]);
  }
  
  if(b){
    cout<<"Yes"<<endl;
    return 0;
  }
  if(k>n || c!=d){
    cout<<"No"<<endl;
    return 0;
  }
  if(k==n){
    reverse(ALL(B));
    b=true;
    rep(i,n){
      if(A[i]!=B[i]) b=false;
      if(b) cout<<"Yes"<<endl;
      else cout<<"No"<<endl;
      return 0;
    }
  }
  if(k<n-1){
    cout<<"Yes"<<endl;
    return 0;
  }
  
  vector<ll> AA(2*n);
  rep(i,n){
    AA[i]=A[i];
    AA[n+i]=A[i];
  }
  
  if(rolling_hush(B,AA)) cout<<"Yes"<<endl;
  else{
    reverse(ALL(B));
    if(rolling_hush(B,AA)) cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
  }

  return 0;
}
0