1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| #include <bits/stdc++.h> using namespace std; typedef pair<long long, long long> pll; typedef long long ll; #define S second #define F first #define mp make_pair pll operator-(const pll &a, const pll &b) { ll g = b.S/__gcd(abs(a.S), b.S)*a.S; ll x = a.F*g/a.S-b.F*g/b.S, y = g; g = __gcd(abs(x), y); return mp(x/g, y/g); } pll operator*(const pll &a, const pll &b) { ll x = a.F*b.F; ll y = a.S*b.S; if(y < 0) y*=-1, x*=-1; ll g = __gcd(abs(x), y); return mp(x/g, y/g); } ostream &operator<<(ostream &out, const pll &a) { if(a.S == 1) out << a.F; else out << a.F << '/' << a.S; return out; } pll operator/(const pll &a, const pll &b) { ll x = a.F*b.S; ll y = a.S*b.F; if(y < 0) y*=-1, x*=-1; ll g = __gcd(abs(x), y); return mp(x/g, y/g); }
signed main() { ios::sync_with_stdio(0); cin.tie(0);
int n; cin >> n; vector<vector<pll>> arr(n, vector<pll>(n+1)); for(int i = 0; i < n; i++) for(int j = 0, a; j < n+1; j++) cin >> a, arr[i][j]=mp(a, 1); for(int i = 0; i < n; i++) { bool can = 0; for(int j = i; j < n; j++) { if(arr[j][i].F != 0) { if(i!=j) swap(arr[i], arr[j]); can = 1; break; } } if(!can) { continue; } for(int j = 0; j < n; j++) { if(i == j) continue; auto x = arr[j][i]/arr[i][i]; for(int k = 0; k < n+1; k++) arr[j][k] = (arr[j][k]-(arr[i][k]*x)); } } for(int i = 0; i < n; i++) { bool can = 0; for(int j = 0; j < n; j++) can|=arr[i][j].F; if(!can) { if(arr[i][n].F) return cout << '0', 0; else return cout << 'N', 0; } } for(int i = 0; i < n; i++) { if(!arr[i][i].F) return cout << 'N', 0; } cout << "1\n"; for(int i = 0; i < n; i++) { cout << 'x' << i+1 << " = " << arr[i][n]/arr[i][i] << '\n'; }
return 0; }
|