-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10268.cpp
52 lines (42 loc) · 995 Bytes
/
10268.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "bits/stdc++.h"
using namespace std;
long long p (long a, long to) {
long long res = 1;
for (int i = 0; i < to; ++i) {
res*= a;
}
return res;
}
long long horner(vector<long long> poly, long long n, long long x)
{
long long result = poly[0]; // Initialize result
// Evaluate value of polynomial using Horner's method
for (long long i=1; i<n; i++)
result = result*x + poly[i];
return result;
}
int main(int argc, char const *argv[]) {
long long x;
while (scanf("%lld",&x) != EOF) {
stack<long long> s;
string str;
cin.ignore();
getline(cin, str);
stringstream ss (str);
long long a;
vector <long long> poly;
while (ss >> a) {
poly.push_back(a);
}
int k = poly.size() - 1;
for (int i = 0; i < poly.size(); ++i) {
poly[i] *= k--;
}
long long cont = horner(poly, poly.size()-1, x);
printf("%lld\n", cont);
//} else {
//printf("0\n");
//}
}
return 0;
}