結果

問題 No.2254 Reverse Only
ユーザー 0214sh70214sh7
提出日時 2023-03-24 23:18:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 458 ms / 2,000 ms
コード長 6,478 bytes
コンパイル時間 2,909 ms
コンパイル使用メモリ 219,724 KB
実行使用メモリ 25,248 KB
最終ジャッジ日時 2023-10-18 21:34:31
合計ジャッジ時間 11,175 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 155 ms
11,256 KB
testcase_09 AC 457 ms
25,248 KB
testcase_10 AC 136 ms
11,256 KB
testcase_11 AC 154 ms
11,256 KB
testcase_12 AC 154 ms
9,672 KB
testcase_13 AC 156 ms
11,256 KB
testcase_14 AC 155 ms
11,256 KB
testcase_15 AC 156 ms
11,256 KB
testcase_16 AC 219 ms
25,248 KB
testcase_17 AC 218 ms
25,248 KB
testcase_18 AC 153 ms
9,672 KB
testcase_19 AC 150 ms
11,256 KB
testcase_20 AC 458 ms
25,248 KB
testcase_21 AC 452 ms
25,248 KB
testcase_22 AC 381 ms
25,248 KB
testcase_23 AC 441 ms
25,248 KB
testcase_24 AC 317 ms
25,248 KB
testcase_25 AC 325 ms
25,248 KB
testcase_26 AC 325 ms
25,248 KB
testcase_27 AC 7 ms
4,348 KB
testcase_28 AC 34 ms
4,920 KB
testcase_29 AC 73 ms
7,032 KB
testcase_30 AC 63 ms
6,504 KB
testcase_31 AC 36 ms
5,184 KB
testcase_32 AC 41 ms
5,448 KB
testcase_33 AC 20 ms
4,392 KB
testcase_34 AC 94 ms
8,088 KB
testcase_35 AC 60 ms
6,240 KB
testcase_36 AC 130 ms
9,936 KB
testcase_37 AC 138 ms
10,200 KB
testcase_38 AC 29 ms
4,656 KB
testcase_39 AC 122 ms
9,408 KB
testcase_40 AC 6 ms
4,348 KB
testcase_41 AC 54 ms
5,976 KB
testcase_42 AC 32 ms
4,920 KB
testcase_43 AC 99 ms
8,352 KB
testcase_44 AC 145 ms
10,728 KB
testcase_45 AC 89 ms
7,824 KB
testcase_46 AC 5 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PP;
//#define MOD 1000000007
#define MOD 998244353
#define INF 2305843009213693951
//#define INF 810114514
#define PI 3.141592653589
#define setdouble setprecision
#define REP(i,n) for(ll i=0;i<(n);++i)
#define OREP(i,n) for(ll i=1;i<=(n);++i)
#define RREP(i,n) for(ll i=(n)-1;i>=0;--i)
#define ALL(v) (v).begin(), (v).end()
#define GOODBYE do { cout << "-1" << endl; return 0; } while (false)
#define MM <<" "<<
#define Endl endl
#define debug true
#define debug2 false


class rollinghash{
    /*
    Copyright (c) 2022 0214sh7
    https://github.com/0214sh7/library/
    */
    private:
    static constexpr long long mod = (1LL << 61)-1;
    std::vector<long long> Base = {12345,10000000};
    std::vector<long long> BaseInv;
    std::vector<std::vector<long long>> BaseInvExp;
    static constexpr long long h = 100;
    
    long long product(long long a,long long b){
        static constexpr long long m = 1LL << 31;
        long long a1 = a/m,a2 = a%m;
        long long b1 = b/m,b2 = b%m;
        
        long long r = 0 , s;
        r = (r + 2*a1*b1) % mod;
        s = (a1*b2 + b1*a2) % mod;
        long long s1 = s/m,s2 = s%m;
        s = (2*s1+m*s2) % mod;
        r = (r + s) % mod;
        r = (r + a2*b2) % mod;
        
        return r;
    }
    
    long long power(long long b,long long e){
        long long r=1;
        while(e){
            if(e&1){
                r=product(r,b)%mod;
            }
                b=product(b,b)%mod;
                e >>=1;
        }
        return r;
    }
    
    public:
    std::vector<long long> S;
    std::vector<std::vector<long long>> H,Hsum;
    
    void init(std::vector<long long> cs){
        S=cs;
        int n=S.size();
        
        BaseInv.resize(Base.size());
        BaseInvExp.resize(Base.size());
        H.resize(Base.size());
        Hsum.resize(Base.size());
        for(int i=0;i<Base.size();i++){
            BaseInvExp[i].assign(n+1,1);
            H[i].assign(n+1,0);
            Hsum[i].assign(n+1,0);
        }
        
        //逆元
        for(int i=0;i<Base.size();i++){
            BaseInv[i]=power(Base[i],mod-2);
        }
        for(int i=0;i<Base.size();i++){
            for(int j=0;j<n;j++){
                BaseInvExp[i][j+1] = product(BaseInvExp[i][j],BaseInv[i]);
            }
        }
        
        //本体
        for(int i=0;i<Base.size();i++){
            long long b=1;
            for(int j=0;j<n;j++){
                H[i][j]=product(b,S[j]+h);
                b=product(b,Base[i]);
            }
        }
        
        //累積和
        for(int i=0;i<Base.size();i++){
            for(int j=0;j<n;j++){
                Hsum[i][j+1]=(Hsum[i][j]+H[i][j])%mod;
            }
        }
    }
    
    rollinghash(std::vector<long long> S){
        init(S);
    }
    
    std::vector<long long> get(int l,int r){
        std::vector<long long> R(Base.size());
        for(int i=0;i<Base.size();i++){
            long long g = (Hsum[i][r]-Hsum[i][l]+mod)%mod;
            g=product(g,BaseInvExp[i][l]);
            R[i] = g;
        }
        return R;
    }
    
    std::vector<long long> instant(std::vector<long long> P){
        std::vector<long long> R;
        for(int i=0;i<Base.size();i++){
            long long r = 0, b = 1;
            for(int j=0;j<P.size();j++){
                r = (r+product(b,P[j]+h))%mod;
                b = product(b,Base[i]);
            }
            R.push_back(r);
        }
        return R;
    }
    
    std::vector<long long> connect(std::vector<long long> P,long long ps,std::vector<long long> Q,long long qs){
        std::vector<long long> R;
        for(int i=0;i<Base.size();i++){
            long long r = (product(Q[i],power(Base[i],ps))+P[i])%mod;
            R.push_back(r);
        }
        return R;
    }
    
};

int main(void){
    //cin.tie(nullptr);
    //ios::sync_with_stdio(false);
    
    ll N,K;
    cin >> N >> K;
    vector<ll> A(N),B(N);
    REP(i,N){cin >> A[i];}
    REP(i,N){cin >> B[i];}
    
    vector<ll> sA = A,sB = B;
    sort(ALL(sA));
    sort(ALL(sB));
    
    if(sA!=sB){
        cout << "No" << endl;
        return 0;
    }
    
    vector<ll> rB = B;reverse(ALL(rB));
    
    if(K<=N-2){
        cout << "Yes" << endl;
        return 0;
    }
    
    if(K==N){
        if(A==B || A==rB){
            cout << "Yes" << endl;
        }else{
            cout << "No" << endl;
        }
        return 0;
    }
    
    if(K>N){
        if(A==B){
            cout << "Yes" << endl;
        }else{
            cout << "No" << endl;
        }
        return 0;
    }
    
    
    rollinghash ROL(A);
    
    vector<ll> hB = ROL.instant(B),hrB = ROL.instant(rB);
    
    //REP(i,hB.size()){cout << hB[i] << " ";}cout << Endl;
    //REP(i,hB.size()){cout << hrB[i] << " ";}cout << Endl;cout << Endl;
    
    REP(i,N){
        vector<ll> S = ROL.get(0,i),T = ROL.get(i,N);
        vector<ll> R = ROL.connect(T,N-i,S,i);
        //REP(i,hB.size()){cout << R[i] << " ";}cout << Endl;
        
        if(R==hB || R==hrB){
            cout << "Yes" << endl;
            return 0;
        }
    }
    
    cout << "No" << endl;
    
    /*ll N = 5,K = 3;
    cin >> N >> K;
    map<vector<ll>,ll> ind;
    ll now = 0;
    vector<ll> d(N);
    REP(i,N){d[i] = i;}

    do{
        
        ind[d] = now;
        now++;
        
    }while(next_permutation(ALL(d)));
    
    queue<vector<ll>> que;
    vector<bool> enqueued(now,false);
    
    REP(i,N){d[i] = i;}
    que.push(d);
    enqueued[ind[d]] = true;
    
    while(!que.empty()){
        
        vector<ll> t = que.front();
        que.pop();
        
        for(ll l=0;l<N;l++){
            for(ll r=l;r<N;r++){
                
                if((r-l+1)<K)continue;
                vector<ll> R = t;
                for(ll e=l;e<=r;e++){
                    R[e] = t[l+r-e];
                }
                
                if(!enqueued[ind[R]]){
                    que.push(R);
                    enqueued[ind[R]] = true;
                }
                
            }
        }
        
    }
    
    
    REP(i,N){d[i] = i;}
    ll c = 0;
    do{
        if(enqueued[ind[d]]){
            REP(i,N){
                cout << d[i] << " ";
            }
            cout << endl;
            c++;
        }
    }while(next_permutation(ALL(d)));
    
    cout << c MM (now-c) << endl;*/
    
    return 0;
}
0