結果
問題 |
No.2558 中国剰余定理
|
ユーザー |
![]() |
提出日時 | 2023-12-03 15:34:09 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 642 bytes |
コンパイル時間 | 1,536 ms |
コンパイル使用メモリ | 166,020 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-09-26 22:10:58 |
合計ジャッジ時間 | 2,092 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 29 |
ソースコード
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int A[3000][3]; void d(int i){ int x=A[i][0]%A[i+1][0],y=A[i][0]/A[i+1][0]; A[i+2][0]=x; A[i+2][1]=A[i][1]-y*A[i+1][1]; A[i+2][2]=A[i][2]-y*A[i+1][2]; } int main() { int p,q,a,b;cin>>p>>q>>a>>b; if(p<q){swap(p,q);swap(a,b);} if(q==1){cout<<a<<endl;} else{ A[0][0]=p;A[0][1]=1;A[0][2]=0; A[1][0]=q;A[1][1]=0;A[1][2]=1; int i=0; while(A[i+1][0]!=1){ d(i);i++; } int x=A[i+1][1],y=A[i+1][2]; long long ans=p*x; ans%=(p*q);ans*=(b-a);ans+=a; ans%=(p*q); if(ans<0){ans+=(p*q);} cout<<ans<<endl; } }