結果

問題 No.1544 [Cherry 2nd Tune C] Synchroscope
ユーザー homer12homer12
提出日時 2021-06-11 21:41:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,037 bytes
コンパイル時間 2,239 ms
コンパイル使用メモリ 217,268 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-08-21 11:56:24
合計ジャッジ時間 6,152 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using iPair = pair<int,int>;
using lPair = pair<ll, ll>;
using ivector = vector<int>;
using lvector = vector<ll>;
using istack = stack<int>;
using iqueue = queue<int>;
using ivv = vector<vector<int>>;
using lvv = vector<vector<ll>>;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
vector<iPair> dir = {{1,0}, {-1,0}, {0,1}, {0,-1}};
#define dump(x) cout << #x << " = " << (x) << endl
#define ALL(x) begin(x),end(x)
#define rep(i,s,e) for(ll i=(s), i_stop=(e); i<i_stop; ++i)
#define repRev(i,s,e) for(ll i=(e)-1, i_stop=(s); i>=i_stop; --i)
#define range(i,s,n) for(ll i=(s), i_stop=(s)+(n); i<i_stop; ++i)
#define rangeRev(i,s,n) for(ll i=(s), i_stop=(s)-(n); i>i_stop; --i)
#define foreach(x,container) for(auto &&x:container)
template<typename T> bool chmax(T& a, const T b) {if(a<b) {a=b;return true;} return false;}
template<typename T> bool chmin(T& a, const T b) {if(a>b) {a=b;return true;} return false;}
template<typename T> void printArr(vector<T> &arr){
    for(auto &x:arr) {cout << x << " ";} cout << endl;
}

/*
因爲涉及到餘數,寫代碼從0開始。
*/

struct NumberTheory{
    inline ll mod(ll a, ll m){
        return (a % m + m) % m;
    }
 
    // 參數引用形式返回 ap + bq = gcd(a,b) 的一組解(p0, q0)
    // 返回值形式返回 gcd(a,b)
    ll extGcd(ll a, ll b, ll &p, ll &q){
        if( b == 0 ) { p = 1; q = 0; return a; }
        ll d = extGcd(b, a%b, q, p);
        q = q - (a/b) * p;
        return d;
    }

    // 當a與m互素時,返回乘法逆元。否則返回-1
    ll modinv(ll a, ll m) {
        ll p, q;
        ll d = extGcd(a,m,p,q);
        return (d==1)?mod(p,m):-1;
    }


    // 返回中國剩餘定理的解以及最小公倍數
    pair<ll,ll> crt(vector<ll> b, vector<ll> m){
        ll r = 0, M = 1;
        for(int i = 0; i < (int)b.size(); ++i){
            ll p, q;
            ll d = extGcd(M, m[i], p, q);
            if((b[i] - r) % d != 0) return make_pair(0, -1);
            ll tmp = (b[i] - r) / d * p % (m[i] / d);
            r += M * tmp;
            M *= m[i] / d;
        }
        return make_pair(mod(r, M), M);
    }
};


void solve() {
    int n,m; cin>>n>>m;

    
    map<int,int> mapping;
    vector<iPair> res;
    ivector a(n); range(i, 0, n) {cin>>a[i]; mapping[a[i]] = i;}
    ivector b(n); range(i, 0, m) {
        cin>>b[i];
        if(mapping.count(b[i])) {
            res.emplace_back(mapping[b[i]], i);
        }
    }

    if(res.size() == 0) {
        cout<<-1<<endl; return;
    }

    NumberTheory nt;
    ll ans = LINF;
    for(auto [i,j] : res) {
        // n,m不互素的話不一定有解
        ll p,q;
        ll g = nt.extGcd(i,j,p,q);
        if(i%g != j%g) continue;
        lvector mod = {(ll)n, (ll)m};
        lvector b = {(ll)i, (ll)j};
        auto [t, M] = nt.crt(b, mod);
        chmin(ans, t);
    }

    cout << ans+1 << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    solve();
    return 0;
}
0