Advertisement
Solingen

z3.2.cpp

Dec 21st, 2024
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.97 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int mod7(int x)
  5. {
  6.     return (x % 7 + 7) % 7;
  7. }
  8.  
  9. // Вычисление P(x) (mod 7), где коэффициенты хранятся в массиве coeffs,
  10. // а degree - степень многочлена
  11. int polyValueMod7(const int* coeffs, int degree, int x)
  12. {
  13.     // P(x) = coeffs[0]*x^degree + coeffs[1]*x^(degree-1) + ... + coeffs[degree]
  14.     // Будем возводить x в степени последовательно
  15.     int result = 0;
  16.     // Для простоты - прямой способ:
  17.     int power = 1;
  18.     // но нам удобнее идти с высшей степени:
  19.     for (int i = 0; i <= degree; i++)
  20.     {
  21.         // i-я позиция в массиве = коэффициент при x^(degree-i)
  22.         // coeffs[0] при x^degree, coeffs[1] при x^(degree-1) ...
  23.         int exponent = degree - i;
  24.         // возвести x в exponent
  25.         int curPow = 1;
  26.         for (int j = 0; j < exponent; j++)
  27.         {
  28.             curPow = mod7(curPow * x);
  29.         }
  30.         result = mod7(result + mod7(coeffs[i] * curPow));
  31.     }
  32.     return mod7(result);
  33. }
  34.  
  35. int main()
  36. {
  37.     int degree;
  38.     cout << "Введите степень многочлена: ";
  39.     cin >> degree;
  40.     int* coeffs = new int[degree+1];
  41.     cout << "Введите " << degree+1 << " коэффициентов (от старшего к младшему) по модулю 7:\n";
  42.     for (int i = 0; i <= degree; i++)
  43.     {
  44.         cin >> coeffs[i];
  45.         coeffs[i] = mod7(coeffs[i]);
  46.     }
  47.  
  48.     bool found = false;
  49.     cout << "Корни многочлена в F7: ";
  50.     for (int x = 0; x < 7; x++)
  51.     {
  52.         int val = polyValueMod7(coeffs, degree, x);
  53.         if (val == 0)
  54.         {
  55.             cout << x << " ";
  56.             found = true;
  57.         }
  58.     }
  59.     if (!found) cout << "нет корней";
  60.     cout << endl;
  61.  
  62.     delete[] coeffs;
  63.     return 0;
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement