結果

問題 No.186 中華風 (Easy)
ユーザー HIcoderHIcoder
提出日時 2023-06-07 19:26:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,894 bytes
コンパイル時間 1,489 ms
コンパイル使用メモリ 88,964 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-28 17:44:45
合計ジャッジ時間 1,611 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘std::tuple<long long int, long long int, long long int> ext_gcd(ll, ll)’ 内:
main.cpp:28:10: 警告: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
   28 |     auto [g,x,y]=ext_gcd(b,a%b);
      |          ^
main.cpp: 関数 ‘std::pair<long long int, long long int> chinese_rem(ll, ll, ll, ll)’ 内:
main.cpp:42:10: 警告: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
   42 |     auto [d,p,q] = ext_gcd(m1,m2);
      |          ^
main.cpp: 関数 ‘std::pair<long long int, long long int> chinese_rem2(const std::vector<long long int>&, const std::vector<long long int>&)’ 内:
main.cpp:90:14: 警告: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
   90 |         auto [c,flag]=chinese_rem(x,M,b[i],m[i]);
      |              ^

ソースコード

diff #

//AC
#include<iostream>
#include<set>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<map>
#include<stack>
#include<numeric>
#include<queue>
#include<cmath>
#include<deque>
#include<cassert>
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
const ll INF=1LL<<60;

//ax+by=gcd(a,b)
tuple<ll,ll,ll> ext_gcd(ll a,ll b){
    //cout<<"a="<<a<<",b="<<b<<endl;

    if(b==0){
        //a*1+b*0 = a
        return make_tuple(a,1,0);
    }
    auto [g,x,y]=ext_gcd(b,a%b);
    //cout<<"g="<<g<<",x="<<x<<",y="<<y<<endl;

    return make_tuple(g,y,x-(a/b)*y);

    
}




pair<ll,ll> chinese_rem(ll b1,ll m1,ll b2, ll m2){
    
    //d=m1*p+m2*q
    auto [d,p,q] = ext_gcd(m1,m2);


    ll lcm=(m1/d)*m2;
    
    if((b2-b1)%d !=0){
        //(r,m)  = r (mod m)
        return make_pair(0,-1);
    }

    ll c=(b2-b1)/d;
   
   
    c*=p;

    c%=(m2/d);
    c*=m1;
    c%=lcm;

    ll x=b1+c;
    x%=lcm;
    x+=lcm;
    x%=lcm;

    return make_pair(x,lcm);
};



ll gcd(ll x,ll y){
    return y==0?x:gcd(y,x%y);
}

ll lcm(ll x,ll y){

    return x/gcd(x,y)*y;
}


pair<ll,ll> chinese_rem2(const vector<ll>& b,const vector<ll>& m){

    assert(b.size()==m.size());
    int n=b.size();

    ll x=0;
    ll M=1;
    for(int i=0;i<n;i++){
        
        auto [c,flag]=chinese_rem(x,M,b[i],m[i]);
        if(flag==-1){
            return make_pair(0,-1);
        }

        M=lcm(M,m[i]);
        x=c;
        
    }
    
    return make_pair((x%M + M)%M ,M);
}


int main(){

    //cout<<chinese_rem(2,3,3,5)<<endl;
    vector<ll> b(3);
    vector<ll> m(3);

    for(int i=0;i<3;i++){
        cin>>b[i]>>m[i];
    }

    if(b[0]==0 && b[1]==0 && b[2]==0){
        cout<<lcm(lcm(m[0],m[1]),m[2])<<endl;
        return 0;
    }

    auto ans=chinese_rem2(b,m);

    if(ans.second==-1){
        cout<<-1<<endl; 
    }else{
  
        cout<<ans.first<<endl;
    }
}
0