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= 10000009; double a[15][15],aa[15][15], del; double eps = 5e-5; /*高斯消元,系数矩阵为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 = 1; i <= n; i++) { int k = i; for (int j = i + 1; j <= n; j++)if (fabs(a[j][i]) > fabs(a[k][i]))k = j; if (fabs(del = a[k][i]) < eps)return 0; for (int j = i; j <= n + 1; j++)swap(a[i][j], a[k][j]); for (int j = i; j <= n + 1; j++)a[i][j] /= del; for (k = 1; k <= n; k++)if (k != i) { del = a[k][i]; for (int j = i; j <= n + 1; j++)a[k][j] -= a[i][j] * del; } } return 1; } int main() { int d; while (cin >> d&&d) { mmm(a, 0); mmm(aa, 0); rep(i, 1, d + 3) { //cin >> aa[i][d + 2]; scanf("%lf", &aa[i][d + 2]); //aa[i][d + 2] = aa[i][d + 1]; }aa[1][1] = 1; rep(i, 1, d + 2) { rep(j, 1, d+1) { aa[i+1][j] =pow(1.0*i, j-1); } } rep(k, 1, d + 3) { int tot = 1; rep(i, 1, d + 3)if (i != k&&tot<=d+1) { rep(j, 1, d+2) a[tot][j] = aa[i][j]; tot++;} if (!gauss(d + 1))continue; double temp = 0; int last = d + 3; if (k == d + 3)last--; rep(i, 1, d+1) temp += a[i][d + 2] * aa[last][i]; if (abs(temp - aa[last][d + 2]) < 0.5) { cout << k - 1 << 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*/
原文地址:https://www.cnblogs.com/SuuT/p/9532172.html
时间: 2024-10-20 01:29:20