Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int mod7(int x)
- {
- return (x % 7 + 7) % 7;
- }
- // Вычисление P(x) (mod 7), где коэффициенты хранятся в массиве coeffs,
- // а degree - степень многочлена
- int polyValueMod7(const int* coeffs, int degree, int x)
- {
- // P(x) = coeffs[0]*x^degree + coeffs[1]*x^(degree-1) + ... + coeffs[degree]
- // Будем возводить x в степени последовательно
- int result = 0;
- // Для простоты - прямой способ:
- int power = 1;
- // но нам удобнее идти с высшей степени:
- for (int i = 0; i <= degree; i++)
- {
- // i-я позиция в массиве = коэффициент при x^(degree-i)
- // coeffs[0] при x^degree, coeffs[1] при x^(degree-1) ...
- int exponent = degree - i;
- // возвести x в exponent
- int curPow = 1;
- for (int j = 0; j < exponent; j++)
- {
- curPow = mod7(curPow * x);
- }
- result = mod7(result + mod7(coeffs[i] * curPow));
- }
- return mod7(result);
- }
- int main()
- {
- int degree;
- cout << "Введите степень многочлена: ";
- cin >> degree;
- int* coeffs = new int[degree+1];
- cout << "Введите " << degree+1 << " коэффициентов (от старшего к младшему) по модулю 7:\n";
- for (int i = 0; i <= degree; i++)
- {
- cin >> coeffs[i];
- coeffs[i] = mod7(coeffs[i]);
- }
- bool found = false;
- cout << "Корни многочлена в F7: ";
- for (int x = 0; x < 7; x++)
- {
- int val = polyValueMod7(coeffs, degree, x);
- if (val == 0)
- {
- cout << x << " ";
- found = true;
- }
- }
- if (!found) cout << "нет корней";
- cout << endl;
- delete[] coeffs;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement