MSzopa

Arkusz Pokazowy Zad 3. C++

Dec 19th, 2022
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5. bool CzyPierwsza(int N) {
  6.     if (N < 2)
  7.         return false;
  8.     if (N == 2)
  9.         return true;
  10.     for (int i = 2; i < N; i++)
  11.         if (N % i == 0)
  12.             return false;
  13.     return true;
  14. }
  15. int potega(int a, int x, int M)
  16. {
  17.     int w = 1;
  18.     int z = a;
  19.     while (x > 0)
  20.     {
  21.         if (x % 2 == 1)
  22.             w = w * z % M;
  23.         z = z * z % M;
  24.         x = x / 2;
  25.     }
  26.     return w;
  27. }
  28. int Nwd(int a, int b) {
  29.     while (a != b) {
  30.         if (a < b)
  31.             swap(a, b);
  32.         a -= b;
  33.     }
  34.     return a;
  35. }
  36. int main()
  37. {
  38.     ifstream in;
  39.     in.open("liczby.txt");
  40.     const int liczbaWejsc = 1000;
  41.  
  42.     int c_1 = 0, c_2 = 0, c_3 = 0;
  43.     for (int i = 0; i < liczbaWejsc; i++) {
  44.         int a, b, M;
  45.         in >> M >> a >> b;
  46.         if (CzyPierwsza(M))
  47.             c_1++;
  48.         if (Nwd(M, a) == 1)
  49.             c_2++;
  50.         bool daSie = false;
  51.         for (int x = 0; x < M; x++) {
  52.             if (potega(a, x, M) == b)
  53.             {
  54.                 daSie = true;
  55.                 break;
  56.             }
  57.  
  58.         }
  59.         if (daSie)
  60.             c_3++;
  61.     }
  62.     ofstream out;
  63.     out.open("wyniki3.txt");
  64.     cout << "3.3. " << c_1 << endl;
  65.     cout << "3.4. " << c_2 << endl;
  66.     cout << "3.5. " << c_3 << endl;
  67.     out << "3.3. " << c_1 << endl;
  68.     out << "3.4. " << c_2 << endl;
  69.     out << "3.5. " << c_3 << endl;
  70.     out.flush();
  71.     out.close();
  72. }
Add Comment
Please, Sign In to add comment