thisisnotabin

IS Lab Chinese Theorem

Sep 13th, 2023
824
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     int a[10], n[10], m[10], mi[10], i, size, M = 1, Y = 0;
  7.     cout << "Enter the size of array :";
  8.     cin >> size;
  9.  
  10.     for (i = 0; i < size; i++)
  11.     {
  12.         cout << "The value of a" << i << endl;
  13.         cin >> a[i];
  14.     }
  15.    
  16.     for (i = 0; i < size; i++)
  17.     {
  18.         cout << "Enter the values of n"<<i<<": ";
  19.         cin >> n[i];
  20.     }
  21.  
  22.     for (i = 0; i < size; i++)
  23.     {
  24.         M = M * n[i];
  25.     }
  26.     cout << "\nM = " << M;
  27.     for (i = 0; i < size; i++)
  28.     {
  29.         m[i] = M / n[i];
  30.         cout << "\nm" << i << "= " << m[i];
  31.     }
  32.  
  33.     for (i = 0; i < size; i++)
  34.     {
  35.         mi[i] = m[i] % n[i];
  36.         cout << "\nm" << i << " inverse = " << mi[i];
  37.     }
  38.  
  39.     for (i = 0; i < size; i++)
  40.     {
  41.         Y = Y + (a[i] * m[i] * mi[i]);
  42.     }
  43.     cout << "\n\nY = " << Y;
  44.     Y = Y % M;
  45.     cout << "\n\nChinese Remainder(Y MOD M:" << Y << endl;
  46.     for (int i = 0; i < size; i++)
  47.     {
  48.         cout << a[i] << "(mod)" << n[i] << "=" << Y << endl;
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment