結果

問題 No.2254 Reverse Only
ユーザー umezoumezo
提出日時 2023-03-24 23:37:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 290 ms / 2,000 ms
コード長 1,566 bytes
コンパイル時間 2,118 ms
コンパイル使用メモリ 209,180 KB
実行使用メモリ 28,220 KB
最終ジャッジ日時 2023-10-18 21:40:33
合計ジャッジ時間 10,281 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 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 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 284 ms
25,120 KB
testcase_09 AC 290 ms
28,220 KB
testcase_10 AC 151 ms
25,120 KB
testcase_11 AC 289 ms
25,120 KB
testcase_12 AC 273 ms
25,120 KB
testcase_13 AC 255 ms
25,120 KB
testcase_14 AC 259 ms
25,120 KB
testcase_15 AC 280 ms
25,120 KB
testcase_16 AC 281 ms
28,220 KB
testcase_17 AC 281 ms
28,220 KB
testcase_18 AC 255 ms
25,120 KB
testcase_19 AC 211 ms
25,120 KB
testcase_20 AC 214 ms
28,220 KB
testcase_21 AC 219 ms
28,220 KB
testcase_22 AC 147 ms
28,220 KB
testcase_23 AC 161 ms
28,220 KB
testcase_24 AC 175 ms
28,220 KB
testcase_25 AC 178 ms
28,220 KB
testcase_26 AC 175 ms
28,220 KB
testcase_27 AC 5 ms
4,348 KB
testcase_28 AC 36 ms
7,960 KB
testcase_29 AC 94 ms
13,240 KB
testcase_30 AC 78 ms
11,920 KB
testcase_31 AC 37 ms
8,224 KB
testcase_32 AC 42 ms
9,016 KB
testcase_33 AC 18 ms
5,880 KB
testcase_34 AC 125 ms
16,144 KB
testcase_35 AC 67 ms
11,656 KB
testcase_36 AC 223 ms
21,688 KB
testcase_37 AC 243 ms
22,480 KB
testcase_38 AC 28 ms
7,168 KB
testcase_39 AC 205 ms
20,368 KB
testcase_40 AC 5 ms
4,348 KB
testcase_41 AC 60 ms
10,864 KB
testcase_42 AC 30 ms
7,432 KB
testcase_43 AC 142 ms
17,200 KB
testcase_44 AC 236 ms
23,800 KB
testcase_45 AC 129 ms
15,880 KB
testcase_46 AC 5 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;
typedef unsigned long long ull;
 
#include<bits/stdc++.h>
using namespace std;

const ull B1=1000000007;
const ull B2=100000007;

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