Problem:
Problem D:
Find the Outlier
Professor Abacus has just built a new computing engine for making numerical tables. It was designed to calculate the values of a polynomial function in one variable at several points at a time. With the polynomial function
f
(
x
) =
x
2
+ 2
x
+ 1, for instance, a possible expected calculation result is 1 (=
f
(0)), 4 (=
f
(1)), 9 (=
f
(2)), 16 (=
f
(3)), and 25 (=
f
(4)).
It is a pity, however, the engine seemingly has faulty components and exactly one value among those calculated simultaneously is always wrong. With the same polynomial function as above, it can, for instance, output 1, 4, 12, 16, and 25 instead of 1, 4, 9, 16, and 25.
You are requested to help the professor identify the faulty components. As the first step, you should write a program that scans calculation results of the engine and finds the wrong values.
Input
The input is a sequence of datasets, each representing a calculation result in the following format.
d
v
0
v
1
...
v
d
+2
Here,
d
in the first line is a positive integer that represents the degree of the polynomial, namely, the highest exponent of the variable. For instance, the degree of 4
x
5
+ 3
x
+ 0.5 is five and that of 2.4
x
+ 3.8 is one.
d
is at most five.
The following
d
+ 3 lines contain the calculation result of
f
(0),
f
(1), ... , and
f
(
d
+ 2) in this order, where
f
is the polynomial function. Each of the lines contains a decimal fraction between -100.0 and 100.0, exclusive.
You can assume that the wrong value, which is exactly one of
f
(0),
f
(1), ... , and
f
(
d
+2), has an error greater than 1.0. Since rounding errors are inevitable, the other values may also have errors but they are small and never exceed 10
-6
.
The end of the input is indicated by a line containing a zero.
Output
For each dataset, output
i
in a line when
v
i
is wrong.
Sample Input
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
Output for the Sample Input
2
1
1
6

Submitted solutions:

