Reproduced in the blog:
Horner's method of polynomial
Is to simplify O (n ^ 2) is written to O (n) only.
However, this algorithm is also very friendly for polynomials.
. 1 #include <bits / STDC ++ H.> 2 #define N 1001 . 3 the using namespace STD; . 4 int n-, X, A [N], MOD; . 5 int main () . 6 { . 7 int ANS; . 8 Scanf ( " % D % D% D " , & n-, & x, & MOD); // n-represents the function f (x) the highest-order term of x, mod represents modulo number; . 9 for ( int I = 0 ; I <= n-; I ++ ) 10 Scanf ( " % D " , a & [I]); // a [I] represents a coefficient of each term; . 11 ANS =A [n-]; 12 is for ( int I = N- . 1 ; I> = 0 ; i-- ) 13 is { 14 ANS = (X + A * ANS [I]) MOD%; // Horner's method body, equation It is f (x) = (... ( (a [n] * x + a [n-1]) * x + a [n-2]) * x + ... a [1]) * x + a [0]; 15 } 16 the printf ( " % D " , ANS); . 17 return 0 ; 18 is }