結果

問題 No.1544 [Cherry 2nd Tune C] Synchroscope
ユーザー abc3abc3
提出日時 2021-06-11 22:26:53
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 318 ms / 2,000 ms
コード長 2,335 bytes
コンパイル時間 2,548 ms
コンパイル使用メモリ 201,892 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-08-21 13:03:18
合計ジャッジ時間 4,587 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 5 ms
4,384 KB
testcase_04 AC 8 ms
4,380 KB
testcase_05 AC 6 ms
4,376 KB
testcase_06 AC 7 ms
4,380 KB
testcase_07 AC 7 ms
4,376 KB
testcase_08 AC 6 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 7 ms
4,376 KB
testcase_11 AC 5 ms
4,380 KB
testcase_12 AC 4 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 9 ms
4,376 KB
testcase_15 AC 10 ms
4,380 KB
testcase_16 AC 4 ms
4,376 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 8 ms
4,380 KB
testcase_21 AC 3 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 12 ms
4,380 KB
testcase_24 AC 12 ms
4,380 KB
testcase_25 AC 12 ms
4,376 KB
testcase_26 AC 12 ms
4,380 KB
testcase_27 AC 13 ms
4,380 KB
testcase_28 AC 12 ms
4,380 KB
testcase_29 AC 13 ms
4,380 KB
testcase_30 AC 12 ms
4,380 KB
testcase_31 AC 12 ms
4,380 KB
testcase_32 AC 13 ms
4,380 KB
testcase_33 AC 318 ms
4,380 KB
testcase_34 AC 2 ms
4,388 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 12 ms
4,380 KB
testcase_39 AC 12 ms
4,380 KB
testcase_40 AC 12 ms
4,376 KB
testcase_41 AC 12 ms
4,380 KB
testcase_42 AC 12 ms
4,376 KB
testcase_43 AC 12 ms
4,380 KB
testcase_44 AC 11 ms
4,376 KB
testcase_45 AC 12 ms
4,380 KB
testcase_46 AC 11 ms
4,380 KB
testcase_47 AC 11 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

    #include <bits/stdc++.h>
    using namespace std;
    #include <math.h>
    #include <iomanip>
    #include <cstdint>
    #include <string>
    #include <sstream>
    template<class T> inline bool chmax(T& a, T b) { if (a <= b) { a = b; return 1; } return 0; }
    template<class T> inline bool chmin(T& a, T b) { if (a >= b) { a = b; return 1; } return 0; }
    #define rep(i,n) for (int i = 0; i < (n); ++i)
    typedef long long ll;
    using P=pair<ll,ll>;
    const int INF=1001001001;
    //const int mod=998244353;
    
    // 負の数にも対応した mod
    // 例えば -17 を 5 で割った余りは本当は 3 (-17 ≡ 3 (mod. 5))
    // しかし単に -17 % 5 では -2 になってしまう
    inline long long mod(long long a, long long m) {
        return (a % m + m) % m;
    }
    
    // 拡張 Euclid の互除法
    // ap + bq = gcd(a, b) となる (p, q) を求め、d = gcd(a, b) をリターンします
    long long extGcd(long long a, long long b, long long &p, long long &q) {  
        if (b == 0) { p = 1; q = 0; return a; }  
        long long d = extGcd(b, a%b, q, p);  
        q -= a/b * p;  
        return d;  
    }
    
    // 中国剰余定理
    // リターン値を (r, m) とすると解は x ≡ r (mod. m)
    // 解なしの場合は (0, -1) をリターン
    pair<long long, long long> ChineseRem(long long b1, long long m1, long long b2, long long m2) {
      long long p, q;
      long long d = extGcd(m1, m2, p, q); // p is inv of m1/d (mod. m2/d)
      if ((b2 - b1) % d != 0) return make_pair(0, -1);
      long long m = m1 * (m2/d); // lcm of (m1, m2)
      long long tmp = (b2 - b1) / d * p % (m2/d);
      long long r = mod(b1 + m1 * tmp, m);
      return make_pair(r, m);
    }

    void solve(){
        int n,m;
        cin>>n>>m;
        vector<int>a(n),b(m);
        rep(i,n){cin>>a[i];}
        rep(i,m){cin>>b[i];}
        ll mn=1e18;
        rep(i,n){
            rep(j,m){
                if(a[i]!=b[j]){continue;}
                auto t=ChineseRem(i,n,j,m);
                if(t.first==0&&t.second==-1){continue;}
                chmin(mn,t.first+1);
            }
        }
        if(mn==1e18){mn=-1;}
        cout<<mn<<endl;
    }
    int main(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        solve();
        return 0;
    }
0