code_snippet: #include<bits/stdc++.h>
using namespace std;
int d;
double a[1003][1004];
double k[20005];
bool solve(){
//	for(int i=1;i<=d+3;i++){
	////	for(int j=1;j<=d+2;j++){
	//		cout<<a[i][j]<<" ";
	//	}
	//	cout<<endl;
	//}
	int n=d+1,m=d+2;
	
	/*for(int i=1;i<=n;i++){
		int maxx=i;
		for(int j=i+1;j<=n;j++){
			if(fabs(a[j][i])>fabs(a[maxx][i])){
				maxx=j;
			}
		}
		if(fabs(a[maxx][i])>1e-4) swap(a[i],a[maxx]);
		else continue;
		for(int j=n+1;j>=i;j--) a[i][j]/=a[i][i]; 
		for(int j=1;j<=n+1;j++){
			if(j==i) continue;
			for(int k=n+1;k>=i;k--){
				a[j][k]-=a[j][i]*a[i][k];
			}
		}
	}*/
	int line=1;
	for(int i=1;i<=n;i++){
		int id=line;
		for(int j=id+1;j<=m;j++){
			if(fabs(a[j][i])>fabs(a[id][i])){
				id=j;
			}
		}
		if(fabs(a[id][i])>1e-10) swap(a[line],a[id]);
		else continue;
		for(int j=n+1;j>=i;j--) a[line][j]/=a[line][i];
		for(int j=1;j<=m;j++){
			if(line==j){
				continue;
			} 
			for(int k=n+1;k>=i;k--) a[j][k]-=a[j][i]*a[line][k];
		}
		line++;
	}
//	cout<<"x: "<<"\n";
//	for(int i=1;i<=d+3;i++){
//		for(int j=1;j<=d+2;j++){
//			cout<<a[i][j]<<" ";
//		}
//		cout<<endl;
//	}
//	cout<<a[d+2][d+2]<<endl;
	for(int i=line;i<=m;i++) {
		if(fabs(a[i][n+1])>1e-5){
			return false;
		}
	}
	return true;

}
int power(int c,int b){
	int res=1;
	while(b){
		if(b&1){
			res=res*c;
		}
		c=c*c;
		b>>=1;
	}
	return res;
}
signed main(){
	while(cin>>d){
		if(!d) break;
		else{
			
			for(int i=1;i<=d+3;i++){
				cin>>k[i];
				//cin>>a[i][d+2];
				for(int j=1;j<=d;j++) a[i][j]=i;
			}
			for(int i=1;i<=d+3;i++){
				for(int j=1;j<=d+3;j++){
					for(int p=1;p<=d+2;p++) a[j][p]=(p==d+2)?k[j]:power(j-1,d+1-p);
				}
				for(int j=i;j<=d+2;j++) swap(a[j],a[j+1]);
			//cout<<"x "<<i<<endl;
				if(solve()){
					cout<<i-1<<endl;
					break;
				}
			}
		}
	}
}
/*
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

*/
accuracy: 1
time_ms: 10
memory_kb: 3488
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;
template <typename T>
struct mat {
	int n, m;
	vector<vector<T>> a;
	mat(int n_, int m_) : n(n_), m(m_), a(n_, vector<T>(m_)) {}
};
template <typename T>
vector<T> solveLSE(mat<T> a, vector<T> b) { // ax=b
	assert(a.n == b.size() && a.m == b.size());
	int n = a.n;
	for (int i = 0; i < n; i++) {
		int p = i;
		for (int j = p; j < n; j++) {
			if (a.a[i][j] > a.a[i][p]) p = j;
		}
		swap(a.a[i], a.a[p]);
		swap(b[i], b[p]);
		for (int j = 0; j < n; j++) {
			if (j != i) a.a[i][j] /= a.a[i][i];
		}
		b[i] /= a.a[i][i];
		a.a[i][i] = 1;
		for (int k = 0; k < n; k++) {
			if (i == k) continue;
			for (int j = 0; j < n; j++) {
				if (j != i) a.a[k][j] -= a.a[i][j] * a.a[k][i];
			}
			b[k] -= b[i] * a.a[k][i];
			a.a[k][i] = 0;
		}
	}
	return b;
}
int main() {
	mt19937 mt(time(NULL));
	while (1) {
		int d;
		cin >> d;
		if (d == 0) break;
		uniform_int_distribution<int> rd(0, d + 2);
		vector<double> v(d + 3);
		for (int i = 0; i < d + 3; i++) {
			cin >> v[i];
		}
		int ans = -1;
		double bestscore = 1e100;
		for (int igni = 0; igni < d + 3; igni++) {
			mat<double> a(d + 1, d + 1);
			vector<int> idx;
			for (int i = 0; i < d + 3; i++) {
				if (i == igni) continue;
				idx.push_back(i);
			}
			shuffle(idx.begin(), idx.end(), mt);
			idx.pop_back();
			vector<double> b(d + 1);
			{
				int i = 0;
				for (int ri : idx) {
					for (int j = 0; j < d + 1; j++) {
						a.a[i][j] = pow(ri, j);
					}
					b[i] = v[ri];
					i++;
				}
			}

			auto res = solveLSE(a, b);
			assert(res.size() == d + 1);
			auto f = [&](int x) {
				double r = 0;
				for (int j = 0; j <= d; j++) {
					r += res[j] * pow(x, j);
				}
				return r;
			};
			double igerror = 0, nomerror = 0;
			for (int i = 0; i < d + 3; i++) {
				if (i == igni) {
					igerror += abs(f(i) - v[i]);
				} else {
					nomerror += abs(f(i) - v[i]);
				}
			}
			double score = nomerror / igerror;
			if (bestscore > score) {
				// for (int i = 0; i < d + 1; i++) {
				// 	cerr << res[i] << " ";
				// }
				// cerr << endl;
				bestscore = score;
				ans = igni;
			}
		}
		cout << ans << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3900
final_rank: 17

code_snippet: //
// 実数行列 (行列累乗と、掃き出し法)
//
// verified:
//   AOJ 2171 Strange Couple
//     http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2171
//
//   AOJ 1328 Find the Outlier
//     https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1328
//


#include <bits/stdc++.h>
using namespace std;


// basic settings
long double EPS = 1e-10;  // to be set appropriately

// matrix
template<class T> struct Matrix {
    // inner value
    vector<vector<T>> val;
    
    // constructors
    Matrix(int H, int W, T x = 0) : val(H, vector<T>(W, x)) {}
    Matrix(const Matrix &mat) : val(mat.val) {}
    void init(int H, int W, T x = 0) {
        val.assign(H, vector<T>(W, x));
    }
    void resize(int H, int W) {
        val.resize(H);
        for (int i = 0; i < H; ++i) val[i].resize(W);
    }
    
    // getter and debugger
    constexpr int height() const { return (int)val.size(); }
    constexpr int width() const { return (int)val[0].size(); }
    vector<T>& operator [] (int i) { return val[i]; }
    constexpr vector<T>& operator [] (int i) const { return val[i]; }
    friend constexpr ostream& operator << (ostream &os, const Matrix<T> &mat) {
        os << endl;
        for (int i = 0; i < mat.height(); ++i) {
            for (int j = 0; j < mat.width(); ++j) {
                if (j) os << ", ";
                os << mat.val[i][j];
            }
            os << endl;
        }
        return os;
    }
    
    // comparison operators
    constexpr bool operator == (const Matrix &r) const {
        return this->val == r.val;
    }
    constexpr bool operator != (const Matrix &r) const {
        return this->val != r.val;
    }
    
    // arithmetic operators
    constexpr Matrix& operator += (const Matrix &r) {
        assert(height() == r.height());
        assert(width() == r.width());
        for (int i = 0; i < height(); ++i) {
            for (int j = 0; j < width(); ++j) {
                val[i][j] += r[i][j];
            }
        }
        return *this;
    }
    constexpr Matrix& operator -= (const Matrix &r) {
        assert(height() == r.height());
        assert(width() == r.width());
        for (int i = 0; i < height(); ++i) {
            for (int j = 0; j < width(); ++j) {
                val[i][j] -= r[i][j];
            }
        }
        return *this;
    }
    constexpr Matrix& operator *= (T v) {
        for (int i = 0; i < height(); ++i)
            for (int j = 0; j < width(); ++j)
                val[i][j] *= v;
        return *this;
    }
    constexpr Matrix& operator *= (const Matrix &r) {
        assert(width() == r.height());
        Matrix<T> res(height(), r.width());
        for (int i = 0; i < height(); ++i)
            for (int j = 0; j < r.width(); ++j)
                for (int k = 0; k < width(); ++k)
                    res[i][j] += val[i][k] * r[k][j];
        return (*this) = res;
    }
    constexpr Matrix operator + () const { return Matrix(*this); }
    constexpr Matrix operator - () const { return Matrix(*this) *= T(-1); }
    constexpr Matrix operator + (const Matrix &r) const { return Matrix(*this) += r; }
    constexpr Matrix operator - (const Matrix &r) const { return Matrix(*this) -= r; }
    constexpr Matrix operator * (T v) const { return Matrix(*this) *= v; }
    constexpr Matrix operator * (const Matrix &r) const { return Matrix(*this) *= r; }
    
    // pow
    constexpr Matrix pow(long long n) const {
        assert(height() == width());
        Matrix<T> res(height(), width()),  mul(*this);
        while (n > 0) {
            if (n & 1) res *= mul;
            mul *= mul;
            n >>= 1;
        }
        return res;
    }
    friend constexpr Matrix<T> pow(const Matrix<T> &mat, long long n) {
        return mat.pow(n);
    }
    
    // gauss-jordan
    constexpr int find_pivot(int cur_rank, int col) const {
        int pivot = -1;
        T max_v = EPS;
        for (int row = cur_rank; row < height(); ++row) {
            if (abs(val[row][col]) > max_v) {
                max_v = abs(val[row][col]);
                pivot = row;
            }
        }
        return pivot;
    }
    constexpr void sweep(int cur_rank, int col, int pivot) {
        swap(val[pivot], val[cur_rank]);
        auto fac = val[cur_rank][col];
        for (int col2 = 0; col2 < width(); ++col2) {
            val[cur_rank][col2] /= fac;
        }
        for (int row = 0; row < height(); ++row) {
            if (row != cur_rank && abs(val[row][col]) > EPS) {
                auto fac = val[row][col];
                for (int col2 = 0; col2 < width(); ++col2) {
                    val[row][col2] -= val[cur_rank][col2] * fac;
                }
            }
        }
    }
    constexpr int gauss_jordan(int not_sweep_width = 0) {
        int rank = 0;
        for (int col = 0; col < width(); ++col) {
            if (col == width() - not_sweep_width) break;
            int pivot = find_pivot(rank, col);
            if (pivot == -1) continue;
            sweep(rank++, col, pivot);
        }
        return rank;
    }
    friend constexpr int gauss_jordan(Matrix<T> &mat, int not_sweep_width = 0) {
        return mat.gauss_jordan(not_sweep_width);
    }
    friend constexpr vector<T> linear_equation(const Matrix<T> &mat, const vector<T> &b) {
        // extend
        Matrix<T> A(mat.height(), mat.width() + 1);
        for (int i = 0; i < mat.height(); ++i) {
            for (int j = 0; j < mat.width(); ++j) A[i][j] = mat.val[i][j];
            A[i].back() = b[i];
        }
        int rank = A.gauss_jordan(1);
        
        // check if it has no solution
        vector<T> res;
        for (int row = rank; row < mat.height(); ++row)
            if (abs(A[row].back()) > EPS)
                return res;

        // answer
        res.assign(mat.width(), 0);
        for (int i = 0; i < rank; ++i) res[i] = A[i].back();
        return res;
    }
};



/*/////////////////////////////*/
// Examples
/*/////////////////////////////*/

void AOJ_2171() {
    int N, s, t;
    while (cin >> N >> s >> t, N) {
        --s, --t;
        vector<int> q(N);
        vector<vector<int>> a(N, vector<int>(N));
        for (int i = 0; i < N; ++i) cin >> q[i];
        for (int i = 0; i < N; ++i) for (int j = 0; j < N; ++j) cin >> a[i][j];

        // Dijkstra
        const int INF = 1<<29;
        vector<int> dist(N, INF);
        vector<bool> seen(N, 0);
        dist[t] = 0;
        for (int iter = 0; iter < N; ++iter) {
            int curd = INF;
            int v = -1;
            for (int i = 0; i < N; ++i) {
                if (seen[i]) continue;
                if (curd > dist[i]) {
                    curd = dist[i];
                    v = i;
                }
            }
            if (v == -1) break;
            for (int w = 0; w < N; ++w) {
                if (w == v) continue;
                if (a[v][w] == 0) continue;
                dist[w] = min(dist[w], curd + a[v][w]);
            }
            seen[v] = true;
        }
        if (dist[s] >= INF) {
            cout << "impossible" << endl;
            return;
        }

        // 連立一次方程式を作る
        Matrix<double> A(N, N, 0);
        vector<double> b(N, 0);
        for (int v = 0; v < N; ++v) {
            if (v == t) {
                A[v][v] = 1;
                b[v] = 0;
            }
            else {
                vector<int> neigbor;
                for (int w = 0; w < N; ++w) {
                    if (a[v][w] == 0) continue;
                    if (q[v] == 1 && dist[w] + a[v][w] != dist[v]) continue;
                    neigbor.push_back(w);
                }
                int K = neigbor.size();
                for (auto w : neigbor) {
                    A[v][w] -= 1;
                    b[v] += a[v][w];
                }
                A[v][v] += K;
            }
        }
          
        // 解く
        auto res = linear_equation(A, b);
        if (res.empty()) cout << "impossible" << endl;
        else cout << fixed << setprecision(15) << res[s] << endl;
    }
}

void AOJ_1328() {
    using D = long double;
    EPS = 1e-5;  // set EPS
    
    auto dpow = [&](D a, int n) -> D {
        D res = 1.0;
        for (int i = 0; i < n; ++i) res *= a;
        return res;
    };
    auto func = [&](const vector<D> &coef, int i) -> D {
        D res = 0.0;
        for (int p = 0; p < (int)coef.size(); ++p)
            res += coef[p] * pow(i, p);
        return res;
    };
    
    int d;
    while (cin >> d, d) {
        vector<D> v(d + 3);
        for (int i = 0; i < d + 3; ++i) cin >> v[i];

        bool finish = false;
        int res = 0;
        for (int i = 0; i < d + 3 && !finish; ++i) {
            for (int j = i + 1; j < d + 3 && !finish; ++j) {
                Matrix<D> A(d + 1, d + 1);
                vector<D> b(d + 1);
                for (int k = 0, iter = 0; k < d+3; ++k) {
                    if (k == i || k == j) continue;
                    for (int p = 0; p < d + 1; ++p) {
                        A[iter][p] = pow(k, p);
                        b[iter] = v[k];
                    }
                    ++iter;
                }
                vector<D> ans = linear_equation(A, b);
                if (ans.empty()) continue;
                D vi = func(ans, i), vj = func(ans, j);
                int num = 0;
                if (fabs(vi - v[i]) > EPS) res = i, ++num;
                if (fabs(vj - v[j]) > EPS) res = j, ++num;
                if (num == 1) goto end;
            }
        }
        end:
        cout << res << endl;
    }
}


int main() {
    //AOJ_2171();
    AOJ_1328();
}
accuracy: 1
time_ms: 10
memory_kb: 3840
final_rank: 16

code_snippet: #include<bits/stdc++.h>
using namespace std;

using ld = long double;
using point = pair<ld,ld>;

ld compute(const vector<point>& points, const ld& x) {
    int n = points.size();

    vector<ld> vals;
    for (int i = 0; i < n; i++) {
        ld val = points[i].second;

        for (int j = 0; j < n; j++) {
            if(i == j)continue;
            val *= (x - points[j].first);
            val /= (points[i].first - points[j].first);
        }

        vals.push_back(val);
    }
    sort(vals.begin(),vals.end());

    return accumulate(vals.begin(),vals.end(), 0.0l);
}

void solve(int d) {
    ld v[d + 3];
    for (int i = 0; i < d + 3; i++) {
        cin >> v[i];
    }

    vector<pair<ld,int>> diffs;

    for (int exclude = 0; exclude < d + 3; exclude++) {
        vector<point> points;
        for (int i = 0; i < d + 3; i++) {
            if(i == exclude) continue;
            points.emplace_back(i, v[i]);
        }

        random_shuffle(points.begin(), points.end());
        points.resize(d + 1);

        ld diff = 0.0l;
        for (int i = 0; i < d + 3; i++) {
            if(i == exclude) continue;
            diff += powl(compute(points, i) - v[i], 2.0l);
        }
        diffs.emplace_back(diff, exclude);
    }

    sort(diffs.begin(), diffs.end());
    cout << diffs.front().second << endl;
}

int main() {
    int d;
    while(true) {
        cin >> d;
        if(d == 0) break;
        solve(d);
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3328
final_rank: 10

code_snippet: #include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 1000000001

double eps = 1e-2;
vector<double> add(vector<double> a,vector<double> b){
	vector<double> c(max((int)a.size(),(int)b.size()),0.0);
	rep(i,a.size())c[i] += a[i];
	rep(i,b.size())c[i] += b[i];
	return c;
}

vector<double> mul(vector<double> a,vector<double> b){
	vector<double> c(a.size()+b.size()-1,0.0);
	rep(i,a.size()){
		rep(j,b.size()){
			c[i+j] += a[i]*b[j];
		}
	}
	return c;
}

double get(vector<double> a,double x){
	double ret = 0.0;
	rep(i,a.size()){
		ret += pow(x,i) * a[i];
	}
	return ret;
}

vector<double> Find(vector<double> x,vector<double> y){
	vector<double> ret;
	
	rep(i,x.size()){
		vector<double> t(1,1.0);
		rep(j,x.size()){
			if(i==j)continue;
			t = mul(t,{-x[j],1.0});
		}
		double A = y[i];
		A /= get(t,x[i]);
		t = mul(t,{A});
		ret = add(ret,t);
	}
	return ret;
}

int main(){
	
	while(true){
		int d;
		cin>>d;
		if(d==0)break;
		vector<double> v(d+3);
		rep(i,d+3)cin>>v[i];
		
		int ans = -1;
		rep(i,d+3){
			rep(j,d+3){
				if(i==j)continue;
				vector<double> x,y;
				rep(k,d+3){
					if(i==k||j==k)continue;
					x.push_back(k);
					y.push_back(v[k]);
				}
				
				auto ret = Find(x,y);
				bool f0 = true,f1 = true;
				if(abs(get(ret,i)-v[i])<eps)f0=false;
				if(abs(get(ret,j)-v[j])<eps)f1=false;
				if(f0&&f1)continue;
				if(f0)ans = i;
				else ans = j;
				
			}
		}
		
		cout<<ans<<endl;
		
	}
	
	return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 3920
final_rank: 20

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<queue>
#include<cassert>
#include<set>

using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int> >;
using pii = pair<int, int>;
using ppi = pair<pii, int>;

typedef vector<double> vec;
typedef vector<vec> mat;

const double EPS = 1e-8;
vec gauss_jordan(const mat& A, const vec& b){
  int n = A.size();
  mat B(n, vec(n + 1));
  for(int i = 0; i < n; i++)
    for(int j = 0; j < n; j++) B[i][j] = A[i][j];
  for(int i = 0; i < n; i++) B[i][n] = b[i];
  for(int i = 0; i < n; i++){
    int pivot = i;
    for(int j = i; j < n; j++){
      if(abs(B[j][i]) > abs(B[pivot][i])) pivot = j;
    }
    swap(B[i], B[pivot]);

    if(abs(B[i][i]) < EPS) return vec();

    for(int j = i+1; j <= n; j++) B[i][j] /= B[i][i];
    for(int j = 0; j < n; j++){
      if(i != j){
        for(int k = i+1; k <= n; k++) B[j][k] -= B[j][i] * B[i][k];
      }
    }
  }
  vec x(n);
  for(int i = 0; i < n; i++) x[i] = B[i][n];
  return x;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int ti = clock();
  // start-----------------------------------------------
  int d;
  while(cin >> d, d){
    vec v(d+3);
    for(int i = 0; i < d+3; i++) cin >> v[i];
    int ans = 0;
    mat m(d+3, vec(d+2));
    for(int i = 0; i < d+3; i++){
      for(int j = 0; j < d+1; j++) m[i][j] = pow(1.0*i, j);
    }
    for(int i = 0; i < d+3; i++){
      for(int j = i+1; j < d+3; j++){
        vec b(d+1);
        mat A(d+1, vec(d+1));
        for(int k = 0, l = 0; k < d+3; k++)
          if(i != k && j != k) A[l] = m[k], b[l] = v[k], l++;
        vec x = gauss_jordan(A, b);
        double res[2] = {};
        for(int k = 0; k < d+1; k++){
          res[0] += x[k] * m[i][k];
          res[1] += x[k] * m[j][k];
        }
        if(abs(res[0] - v[i]) + 1e-5 > 1.0 && abs(res[1] - v[j]) < 1e-5) ans = i;
        if(abs(res[0] - v[i]) < 1e-5 && abs(res[1] - v[j]) + 1e-5 > 1.0) ans = j;
      }
    }
    cout << ans << endl;
  }

  // end-----------------------------------------------
  // cerr << 1.0 * (clock() - ti) / CLOCKS_PER_SEC << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3980
final_rank: 18

code_snippet: #include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
#include <unordered_map>
#include <fstream>
#include <ctime>
#include <complex>
#include <iomanip>
#include <random>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 510000;
// ll dy[8] = {0,-1,0,1,1,-1,-1,1};
// ll dx[8] = {-1,0,1,0,1,-1,1,-1};
const double pi = acos(-1);
const double eps = 1e-4;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    for(auto itr = a.begin(); itr != a.end(); itr++){
		cout << *itr << " ";
	}
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
	cout << "debug: " << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << "debug: " << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
const int mod = 1e9 + 7;
//const int mod = 998244353;

typedef vector<vector<double>> mat;

mat matinv(mat a){
    int n = a.size();
    mat b(n,vector<double>(n*2));
    rep(i,n){
        rep(j,n){
            b[i][j] = a[i][j];
        }
    }
    rep(i,n) b[i][n+i] = 1;
    rep(i,n){
        int pivot = i;
        for(int j=i; j<n; j++){
            if(abs(b[j][i]) > abs(b[pivot][i])) pivot = j;
        }
        if(abs(b[pivot][i]) < eps){
            b.clear();
            return b;
        }
        swap(b[i], b[pivot]);
        for(int j=i+1; j<2*n; j++) b[i][j] /= b[i][i];
        rep(j,n){
            if(i != j){
                for(int k=i+1; k<2*n; k++) b[j][k] -= b[j][i] * b[i][k];
            }
        }
    }
    mat ret(n,vector<double>(n));
    rep(i,n) rep(j,n) ret[i][j] = b[i][n+j];
    return ret;
}

mat matmul(mat &a, mat &b){
    mat c(a.size(), vector<double>(b[0].size()));
    for(int i=0; i<a.size(); i++){
        for(int k=0; k<b.size(); k++){
            for(int j=0; j<b[0].size(); j++){
                c[i][j] += a[i][k] * b[k][j];
            }
        }
    }
    return c;
}

int main(){
    while(1){
        int d; cin >> d;
        if(!d) break;
        vector<double> f(d+3);
        rep(i,d+3) cin >> f[i];
        bool det = false;
        int ans = -1;
        rep(i,d+3){
            rep(j,i){
                mat a(d+1,vector<double>(d+1));
                mat b(d+1,vector<double>(1));
                int id = 0;
                rep(k,d+3){
                    if(k == j || k == i) continue;
                    b[id][0] = f[k];
                    rep(l,d+1){
                        a[id][l] = pow(k,l);
                    }
                    id++;
                }
                // rep(k,d+1){
                //     rep(l,d+1) cout << a[k][l] << " ";
                //     cout << "\n";
                // }
                mat inv = matinv(a);
                // rep(k,d+1){
                //     rep(l,d+1) cout << inv[k][l] << " ";
                //     cout << "\n";
                // }
                // rep(k,d+1) cout << b[k][0] << " ";
                // cout << "\n";
                // cout << "\n";
                if(inv.empty()) continue;
                mat res = matmul(inv,b);
                auto val = [&](int t) -> double {
                    double ret = 0.0;
                    rep(k,d+1) ret += res[k][0] * pow(t,k);
                    return ret;
                };
                // rep(k,d+1) cout << res[k][0] << " ";
                // cout << "\n";
                bool ff = abs(val(i) - f[i]) > eps;
                bool gg = abs(val(j) - f[j]) > eps;
                if(ff != gg){
                    det = true;
                    if(ff) ans = i;
                    else ans = j;
                    break;
                }
            }
            if(det) break;
        }
        cout << ans << "\n";
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3428
final_rank: 14

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
//using namespace atcoder;
const double pi = 3.141592653589793238462643383279;
using namespace std;
//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;

//container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SQ(a) ((a) * (a))
#define EACH(i, c) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

//repetition
//------------------------------------------
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define MOD 1000000007

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
const double EPS = 1e-3, PI = acos(-1);
//ここから編集
bool eq(double a, double b){ return fabs(a-b) < EPS; }
long double lagrange_interpolation(vector<long double> xi, vector<long double> fi, long double x){
  int n = xi.size()-1;
  long double ret = 0.0;
  for(int i=0; i<=n; i++){
    long double t = 1.0;
    for(int j=0; j<=n; j++){
      if(i == j) continue;
      t *= (x-xi[j])/(xi[i]-xi[j]);
    }
    ret += t*fi[i];
  }
  return ret;
}

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(6);

  int d;
  while(cin >> d, d){

    vector<long double> v(d+3);
    vector<long double> x(d+3);
    REP(i,d+3) {
      cin >> v[i];
      x[i] = i;
    }
    vector<bool> ok(8, false);
    int ans = 0;
    for(int i=0; i<=d+2; i++){
      for(int j=0; j<=d+2; j++){
        vector<long double> xi, fi;
        for(int k=0; k<=d+2; k++){
          if(i == k || j == k) continue;
          xi.push_back(k);
          fi.push_back(v[k]);
        }
        if(eq(lagrange_interpolation(xi, fi, i), v[i])) ok[i]=true;
        if(eq(lagrange_interpolation(xi, fi, j), v[j])) ok[j]=true;
      }
    } 
    for(int i=0; i<=d+2; i++) if(!ok[i]) cout << i << endl;
  }
  
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3224
final_rank: 9

code_snippet: #include<bits/stdc++.h>
using namespace std;

//container utill
#define ALL(v) (v).begin(),(v).end()
#define CR [](auto element1, auto element2){return element1>element2;}
#define LB lower_bound
#define UP upper_bound
#define PB push_back
#define MP make_pair
#define MT make_tuple

using LD = long double;
const LD EPS = 1e-10;
const LD R_EPS = 1e-3;

template<class T> struct Matrix {
    vector<vector<T> > val;
    Matrix(int n, int m, T x = 0) : val(n, vector<T>(m, x)) {}
    void init(int n, int m, T x = 0) {val.assign(n, vector<T>(m, x));}
    size_t size() const {return val.size();}
    inline vector<T>& operator [] (int i) {return val[i];}
};

template<class T> ostream& operator << (ostream& s, Matrix<T> A) {
    s << endl; 
    for (int i = 0; i < A.size(); ++i) {
        for (int j = 0; j < A[i].size(); ++j) {
            s << A[i][j] << ", ";
        }
        s << endl;
    }
    return s;
}

template<class T> Matrix<T> operator * (Matrix<T> A, Matrix<T> B) {
    Matrix<T> R(A.size(), B[0].size());
    for (int i = 0; i < A.size(); ++i)
        for (int j = 0; j < B[0].size(); ++j)
            for (int k = 0; k < B.size(); ++k)
                R[i][j] += A[i][k] * B[k][j];
    return R;
}

template<class T> Matrix<T> pow(Matrix<T> A, long long n) {
    Matrix<T> R(A.size(), A.size());
    for (int i = 0; i < A.size(); ++i) R[i][i] = 1;
    while (n > 0) {
        if (n & 1) R = R * A;
        A = A * A;
        n >>= 1;
    }
    return R;
}

template<class T> int GaussJordan(Matrix<T> &A, bool is_extended = false) {
    int m = A.size(), n = A[0].size();
    int rank = 0;
    for (int col = 0; col < n; ++col) {
        if (is_extended && col == n-1) break;
        int pivot = -1;
        T ma = EPS;
        for (int row = rank; row < m; ++row) {
            if (abs(A[row][col]) > ma) {
                ma = abs(A[row][col]);
                pivot = row;
            }
        }
        if (pivot == -1) continue;
        swap(A[pivot], A[rank]);
        auto fac = A[rank][col];
        for (int col2 = 0; col2 < n; ++col2) A[rank][col2] /= fac;
        for (int row = 0; row < m; ++row) {
            if (row != rank && abs(A[row][col]) > EPS) {
                auto fac = A[row][col];
                for (int col2 = 0; col2 < n; ++col2) {
                    A[row][col2] -= A[rank][col2] * fac;
                }
            }
        }
        ++rank;
    }
    return rank;
}

template<class T> vector<T> linear_equation(Matrix<T> A, vector<T> b) {
    // extended
    int m = A.size(), n = A[0].size();
    Matrix<T> M(m, n + 1);
    for (int i = 0; i < m; ++i) {
        for (int j = 0; j < n; ++j) M[i][j] = A[i][j];
        M[i][n] = b[i];
    }
    int rank = GaussJordan(M, true);
    
    // check if it has no solution
    vector<T> res;
    for (int row = rank; row < m; ++row) if (abs(M[row][n]) > EPS) return res;

    // answer
    res.assign(n, 0);
    for (int i = 0; i < rank; ++i) res[i] = M[i][n];
    return res;
}

void nextind(int d, vector<int> &ind){
    int s=ind.size();
    while(ind[s-1]==(d+2)+(s-(d+1))){
        auto t=ind.end();
        t--;
        ind.erase(t);
        s=ind.size();
    }
    ind[s-1]++;
    while(s<d+1){
        ind.PB(ind[s-1]+1);
        s=ind.size();
    }
}

void solve(int d){
    LD f[d+3];
    int i,j,k;
    for(i=0; i<d+3; i++){
        cin >> f[i];
    }
    vector<vector<LD> > coef;
    vector<int> ind;
    for(i=0; i<d; i++){
        ind.PB(i);
    }
    ind.PB(d-1);
    vector<vector<int> > indind;
    for(i=0; i<((d+3)*(d+2))/2; i++){
        nextind(d,ind);
        indind.PB(ind);
        Matrix<LD> A(d+1,d+1);
        for(j=0; j<d+1; j++){
            int ele=1;
            for(k=0; k<d+1; k++){
                A[j][k]=ele;
                ele*=ind[j];
            }
        }
        vector<LD> b;
        for(j=0; j<d+1; j++){
            b.PB(f[ind[j]]);
        }
        coef.PB(linear_equation(A,b));
    }
    vector<bool> safe(d+3,0);
    bool now;
    for(i=0; i<((d+3)*(d+2))/2; i++){
        now=0;
        for(j=i+1; j<((d+3)*(d+2))/2; j++){
            for(k=0; k<d+1; k++){
                if(abs(coef[i][k]-coef[j][k])>R_EPS) break;
            }
            if(k<d+1) continue;
            now=1;
        }
        if(now==1){
            for(j=0; j<d+1; j++){
                safe[indind[i][j]]=1;
            }
        }
    }
    for(i=0; i<d+3; i++){
        if(safe[i]==0) break;
    }
    cout << i << endl;
}

int main(){
    int d;
    while(cin >> d, d>0){
        solve(d);
    }
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3108
final_rank: 4

code_snippet: #include <iostream>
#include <vector>
#include <array>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <string>
#include <sstream>
#include <algorithm>
#include <random>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <cassert>
#include <climits>
#include <bitset>
#include <functional>
#include <iomanip>
#include <random>

#define FOR_LT(i, beg, end) for (int i = (int)(beg); i < (int)(end); i++)
#define FOR_LE(i, beg, end) for (int i = (int)(beg); i <= (int)(end); i++)
#define FOR_DW(i, beg, end) for (int i = (int)(beg); (int)(end) <= i; i--)
#define REP(n)              for (int repeat_index = 0; repeat_index < (int)n; repeat_index++)

using namespace std;
//=============================================================================
// Matrix
//=============================================================================

template <typename T>
struct Matrix {
    Matrix(size_t m, size_t n, T x = 0) : val_(vector<vector<T>>(m, vector<T>(n, x))) {}

    void Init(size_t m, size_t n, T x = 0)
    {
        this->val_.assign(vector<vector<T>>(m, vector<T>(n, x)));
    }

    size_t size() {
        return this->val_.size();
    }

    inline vector<T>& operator [] (int i) { return this->val_[i]; }

    void print() {
        for (auto& v : val_) {
            for (auto& t : v) {
cout << t << " ";
            }
            cout << endl;
        }
    }

    vector<vector<T>> val_;
};

//=============================================================================
// REAL
//=============================================================================
static const double kEPS = 1e-10;

template<class T>
int gauss_jordan(Matrix<T>& A, bool is_extended = false) {
    int m = A.size(), n = A[0].size();

    int rank = 0;
    for (int col = 0; col < n; ++col) {
        if (is_extended && col == n - 1) break;

        int pivot = -1;
        T ma = kEPS;
        for (int row = rank; row < m; ++row) {
            if (abs(A[row][col]) > ma) {
                ma = abs(A[row][col]);
                pivot = row;
            }
        }
        if (pivot == -1) continue;

        swap(A[pivot], A[rank]);
        {
            auto fac = A[rank][col];

            for (int col2 = 0; col2 < n; ++col2) {
                A[rank][col2] /= fac;
            }
        }

        for (int row = 0; row < m; ++row) {
            if (row != rank && abs(A[row][col]) > kEPS) {
                auto fac = A[row][col];
                for (int col2 = 0; col2 < n; ++col2) {
                    A[row][col2] -= A[rank][col2] * fac;
                }
            }
        }

        ++rank;
    }

    return rank;
}


template<class T> vector<T> linear_equation(Matrix<T> A, vector<T> b)
{
    int m = A.size(), n = A[0].size();

    Matrix<T> M(m, n + 1);
    for (int i = 0; i < m; ++i) {
        for (int j = 0; j < n; ++j) M[i][j] = A[i][j];
        M[i][n] = b[i];
    }
    int rank = gauss_jordan(M, true);

    vector<T> res;
    for (int row = rank; row < m; ++row) {
        if (abs(M[row][n]) > kEPS) return res;
    }

    res.assign(n, 0);
    for (int i = 0; i < rank; ++i) res[i] = M[i][n];
    return res;
}

void solve(int d)
{
    vector<double> y(d + 3);
    FOR_LT(i, 0, d + 3) {
        cin >> y[i];
    }

    vector<int> s;
    vector<vector<int>> sc;
    vector<vector<double>> v;
    FOR_LT(i, 0, (1 << (d + 3))) {
        {
            int c = 0;
            FOR_LT(j, 0, (d + 3)) {
                if (i & (1 << j)) c++;
            }
            if (c != d + 1) continue;
        }


        Matrix<double> A(d + 1, d + 1);
        vector<double> b(d + 1);

        int c = 0;
        FOR_LT(j, 0, d + 3) {
            if (i & (1 << j)) {
                FOR_LE(k, 0, d) {
                    A[c][k] = pow(j, k);
                }
                b[c] = y[j];
                c++;
            }
		}

        vector<double> ans = linear_equation(A, b);
        assert(!ans.empty());

        {
            bool is_same = false;
            FOR_LT(j, 0, s.size()) {
                bool is_c_same = true;
                FOR_LT(k, 0, d + 1) {
                    if (abs(v[j][k] - ans[k]) >= 10E-4) {
                        is_c_same = false;
                        break;
                    }
                }
                if (is_c_same) {
                    is_same = true;
                    s[j]++;
                    FOR_LT(k, 0, d + 3) {
                        if (i & (1 << k)) {
                            sc[j][k]++;
                        }
                    }
                    break;
                }
            }
            if (!is_same) {
                s.push_back(1);
                v.push_back(ans);
                vector<int> scc(d + 3);
                FOR_LT(k, 0, d + 3) {
                    if (i & (1 << k)) {
                        scc[k]++;
                    }
                }
                sc.push_back(scc);
            }
        }
	}

    int argmax = -1;
    int maxv = 0;
    FOR_LT(i, 0, s.size()) {
        if (maxv < s[i]) {
            argmax = i;
            maxv = s[i];
        }
    }

    FOR_LT(i, 0, d + 3) {
        if (sc[argmax][i] == 0) {
            cout << i << endl;
            return;
        }
    }
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout << fixed << setprecision(20);

	while (true) {
		int d; cin >> d;
		if (d == 0) break;
		solve(d);
	}

	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3344
final_rank: 13

code_snippet: #include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>
#include <cstring>
#include <cassert>

#define rep(i, m, n) for(int i=int(m);i<int(n);i++)
#define all(c) begin(c),end(c)

template<typename T1, typename T2>
inline void chmin(T1 &a, T2 b) { if (a > b) a = b; }

template<typename T1, typename T2>
inline void chmax(T1 &a, T2 b) { if (a < b) a = b; }

typedef long long int ll;
using ll = long long int;
using ull = long long unsigned int;
using Int = long long int;
using uInt = long long unsigned int;
using Double = long double;
using namespace std;
#define INF (1 << 30) - 1
#define INFl (ll)5e15
#define DEBUG 0
#define dump(x)  cerr << #x << " = " << (x) << endl
#define MOD 1000000007


//edit
const double EPS = 1E-7;
typedef vector<Double> vec;
typedef vector<vec> mat;

//Ax = b を解く。Aは正方行列
// 解がないか、一意でない場合は長さ0の配列を返す
vec gauss_jordan(const mat &A, const vec &b) {
    int n = A.size();
    mat B(n, vec(n + 1));
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++) B[i][j] = A[i][j];
    //行列Aの後ろにbを並べ、同時に処理する。
    for (int i = 0; i < n; i++) B[i][n] = b[i];

    for (int i = 0; i < n; i++) {
        //注目している変数の係数の絶対値が大きい式をi番目に持ってくる
        int pivot = i;
        for (int j = i; j < n; j++) {
            if (abs(B[j][i]) > abs(B[pivot][i])) pivot = j;
        }
        swap(B[i], B[pivot]);

        //解がないか一意でない
        if (abs(B[i][i]) < EPS) return vec();

        //注目している変数の係数を1にする
        for (int j = i + 1; j <= n; j++) B[i][j] /= B[i][i];
        for (int j = 0; j < n; j++) {
            if (i != j) {
                //j番目の式からi番目の変数を削除
                for (int k = i + 1; k <= n; k++) B[j][k] -= B[j][i] * B[i][k];
            }
        }
    }
    vec x(n);
    for (int i = 0; i < n; i++) x[i] = B[i][n];
    return x;
}


class Solve {
public:
    Int n;
    vector<Double> v;

    vector<Double> calc(vector<int> idx) {
        vector<vector<Double>> A(idx.size(), vector<Double>(idx.size()));
        vector<Double> b(idx.size());

        for (int i = 0; i < idx.size(); ++i) {
            for (int j = 0; j < idx.size(); ++j) {
                Double tmp = pow(idx[i], j);
                A[i][j] = tmp;
            }
        }
        for (int i = 0; i < idx.size(); ++i) {
            b[i] = v[idx[i]];
        }

        auto ret = gauss_jordan(A, b);
        return ret;
    }

    bool eq_vec(vector<Double> &v1, vector<Double> &v2) {
        for (int i = 0; i < v1.size(); ++i) {
            if (abs(v1[i] - v2[i]) > 0.1) {
                return false;
            }
        }
        return true;
    }

    bool is_wrong(int p) {
        vector<int> idx1, idx2;
        for (int i = 0; i < v.size(); ++i) {
            if (i != p) {
                idx1.push_back(i);
                idx2.push_back(i);
            }
        }
        idx1.pop_back();
        idx2.erase(idx2.begin());

        auto x1 = calc(idx1);
        auto x2 = calc(idx2);

        bool ret = eq_vec(x1, x2);
        return ret;
    }

    bool solve() {
        cin >> n;
        if (n == 0) return false;
        v.resize(n + 3);
        rep(i, 0, n + 3) cin >> v[i];

        int ans = -1;
        for (int i = 0; i <= n + 2; ++i) {
            if (is_wrong(i)) {
                ans = i;
            }
        }
        cout << ans << endl;

        return true;
    }
};


int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
//    std::ifstream in("input.txt");
//    std::cin.rdbuf(in.rdbuf());

    while (Solve().solve());


    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3392
final_rank: 11

code_snippet: #define  _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#pragma comment (linker, "/STACK:526000000")

#include "bits/stdc++.h"

using namespace std;
typedef string::const_iterator State;
#define eps 1e-5L
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL
#define seg_size 262144LL
#define MOD 998244353LL
#define pb push_back
#define mp make_pair
typedef long long ll;
#define REP(a,b) for(long long (a) = 0;(a) < (b);++(a))
#define ALL(x) (x).begin(),(x).end()

// geometry library

typedef complex<long double> Point;
typedef pair<complex<long double>, complex<long double>> Line;

typedef struct Circle {
    complex<long double> center;
    long double r;
}Circle;

//内積、 dot(a,b) = |a||b|cos()
long double dot(Point a, Point b) {
    return (a.real() * b.real() + a.imag() * b.imag());
}
//外積、cross(a,b) = |a||b|sin()
long double cross(Point a, Point b) {
    return (a.real() * b.imag() - a.imag() * b.real());
}

//線分と点の距離
long double Dist_Line_Point(Line a, Point b) {
    if (dot(a.second - a.first, b - a.first) < eps) return abs(b - a.first);
    if (dot(a.first - a.second, b - a.second) < eps) return abs(b - a.second);
    return abs(cross(a.second - a.first, b - a.first)) / abs(a.second - a.first);
}

//線分の交差判定
int is_intersected_ls(Line a, Line b) {
    return (cross(a.second - a.first, b.first - a.first) * cross(a.second - a.first, b.second - a.first) < 0) &&
        (cross(b.second - b.first, a.first - b.first) * cross(b.second - b.first, a.second - b.first) < 0);
}

//線分の交点
Point intersection_l(Line a, Line b) {
    Point da = a.second - a.first;
    Point db = b.second - b.first;
    return a.first + da * cross(db, b.first - a.first) / cross(db, da);
}

//線分と線分の距離
long double Dist_Line_Line(Line a, Line b) {
    if (is_intersected_ls(a, b) == 1) {
        return 0;
    }
    return min({ Dist_Line_Point(a,b.first), Dist_Line_Point(a,b.second),Dist_Line_Point(b,a.first),Dist_Line_Point(b,a.second) });
}

//円と円の交点
pair<Point, Point> intersection_Circle_Circle(Circle a, Circle b) {
    long double dist = abs(a.center - b.center);
    assert(dist <= eps + a.r + b.r);
    assert(dist+eps >= abs(a.r - b.r));
    Point target = b.center - a.center;
    long double pointer = target.real() * target.real() + target.imag() * target.imag();
    long double aa = pointer + a.r * a.r - b.r * b.r;
    aa /= 2.0L;
    Point l{ (aa * target.real() + target.imag() * sqrt(pointer * a.r * a.r - aa * aa))/pointer,
            (aa* target.imag() - target.real() * sqrt(pointer * a.r * a.r - aa * aa)) / pointer};
    Point r{ (aa * target.real() - target.imag() * sqrt(pointer * a.r * a.r - aa * aa)) / pointer,
        (aa * target.imag() + target.real() * sqrt(pointer * a.r * a.r - aa * aa)) / pointer };
    r = r + a.center;
    l = l + a.center;
    return mp(l, r);
}

//end of geometry



void init() {
    iostream::sync_with_stdio(false);
    cout << fixed << setprecision(20);
}

unsigned long xor128() {
    static unsigned long x = 123456789, y = 362436069, z = 521288629, w = 88675123;
    unsigned long t = (x ^ (x << 11));
    x = y; y = z; z = w;
    return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}

#define int ll

template<typename A>
vector<A> solve_algebra_equations(vector<vector<A>> now) {
    for (int q = 0; q < now.size(); ++q) {
        if (now[q][q] == 0) {
            for (int j = q + 1; j < now.size(); ++j) {
                if (now[j][q] != 0) {
                    swap(now[j], now[q]);
                    break;
                }
            }
        }
        for (int j = now[q].size() - 1; j >= 0; --j) {
            now[q][j] /= now[q][q];
        }
        for (int j = 0; j < now.size();++j) {
            if (j == q) continue;
            A val = now[j][q];
            for (int t = 0; t < now[j].size(); ++t) {
                now[j][t] -= val * now[q][t];
            }
        }
    }
    vector<A> ans;
    REP(i, now.size()) {
        ans.push_back(now[i].back());
    }
    return ans;
}

void solve(){
    while (true) {
        int d;
        cin >> d;
        if (d == 0) return;
        vector<long double> inputs;
        REP(i, d + 3) {
            long double a;
            cin >> a;
            inputs.push_back(a);
        }
        int ans = -1;
        REP(i, inputs.size()) {
            for (int q = i + 1; q < inputs.size(); ++q) {
                vector<vector<long double>> neko;
                vector<vector<long double>> check;
                REP(j, inputs.size()) {
                    vector<long double> now;
                    now.push_back(inputs[j]);
                    long double geko = 1;
                    for (int t = 0; t <= d; ++t) {
                        now.push_back(geko);
                        geko *= j;
                    }
                    reverse(ALL(now));
                    if (i == j || q == j) {
                        check.push_back(now);
                    }
                    else {
                        neko.push_back(now);
                    }
                 }
                vector<long double>  geko = solve_algebra_equations<long double>(neko);
                REP(t, 2) {
                    long double now_check = 0;
                    for (int q = 0; q < geko.size(); ++q) {
                        now_check += geko[q] * check[t][q];
                    }
                    if (abs(now_check - check[t].back()) < eps) {
                        if (t == 0) {
                            ans = q;
                        }
                        else {
                            ans = i;
                        }
                        break;
                    }
                }
            }
            if (ans != -1) break;
        }
        cout << ans << endl;
    }
}

#undef int
int main() {
    init();
    solve();
}
accuracy: 1
time_ms: 10
memory_kb: 3204
final_rank: 5

code_snippet: #include <bits/stdc++.h>
double lagrange_interpolation(std::vector<double> x, std::vector<double> y, double T) {
    const long long n = x.size() - 1;
    double ret = 0;
    for(int i=0;i<n+1;++i) {
        double t = 1;
        for(int j=0;j<n+1;++j) {
            if(i == j) continue;
            t *= T-x[j];
            t /= x[i]-x[j];
        }
        ret += t * y[i];
    }
    return ret;
}
std::vector<double> lagrange_interpolation(std::vector<double> x, std::vector<double> y) {
    const long long n = x.size() - 1;
    const double eps = 1e-6;
    for(int i=0;i<n+1;++i) {
        double t = 1;
        for(int j=0;j<n+1;++j) {
          if(i != j) t *= x[i]-x[j];
        }
        y[i] /= t;
    }
    long long cur = 0, nxt = 1;
    std::vector<std::vector<double>> dp(2, std::vector<double>(n+2));
    dp[0][0] = -1 * x[0], dp[0][1] = 1;
    for(int i=1;i<n+1;++i) {
        for(int j=0;j<n+2;++j) {
            dp[nxt][j] = dp[cur][j] * -1 * x[i];
            if(j >= 1) dp[nxt][j] += dp[cur][j-1];
        }
        std::swap(nxt, cur);
    }
    std::vector<double> ret(n+1);
    for(int i=0;i<n+1;++i) {
        if(y[i]==0) continue;
        if(abs(x[i]) < eps) {
            for(int j=0;j<n+1;++j) ret[j] += dp[cur][j+1] * y[i];
        } else {
            ret[0] -= dp[cur][0] / x[i] * y[i];
            double pre = -1 * dp[cur][0] / x[i];
            for(int j=1;j<n+1;++j) {
                ret[j] -= (dp[cur][j] - pre) / x[i] * y[i];
                pre = -1 * (dp[cur][j] - pre) / x[i];
            }
        }
    }
    return ret;
}
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
const double eps = 1e-4;
const ll MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1ll<<50;
template<typename T>
void printv(const vector<T>& s) {
  for(int i=0;i<(int)(s.size());++i) {
    cout << s[i];
    if(i == (int)(s.size())-1) cout << endl;
    else cout << " ";
  }
}
template<typename T1, typename T2>
ostream& operator<<(ostream &os, const pair<T1, T2> p) {
  os << p.first << ":" << p.second;
  return os;
}
bool operator== (const vector<double> &v1, const vector<double> &v2) {
  if((int)(v1.size()) == (int)(v2.size())) {
    bool res = true;
    for(int i=0;i<(int)(v1.size());++i) {
      res &= abs(v1[i] - v2[i]) < eps;
    }
    return res;
  } else {
    return false;
  }
}
double err (const vector<double> &v1, const vector<double> &v2) {
  double res = 0;
  for(int i=0;i<(int)(v1.size());++i) {
    res += abs(v1[i] - v2[i]);
  }
  return res;
}
void solve(int d) {
  vector<double> x, y;
  for(int i=0;i<d+3;++i) {
    x.push_back(i);
    double tmp; cin >> tmp;
    y.push_back(tmp);
  }
  vector<pair<double, int>> vp;
  for(int i=0;i<d+3;++i) {
    vector<vector<double>> v;
    for(int j=0;j<d+3;++j) {
      if(j == i) continue;
      vector<double> nx, ny;
      for(int k=0;k<d+3;++k) {
        if(k == i || k == j) continue;
        nx.push_back(x[k]);
        ny.push_back(y[k]);
      }
      v.emplace_back(lagrange_interpolation(nx, ny));
    }
    bool ok = true;
    double ma = 0;
    for(int j=0;j<d+2;++j) {
      ma = max(ma, err(v[j], v[(j+1)%(d+2)]));
    }
    vp.push_back({ma, i});
  }
  sort(vp.begin(), vp.end());
  cout << vp[0].second << endl;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  while(1) {
    int d; cin >> d;
    if(d == 0) break;
    solve(d);
  }
}
accuracy: 1
time_ms: 20
memory_kb: 3220
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
using LL = long long;

#define fs first
#define sc second

const LL MOD = 1e9+7;
const double EPS = 1e-10;

template<class T>bool chmax(T &a, const T &b) {
    if (a < b) { a = b; return true; }
    return false;
}
template<class T>bool chmin(T &a, const T &b) {
    if (b < a) { a = b; return true; }
    return false;
}

// std::vector Declaration
template<typename T>
vector<T> make_v(size_t a) { return vector<T>(a); }
template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts) {
    return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

// std::vector Declaration and Initialization
template<typename T>
vector<T> make_vector(size_t a, T x) { return vector<T>(a, x); }
template<typename T, typename U, typename... Ts>
auto make_vector(size_t a, U b, Ts... ts) {
    return vector<decltype(make_vector<T>(b,ts...))>(a,make_vector<T>(b,ts...));
}

// std::vector Input
template<typename T>
istream& operator>>(istream& is, vector<T>& v) {
    for (auto &e : v) {
        is >> e;
    }
    return is;
}

// std::vector Debug
template<typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    os << "[";
    bool a = 1;
    for (auto e : v) {
        os << (a ? "" : " ");
        os << e;
        a = 0;
    }
    os << "]";
    return os;
}

// std::array Debug
template<typename T, size_t n>
ostream& operator<<(ostream& os, const array<T, n>& v) {
    os << "[";
    bool a = 1;
    for (auto e : v) {
        os << (a ? "" : " ");
        os << e;
        a = 0;
    }
    os << "]";
    return os;
}

// std::deque Debug
template<typename T>
ostream& operator<<(ostream& os, const deque<T>& d) {
    os << "[";
    bool a = 1;
    for (auto e : d) {
        os << (a ? "" : " ");
        os << e;
        a = 0;
    }
    os << "]";
    return os;
}

// std::pair Debug
template<typename T, typename U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
    os << "(" << p.first << " " << p.second << ")";
    return os;
}

// std::set Debug
template<typename T>
ostream& operator<<(ostream& os, const set<T>& st) {
    os << "{";
    bool a = 1;
    for (auto e : st) {
        os << (a ? "" : " ");
        os << e;
        a = 0;
    }
    os << "}";
    return os;
}

// std::multiset Debug
template<typename T>
ostream& operator<<(ostream& os, const multiset<T>& st) {
    os << "{";
    bool a = 1;
    for (auto e : st) {
        os << (a ? "" : " ");
        os << e;
        a = 0;
    }
    os << "}";
    return os;
}

// std::map Debug
template<typename T, typename U>
ostream& operator<<(ostream& os, const map<T, U>& mp) {
    os << "{";
    bool a = 1;
    for (auto e : mp) {
        os << (a ? "" : " ");
        os << e.first << ":" << e.second;
        a = 0;
    }
    os << "}";
    return os;
}

// std::tuple Debug
template<int N, class Tuple>
void out(ostream& os, const Tuple& t){}
template<int N, class Tuple, class H, class ...Ts>
void out(ostream& os, const Tuple& t) {
    if (N) os << " ";
    os << get<N>(t);
    out<N+1,Tuple,Ts...>(os, t);
}
template<class ...Ts>
ostream& operator<<(ostream& os, const tuple<Ts...>& t) {
    os << "(";
    out<0,tuple<Ts...>,Ts...>(os, t);
    os << ")";
    return os;
}

// Debug
#define DUMP(x) cerr<<#x<<" = "<<(x)<<endl

// Weighted edge
template<typename T>
struct edge {
    int src, to;
    T cost;

    edge(int to, T cost) : src(-1), to(to), cost(cost) {}
    edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}

    edge &operator=(const int &x) {
        to = x;
        return *this;
    }

    operator int() const { return to; }

    friend ostream& operator<<(ostream& os, const edge& e) {
        return os << "(" << e.src << "->" << e.to << ":" << e.cost << ")";
    }
};

template<typename T>
using Edges = vector<edge<T>>;
template<typename T>
using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
template<typename T>
using MatrixGraph = vector<vector<T>>;

using Matrix = vector<vector<double>>;

pair<Matrix,Matrix> LUDecomposition(const Matrix& A) {
    // A: Squere Matirix
    int n = A.size();
    const double INF = numeric_limits<double>::max();
    Matrix L = make_v<double>(n, n),
           U = make_v<double>(n, n);

    for (int i = 0; i < n; ++i) {
        L[i][i] = 1;
    }

    for (int i = 0; i < n; ++i) {
        // L
        for (int j = 0; j < i; ++j) {
            double total = 0;
            for (int k = 0; k < j; ++k) {
                total += L[i][k] * U[k][j];
            }
            L[i][j] = (A[i][j] - total) / U[j][j];
        }

        // U
        for (int j = i; j < n; ++j) {
            double total = 0;
            for (int k = 0; k < i; ++k) {
                total += L[i][k] * U[k][j];
            }
            U[i][j] = A[i][j] - total;
        }
    }

    return make_pair(L, U);
}

using Vector = vector<double>;

Vector solve(const Matrix& A, const Vector& b) {
    // A: Squere Matrix
    int n = A.size();
    Matrix L, U;
    tie(L, U) = LUDecomposition(A);

    Vector y(n);
    for (int i = 0; i < n; ++i) {
        double total = 0;
        for (int j = 0; j < i; ++j) {
            total += y[j] * L[i][j];
        }
        y[i] = b[i] - total;
    }

    Vector x(n);
    for (int i = n-1; i >= 0; --i) {
        double total = 0;
        for (int j = n-1; j > i; --j) {
            total += U[i][j] * x[j];
        }
        x[i] = (y[i] - total) / U[i][i];
    }

    return x;
}

int main()
{
    while (true) {
        int d; cin >> d;
        if (d == 0) {
            break;
        }

        vector<double> v(d+3); cin >> v;
        vector<double> ans(d+3);

        for (int m = 0; m < d+3; ++m) {

            int check = (d+1+m) % (d+3);

            Matrix A = make_v<double>(d+1, d+1);
            for (int i = 0; i <= d; ++i) {
                for (int k = 0; k <= d; ++k) {
                    A[i][k] = pow((i+m) % (d+3), k);
                }
            }

            Vector b(d+1);
            for (int i = 0; i <= d; ++i) {
                b[i] = v[(i+m) % (d+3)];
            }

            Vector a = solve(A, b);

            double sum = 0;
            for (int i = 0; i <= d; ++i) {
                sum += a[i] * pow(check, i);
            }

            ans[(check+1) % (d+3)] = abs(sum - v[check]);
        }

        cout << distance(ans.begin(), min_element(ans.begin(), ans.end())) << endl;
    }

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3228
final_rank: 6

code_snippet: #include <iostream>
#include <cmath>
#include <vector>

using Bool = bool;
using Int = long long int;
using Real = long double;
template <class T>
using Vector = std::vector<T>;

constexpr Real EPS = 1e-3;

// pol * (x - d)
Vector<Real> mul(Vector<Real> pol, Real d) {
    pol.insert(pol.begin(), 0);
    for (Int i = 0; i + 1 < pol.size(); ++i) {
        pol[i] -= pol[i + 1] * d;
    }
    return pol;
}

// pol / d
Vector<Real> divc(Vector<Real> pol, Real d) {
    for (auto& a : pol) a /= d;
    return pol;
}

Vector<Real> add(Vector<Real> pol1, Vector<Real> pol2) {
    for (Int i = 0; i < pol1.size(); ++i) {
        pol1[i] += pol2[i];
    }
    return pol1;
}

Real apply(const Vector<Real> pol, Real x) {
    Real ret = 0, acc = 1;
    for (auto a : pol) {
        ret += a * acc;
        acc *= x;
    }
    return ret;
}

Bool solve() {
    Int n;
    std::cin >> n;
    if (n == 0) return false;

    Vector<Real> v(n + 3);
    for (auto& a : v) std::cin >> a;

    for (Int i = 0; i <= n + 1; ++i) {
        // v[i]が偽と仮定
        Vector<Real> res(n + 1, 0);

        for (Int j = 0; j <= n + 1; ++j) {
            if (j == i) continue;

            Vector<Real> pol(1, v[j]);
            for (Int k = 0; k <= n + 1; ++k) {
                if (k == i || k == j) continue;
                pol = mul(pol, k);       // * (x - k)
                pol = divc(pol, j - k);  // / (j - k)
            }
            res = add(res, pol);
        }

        Real err = apply(res, Real(n + 2)) - v[n + 2];
        if (std::abs(err) < EPS) {
            std::cout << i << std::endl;
            return true;
        }
    }

    std::cout << n + 2 << std::endl;
    return true;
}

int main() {
    while (solve()) {}
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3164
final_rank: 2

code_snippet: #include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
#pragma warning (disable: 4996)

long double Gauss(vector<vector<long double>>a, vector<long double>b) {
	int N = a.size();
	if (N == 1) return b[0] / a[0][0];

	vector<vector<long double>> c = a;
	vector<long double> d = b;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) c[i][j] = a[i][j] / a[i][0];
		d[i] = b[i] / a[i][0];
	}

	vector<vector<long double>> e(N - 1, vector<long double>(N - 1, 0));
	vector<long double> f(N - 1, 0);

	for (int i = 0; i < N - 1; i++) {
		for (int j = 0; j < N - 1; j++) {
			e[i][j] = c[i + 1][j + 1] - c[i][j + 1];
		}
		f[i] = d[i + 1] - d[i];
	}

	return Gauss(e, f);
}

vector<long double> Gauss_Erase(vector<vector<long double>> e, vector<long double>f) {
	int N = e.size(); vector<long double> res;

	for (int i = 0; i < N; i++) {
		long double T = Gauss(e, f);
		res.push_back(T);
		
		vector<vector<long double>> a(e.size() - 1, vector<long double>(e.size() - 1, 0));
		vector<long double> b(e.size() - 1, 0);
		
		for (int j = 0; j < N - 1 - i; j++) {
			for (int k = 0; k < N - 1 - i; k++) a[j][k] = e[j][k];
			b[j] = f[j] - T * e[j][e.size() - 1];
		}
		e = a;
		f = b;
	}

	reverse(res.begin(), res.end());
	return res;
}

vector<long double> getans(vector<pair<long double, long double>> J) {
	int N = J.size();
	vector<vector<long double>> a(N, vector<long double>(N, 0));
	vector<long double> b(N, 0);

	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) a[i][j] = 1.0L * pow(J[i].first, 1.0L*(N - 1 - j));
		b[i] = J[i].second;
	}
	
	vector<long double> ret = Gauss_Erase(a, b);
	return ret;
}

bool different(vector<long double> A, vector<long double> B) {
	for (int i = 0; i < A.size(); i++) {
		if (fabs(A[i] - B[i]) > 1e-2) return true;
	}
	return false;
}

int solve(vector<long double> S) {
	int N = S.size() - 3;
	for (int i = 0; i <= N + 2; i++) {
		vector<long double> goal; bool flag = false;
		for (int j = 0; j <= N + 2; j++) {
			if (i == j) continue;
			vector<pair<long double, long double>> G;
			for (int k = 0; k <= N + 2; k++) {
				if (k != i && k != j) G.push_back(make_pair(1.0L * k + 1.0L, S[k]));
			}
			vector<long double> V = getans(G);
			if (goal.size() == 0) goal = V;
			else if (different(goal, V) == true) flag = true;
		}
		if (flag == false) return i;
	}
	return -1;
}

int main() {
	while (true) {
		int N; cin >> N; if (N == 0) break;
		vector<long double> A(N + 3, 0);
		for (int i = 0; i < N + 3; i++) cin >> A[i];

		int ans = solve(A);
		cout << ans << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 3456
final_rank: 19

code_snippet: #include <iostream>
#include <vector>
#include <cmath>
using namespace std;

// Gauss Jordan
using D = long double;
const D EPS = 1e-5;
template<class T> struct Matrix {
    vector<vector<T> > val;
    Matrix(int n, int m, T x = 0) : val(n, vector<T>(m, x)) {}
    void init(int n, int m, T x = 0) {val.assign(n, vector<T>(m, x));}
    size_t size() const {return val.size();}
    inline vector<T>& operator [] (int i) {return val[i];}
};

template<class T> int GaussJordan(Matrix<T> &A, bool is_extended = false) {
    int m = A.size(), n = A[0].size();
    int rank = 0;
    for (int col = 0; col < n; ++col) {
        if (is_extended && col == n-1) break;
        int pivot = -1;
        T ma = EPS;
        for (int row = rank; row < m; ++row) {
            if (abs(A[row][col]) > ma) {
                ma = abs(A[row][col]);
                pivot = row;
            }
        }
        if (pivot == -1) continue;
        swap(A[pivot], A[rank]);
        auto fac = A[rank][col];
        for (int col2 = 0; col2 < n; ++col2) A[rank][col2] /= fac;
        for (int row = 0; row < m; ++row) {
            if (row != rank && abs(A[row][col]) > EPS) {
                auto fac = A[row][col];
                for (int col2 = 0; col2 < n; ++col2) {
                    A[row][col2] -= A[rank][col2] * fac;
                }
            }
        }
        ++rank;
    }
    return rank;
}

template<class T> vector<T> linear_equation(Matrix<T> A, vector<T> b) {
    int m = A.size(), n = A[0].size();
    Matrix<T> M(m, n + 1);
    for (int i = 0; i < m; ++i) {
        for (int j = 0; j < n; ++j) M[i][j] = A[i][j];
        M[i][n] = b[i];
    }
    int rank = GaussJordan(M, true);
    vector<T> res;
    for (int row = rank; row < m; ++row) if (abs(M[row][n]) > EPS) return res;
    res.assign(n, 0);
    for (int i = 0; i < rank; ++i) res[i] = M[i][n];
    return res;
}




D pow(D a, int n) {
    D res = 1.0;
    for (int i = 0; i < n; ++i) res *= a;
    return res;
}

D func(const vector<D> &coef, int i) {
    D res = 0.0;
    for (int p = 0; p < coef.size(); ++p) res += coef[p] * pow(i, p);
    return res;
}

int main() {
    int d;
    while (cin >> d, d) {
        vector<D> v(d+3);
        for (int i = 0; i < d+3; ++i) cin >> v[i];

        int res = 0;
        for (int i = 0; i < d+3; ++i) {
            for (int j = i+1; j < d+3; ++j) {
                Matrix<D> A(d+1, d+1);
                vector<D> b(d+1);
                for (int k = 0, iter = 0; k < d+3; ++k) {
                    if (k == i || k == j) continue;
                    for (int p = 0; p < d+1; ++p) {
                        A[iter][p] = pow(k, p);
                        b[iter] = v[k];
                    }
                    ++iter;
                }
                vector<D> ans = linear_equation(A, b);
                if (ans.empty()) continue;
                D vi = func(ans, i), vj = func(ans, j);
                int num = 0;
                if (fabs(vi - v[i]) > EPS) res = i, ++num;
                if (fabs(vj - v[j]) > EPS) res = j, ++num;
                if (num == 1) goto end;
            }
        }
    end:
        cout << res << endl;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3172
final_rank: 3

code_snippet: #include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <iomanip>
#include <algorithm>
using namespace std;
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl


// Gauss Jordan
using D = long double;
const D EPS = 1e-5;
template<class T> struct Matrix {
    vector<vector<T> > val;
    Matrix(int n, int m, T x = 0) : val(n, vector<T>(m, x)) {}
    void init(int n, int m, T x = 0) {val.assign(n, vector<T>(m, x));}
    size_t size() const {return val.size();}
    inline vector<T>& operator [] (int i) {return val[i];}
};

template<class T> ostream& operator << (ostream& s, Matrix<T> A) {
    s << endl; 
    for (int i = 0; i < A.size(); ++i) {
        for (int j = 0; j < A[i].size(); ++j) {
            s << A[i][j] << ", ";
        }
        s << endl;
    }
    return s;
}

template<class T> int GaussJordan(Matrix<T> &A, bool is_extended = false) {
    int m = A.size(), n = A[0].size();
    int rank = 0;
    for (int col = 0; col < n; ++col) {
        if (is_extended && col == n-1) break;
        int pivot = -1;
        T ma = EPS;
        for (int row = rank; row < m; ++row) {
            if (abs(A[row][col]) > ma) {
                ma = abs(A[row][col]);
                pivot = row;
            }
        }
        if (pivot == -1) continue;
        swap(A[pivot], A[rank]);
        auto fac = A[rank][col];
        for (int col2 = 0; col2 < n; ++col2) A[rank][col2] /= fac;
        for (int row = 0; row < m; ++row) {
            if (row != rank && abs(A[row][col]) > EPS) {
                auto fac = A[row][col];
                for (int col2 = 0; col2 < n; ++col2) {
                    A[row][col2] -= A[rank][col2] * fac;
                }
            }
        }
        ++rank;
    }
    return rank;
}

template<class T> vector<T> linear_equation(Matrix<T> A, vector<T> b) {
    int m = A.size(), n = A[0].size();
    Matrix<T> M(m, n + 1);
    for (int i = 0; i < m; ++i) {
        for (int j = 0; j < n; ++j) M[i][j] = A[i][j];
        M[i][n] = b[i];
    }
    int rank = GaussJordan(M, true);
    vector<T> res;
    for (int row = rank; row < m; ++row) if (abs(M[row][n]) > EPS) return res;
    res.assign(n, 0);
    for (int i = 0; i < rank; ++i) res[i] = M[i][n];
    return res;
}




D pow(D a, int n) {
    D res = 1.0;
    for (int i = 0; i < n; ++i) res *= a;
    return res;
}

D func(const vector<D> &coef, int i) {
    D res = 0.0;
    for (int p = 0; p < coef.size(); ++p) res += coef[p] * pow(i, p);
    return res;
}

int main() {
    int d;
    while (cin >> d, d) {
        vector<D> v(d+3);
        for (int i = 0; i < d+3; ++i) cin >> v[i];

        int res = 0;
        for (int i = 0; i < d+3; ++i) {
            for (int j = i+1; j < d+3; ++j) {
                Matrix<D> A(d+1, d+1);
                vector<D> b(d+1);
                for (int k = 0, iter = 0; k < d+3; ++k) {
                    if (k == i || k == j) continue;
                    for (int p = 0; p < d+1; ++p) {
                        A[iter][p] = pow(k, p);
                        b[iter] = v[k];
                    }
                    ++iter;
                }
                vector<D> ans = linear_equation(A, b);
                if (ans.empty()) continue;
                D vi = func(ans, i), vj = func(ans, j);
                int num = 0;
                if (fabs(vi - v[i]) > EPS) res = i, ++num;
                if (fabs(vj - v[j]) > EPS) res = j, ++num;
                if (num == 1) goto end;
            }
        }
    end:
        cout << res << endl;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3160
final_rank: 1

code_snippet: #include <bits/stdc++.h>
#define int long long
using namespace std;
using ld = long double;

template <class T>
vector<T> solve(const vector<vector<T>> &A, const vector<T> &y) {
    int N = y.size();
    vector<T> x(N, 0);
    vector<vector<T>> tmp(N, vector<T>(N * 2, 0));
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
            tmp[i][j] = A[i][j];
    for (int i = 0; i < N; i++)
        tmp[i][N + i] = 1;
    
    for (int i = 0; i < N; i++) {
        if (tmp[i][i] == 0) tmp[i][i] += 1e-10;
        T sc = tmp[i][i];
        for (int k = 0; k < N * 2; k++)
            tmp[i][k] /= sc;
        for (int j = 0; j < N; j++) {
            if (i == j) continue;
            sc = tmp[j][i];
            for (int k = 0; k < N * 2; k++) {
                tmp[j][k] -= tmp[i][k] * sc;
            }
        }
    }

    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            x[i] += tmp[i][N + j] * y[j];
        }
    }
    return x;
}

signed main() {
    while (1) {
        int d;
        cin >> d;
        if (d == 0) break;
        vector<ld> v(d + 3);
        for (int i = 0; i < d + 3; i++) {
            cin >> v[i];
        }
        
        for (int i = 0; i < d + 3; i++) {
            for (int l = i + 1; l < d + 3; l++) {
                vector<ld> y;
                y.reserve(d + 2);
                auto p = y;
                for (int j = 0; j < d + 3; j++) {
                    if (i == j || l == j) continue;
                    y.push_back(v[j]);
                    p.push_back(j);
                }
                vector<vector<ld>> A(d + 1, vector<ld>(d + 1, 0));
                for (int j = 0; j < d + 1; j++) {
                    ld x = p[j];
                    for (ld k = 0; k < d + 1; k++) {
                        A[j][k] = pow(x, k);
                    }
                }
                
                auto value = [&](const vector<ld> &c, ld p) {
                    ld ret = 0;
                    for (int k = 0; k < c.size(); k++) {
                        ret += c[k] * pow(p, k);
                    }
                    return ret;
                };

                auto c = solve(A, y);

                int cnt = 0;
                int idx = 0;
                for (int j = 0; j < d + 3; j++) {
                    if (abs(value(c, j) - v[j]) > 1e-4) {
                        cnt++;
                        idx = j;
                    }
                }
                if (cnt == 1) {
                    cout << idx << endl;
                    goto next_case;
                }
            }
        }
        next_case:;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3420
final_rank: 12

code_snippet: #include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 998244353;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-3;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
typedef pair<ld, ld> LDP;

typedef vector<ld> vec;
typedef vector<vec> mat;
void debag_matrix(const mat &A) {
	rep(i, A.size()) {
		rep(j, A[i].size()){
			cout << A[i][j] << " ";
	    }
		cout << endl;
	}
}
void gauss_hakidasi(mat &A) {
	int n = A.size(), m = A[0].size();
	int t = min(n, m);
	rep(k, t) {
		int chk = -1;
		Rep(i,k, n) {
			if (abs(A[i][k]) > eps) {
				chk = i; break;
			}
		}
		if (chk < 0)return;
		swap(A[k], A[chk]);
		ld x = A[k][k];
		rep(i, m) {
			A[k][i] /= x;
		}
		rep(i, n) {
			if (i == k)continue;
			x = A[i][k];
			rep(j, m) {
				A[i][j] -= x * A[k][j];
			}
		}
	}
}
int n;
ld d[10];

bool valid(int id, mat A) {
	gauss_hakidasi(A);
	//debag_matrix(A);
	ld sum = 0;
	ld cur = 1;
	per(i, n + 1) {
		sum += A[i][n+1] * cur; cur *= id;
	}
	return abs(sum - d[id]) < eps;
}
int main() {
	while (cin >> n, n) {
		rep(i, n + 3)cin >> d[i];
		rep(i, n + 3) {
			mat A; int ck = 0;
			per(j, n + 3) {
				if (i == j)continue;
				vector<ld> v;
				ld cur = 1;
				rep(k, n + 1) {
					v.push_back(cur);
					cur *= j;
				}
				reverse(v.begin(), v.end());
				v.push_back(d[j]);
				if (A.size() < n + 1)A.push_back(v);
				else ck = j;
			}
			//debag_matrix(A);
			//cout << ck << endl;
			if (valid(ck, A)) {
				cout << i << endl; break;
			}

		}
		//cout << "finish" << endl;
	}
	//stop
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3284
final_rank: 7