942. Lettree's Math Homework
题目描述
Little lettree is a middle school student who is clever but hates doing homework very much.
However, on this Qingming holiday, his math teacher assigned a lot of homework about fractions just like this:
Decomposite the fraction
to this form:
Clever lettree found that these are all repeat work and he need not calculate each fraction by himself. He just need a simple piece of computer program to do this.Now please help him do this simple work!
输入格式
Multiple test cases!
For each test case:
First line contains two integers
n,m(n<m≤6)
, the amount of moleculars and denominators.
Second line contains n real numbers
ai
(Guarantee that
a1<a2<...<an
,
−100<ai<100
).
Third line contains m real numbers
bi
(Guarantee that
b1<b2<...<bm
,
−100<bi<100
).
Input ends with the end of file(EOF).
输出格式
For each test case,output one line of real numbers ci ,accurate to 6 decimal places.
Hint
You should avoid
−0.000000
in your output!
输入样例
2 3
4 50
12 50 76
2 4
58 73
30 44 72 78
输出样例
-0.125000 0.000000 1.125000
-0.042659 0.030462 0.001984
0.010212
里面说的很清晰,f(x)/g(x)那里面的每一个Bi就是下面的xi-xj(i!=j)相乘做分母,然后xi带入f(x)做分子。
代码:
/*
USER_ID: test#299792458ms
PROBLEM: 942
SUBMISSION_TIME: 2016-03-21 16:03:50
*/
#pragma warning(disable:4996)
#include <iostream>
#include <functional>
#include <algorithm>
#include <cstring>
#include <vector>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <deque>
#include <ctime>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define INF 0x33ffffff
#define eps 1e-6
const ll mod = 1000000007;
const int maxn = 505;
const double PI = acos(-1.0);
int n, m;
double a[10], b[10], c[10];
void solve()
{
int i, j, k;
double up, down;
for (i = 0; i < n; i++)
{
cin >> a[i];
}
for (i = 0; i < m; i++)
{
cin >> b[i];
}
for (i = 0; i < m; i++)
{
up = 1, down = 1;
for (j = 0; j < n; j++)
{
up = up*(b[i] - a[j]);
}
for (j = 0; j < m; j++)
{
if (j == i)continue;
down = down*(b[i] - b[j]);
}
c[i] = up / down;
if (c[i] == -0.0)
c[i] = 0.0;
printf("%.6lf%c", c[i], i == m - 1 ? '\n' : ' ');
}
}
int main()
{
#ifdef wangchong756
freopen("i.txt", "r", stdin);
freopen("o.txt", "w", stdout);
int time_wangchong756 = clock();
#endif
while (scanf("%d%d", &n, &m) != EOF)
{
solve();
}
//system("pause");
return 0;
}