https://cn.vjudge.net/problem/UVALive-6185
我真的是服了orz eps 1e5,1e6过不了 开1e2 1e1都能过
题意:给你一个d阶多项式f的f(0),f(1)...f(d+1),f(d+2) 有一个是错误的,问第几个是错的
题解:题目多给了两个方程(约束)。
想了一下如果只给一个,是找不出来的。
给两个的话,可以这么考虑:
先取出一个方程X,再取剩下的n个高斯消元一下,将解得的系数带入最后一个方程,if成立,说明X是错的,else再取另一个(说明错误的在n个 或最后一个)。
坑:下标无限搞错,对着样例1写了一下,虽然具体一些,但有个d+1写成了3orz
#include <iostream>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include<cstdio>
#include<vector>
#define rep(i,t,n) for(int i =(t);i<=(n);++i)
#define per(i,n,t) for(int i =(n);i>=(t);--i)
#define mmm(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn= ;
double a[][],aa[][], del;
double eps = 5e-;
/*高斯消元,系数矩阵为a[i][j],i=1…n,j=1…n,常数为a[i][n+1],i=1…n,返回值为是否有唯一解,答案存在a[i][n+1]*/
bool gauss(int n) {
for (int i = ; i <= n; i++) {
int k = i;
for (int j = i + ; j <= n; j++)if (fabs(a[j][i]) > fabs(a[k][i]))k = j;
if (fabs(del = a[k][i]) < eps)return ;
for (int j = i; j <= n + ; j++)swap(a[i][j], a[k][j]);
for (int j = i; j <= n + ; j++)a[i][j] /= del;
for (k = ; k <= n; k++)if (k != i) {
del = a[k][i];
for (int j = i; j <= n + ; j++)a[k][j] -= a[i][j] * del;
}
}
return ;
} int main() {
int d;
while (cin >> d&&d) {
mmm(a, );
mmm(aa, );
rep(i, , d + ) {
//cin >> aa[i][d + 2];
scanf("%lf", &aa[i][d + ]);
//aa[i][d + 2] = aa[i][d + 1];
}aa[][] = ; rep(i, , d + ) {
rep(j, , d+) {
aa[i+][j] =pow(1.0*i, j-);
}
}
rep(k, , d + ) {
int tot = ;
rep(i, , d + )if (i != k&&tot<=d+) { rep(j, , d+) a[tot][j] = aa[i][j]; tot++;}
if (!gauss(d + ))continue;
double temp = ;
int last = d + ;
if (k == d + )last--;
rep(i, , d+)
temp += a[i][d + ] * aa[last][i];
if (abs(temp - aa[last][d + ]) < 0.5) {
cout << k - << endl; break;
} } }
}
/*
1
1
2
2 4
42.4715310246
79.5420238202
28.0282396675
-30.3627807522
-49.8363481393
-25.5101480106
7.58575761381
*/
/*
2
1.0
4.0
12.0
16.0
25.0
1
-30.5893962764
5.76397083962
39.3853798058
74.3727663177
4
42.4715310246
79.5420238202
28.0282396675
-30.3627807522
-49.8363481393
-25.5101480106
7.58575761381
5
-21.9161699038
-48.469304271
-24.3188578417
-2.35085940324
-9.70239202086
-47.2709510623
-93.5066246072
-82.5073836498
0*/