先处理成上三角矩阵,然后消元
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define N 1005
#define M 505
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MAX(a,b) ((a) > (b) ? (a) : (b))
const int INF = 1<<30;
const double eps = 1e-6;
int n,m;
double a[N][M],x[M];
void swapLine(int i,int j){
if(i == j) return;
for(int k = 0;k <= m;k++) swap(a[i][k],a[j][k]);
}
void print_a(){
printf("*********************\n");
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++) printf("%lf ", a[i][j]);
printf("%lf\n", a[i][m]);
}
}
int gauess(){
for(int i = 0;i < m;i++){
bool flag = false;
for(int j = i;j < n;j++)
if(fabs(a[j][i]) > eps){
flag = true;
swapLine(i,j);
break;
}
if(!flag) return 2;
for(int j = i+1;j < n;j++){
double tmp = a[j][i]/a[i][i];
//printf("tmp:%lf\n", tmp);
for(int k = 0;k <= m;k++)
a[j][k]=a[j][k]-a[i][k]*tmp;
}
//print_a();
}
for(int i = 0;i < n;i++){
bool allZero = true;
for(int j = 0;j < m;j++)
if(fabs(a[i][j]) > eps){
allZero = false;
break;
}
if(allZero && fabs(a[i][m]) > eps) return 0;
}
for(int i = m-1;i >= 0;i--){
double tmp = a[i][m];
for(int j = i+1;j < m;j++)
tmp -= a[i][j]*x[j];
x[i] = tmp/a[i][i];
}
return 1;
}
int main(){
while(~scanf("%d %d",&m,&n)){
for(int i = 0;i < n;i++)
for(int j = 0;j <= m;j++) scanf("%lf",&a[i][j]);
int ans = gauess();
if(ans != 1)
printf("%s\n", ans ? "Many solutions" : "No solutions");
else
for(int i = 0;i < m;i++)
printf("%d\n", (int)(x[i]+0.5));
}
return 0;
}