結果

問題 No.3179 3 time mod
ユーザー tsunamayo123
提出日時 2025-05-30 22:59:46
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,373 bytes
コンパイル時間 7,965 ms
コンパイル使用メモリ 463,708 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2025-06-14 01:38:11
合計ジャッジ時間 8,944 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 42
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#include <boost/multiprecision/cpp_int.hpp>
namespace mp = boost::multiprecision;

using ll = long long;


// https://qiita.com/drken/items/b97ff231e43bce50199a
// けんちょんさんの記事より

// 返り値: a と b の最大公約数
// ax + by = gcd(a, b) を満たす (x, y) が格納される
long long extGCD(long long a, long long b, long long &x, long long &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    long long d = extGCD(b, a%b, y, x);
    y -= a/b * x;
    return d;
}

int main(){
    // input
    ll N,P,Q,R,A,B,C;
    cin>>N>>P>>Q>>R>>A>>B>>C;

    // ans=0に注意
    ll d[3]={P,Q,R};
    ll r[3]={A,B,C};
    for(int i=0;i<3;i++){
        for(int j=i+1;j<3;j++){
            if(d[i]==d[j]&&r[i]!=r[j]){
                cout<<0<<endl;
                return 0;
            }
        }
    }

    ll prod=P*Q*R;
    ll ans=N/prod;

    ll x1,y1;
    extGCD(P,Q,x1,y1); // gcd(P,Q,R)=1より拡張ユーグリッドの互除法が使える
    x1*=B-A;

    ll L=(P*Q),D=P*x1+A;
    D%=P*Q;
    

    ll x22,y2;
    extGCD(L,R,x22,y2);
    mp::cpp_int x2=x22;
    x2*=C-D;

    mp::cpp_int n=L*x2+D;

    if(n<0){
        n+=(L*R)*(((-n)/(L*R))+1);
    }
    n%=L*R;
    

    if(n%prod<=N%prod){
        cout<<ans+1<<endl;
    }else{
        cout<<ans<<endl;
    }
}
0