Cao Chong Pig Raising(CRT

曹操は豚を育てる

题意

サイズnのAとm、mは豚舎の数を表し、aは豚を豚舎に分割した後の残りを表し、最終的にn条件を満たす豚の最小数を尋ねます

aが比較的素数である場合

説明

これはCRTで取得でき、最後にモードを[0、M-1]に変更できます。これは最小の解です。

1 #include <bits / stdc ++。h>
 2  #define ll long long
 3  using  namespace std;
4  const  int N = 2e6;
5  ll a [N]、m [N];
6  int n;
7 ll exgcd(ll a、ll b、ll&x、ll&y){
 8      if(!b){
 9          x = 1 ; y = 0 ;
10          戻るa;
11      }
 12      ll d = exgcd(b、a%b、y、x);
13      y-=(a / b)* x;
14      返品d;
15  }
 16  ll CRT(){
 17      ll M = 1 ;
18      forint i = 1 ; i <= n; i ++ ){
 19          M * = m [i];
20      }
 21      ll res = 0 ;
22      forint i = 1 ; i <= n; i ++ ){
 23          ll x、y;
24          ll tmp = M / m [i];
25          exgcd(tmp、m [i]、x、y);
26          res =(res + tmp * x * a [i])%M;
27      }
28      リターン((res + M)%M)%M;
29  }
 30  int main(){
 31      cin >> n;
32      forint i = 1 ; i <= n; i ++ 33          cin >> m [i] >> a [i];
34      cout << CRT()<< endl;
35  
36 }

 

おすすめ

転載: www.cnblogs.com/hhyx/p/12702754.html