結果
| 問題 |
No.2254 Reverse Only
|
| コンテスト | |
| ユーザー |
noya2
|
| 提出日時 | 2023-03-10 02:15:45 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 188 ms / 2,000 ms |
| コード長 | 3,149 bytes |
| コンパイル時間 | 1,961 ms |
| コンパイル使用メモリ | 205,372 KB |
| 最終ジャッジ日時 | 2025-02-11 07:15:28 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 47 |
ソースコード
#include<bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < int(n); ++i)
#define all(v) v.begin(),v.end()
using namespace std;
using ull = unsigned long long;
void yes(){ printf("Yes\n"); }
void no(){ printf("No\n"); }
struct RollingHash {
RollingHash(const vector<int> &v = {}){ build(v);}
ull get(int l, int r){
assert(0 <= l && l <= r && r <= n);
return cal_mod(inner_hash[r] + POSITIVISER - mul_mod(inner_hash[l], pow_base[r-l]));
}
static ull get_hash(const vector<int> &v){
int len = v.size();
set_hash();
extend_pow_base(len);
ull res = 0;
for (int i = 0; i < len; i++) res = cal_mod(mul_mod(res,BASE) + v[i]);
return res;
}
private:
static constexpr ull MASK30 = (1ULL << 30) - 1;
static constexpr ull MASK31 = (1ULL << 31) - 1;
static constexpr ull MASK61 = (1ULL << 61) - 1;
static constexpr ull MOD = (1ULL << 61) - 1;
static constexpr ull POSITIVISER = MOD * 4;
static ull BASE;
static vector<ull> pow_base;
static ull mul_mod(ull a, ull b){
ull au = a >> 31, ad = a & MASK31;
ull bu = b >> 31, bd = b & MASK31;
ull mid = ad * bu + au * bd;
ull midu = mid >> 30, midd = mid & MASK30;
return (au * bu * 2 + midu + (midd << 31) + ad * bd);
}
static ull cal_mod(ull x){
ull xu = x >> 61;
ull xd = x & MASK61;
ull res = xu + xd;
if (res >= MOD) res -= MOD;
return res;
}
static void set_hash(){
if (BASE == 0) BASE = (1UL<<31) + (random_device()() & MASK31);
}
static void extend_pow_base(int len){
int nlen = pow_base.size();
if (nlen > len) return ;
pow_base.resize(len+1);
for (int i = nlen; i <= len; i++){
pow_base[i] = cal_mod(mul_mod(pow_base[i-1],BASE));
}
}
vector<int> vec;
int n;
vector<ull> inner_hash;
void build(const vector<int> &v){
set_hash();
vec = v;
n = v.size();
extend_pow_base(n);
inner_hash.resize(n+1);
inner_hash[0] = 0;
for (int i = 0; i < n; i++) inner_hash[i+1] = cal_mod(mul_mod(inner_hash[i],BASE) + v[i]);
}
};
using ull = unsigned long long;
ull RollingHash::BASE = 0;
vector<ull> RollingHash::pow_base = vector<ull>(1,1);
using roriha = RollingHash;
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){
yes();
return 0;
}
if (k > n){
no();
return 0;
}
if (k == n){
reverse(all(b));
a == b ? yes() : no();
return 0;
}
auto aa = a; rep(i,n) aa.emplace_back(a[i]);
roriha ha(aa);
ull hb = roriha::get_hash(b);
reverse(all(b));
ull hc = roriha::get_hash(b);
sort(all(a)), sort(all(b));
if (a != b){
no();
return 0;
}
if (k <= n-2){
yes();
return 0;
}
rep(i,n){
ull h = ha.get(i,i+n);
if (h == hb || h == hc){
yes();
return 0;
}
}
no();
return 0;
}
noya2