多项式的一种特殊解法
程序员文章站
2022-06-10 12:52:07
...
#include<iostream>
#include<vector>
#include<ctime>
#include<cmath>
using namespace std;
//多项式解法 y= 1+2*x+3*x^2+......
void main()
{
clock_t start = clock();
//提出x的解法
int n;
int x;
cin >> n >> x;
vector<int> a;
for (int i = 1; i <= n; i++)
{
a.push_back(i);
}
int p = a[n-1];
for (int i = n; i>=2; i--)
{
p = a[i - 2] + p*x;
}
cout << p << endl;
clock_t end = clock();
cout << (double)(end - start) / CLK_TCK << endl;
start = clock();
int sum = 0;
for (int i = 0; i < n; i++)
{
sum += a[i] * pow(x, i);
}
cout << sum << endl;
end = clock();
cout << (double)(end - start) / CLK_TCK;
system("pause");
}