constint N = 110; constdouble eps = 1e-8; int n; double a[N][N];
intgauss(){ int p = 1, r = 1; for (; p <= n; p++) { int target = r; for (int i = r+1; i <= n; i++) if (fabs(a[i][p]) >= fabs(a[target][p])) target = i; if (fabs(a[target][p]) < eps) continue; for (int j = p; j <= n+1; j++) swap(a[r][j], a[target][j]); for (int j = n+1; j >= p; j--) a[r][j] /= a[r][p]; for (int i = r+1; i <= n; i++) { if (fabs(a[i][p]) < eps) continue; for (int j = n+1; j >= p; j--) a[i][j] -= a[r][j] * a[i][p]; } r++; }
if (r <= n) { for (int i = r; i <= n; i++) if (fabs(a[i][n+1]) > eps) return-1; return0; }
for (int i = n; i; i--) for (int j = i+1; j <= n; j++) a[i][n+1] -= a[j][n+1] * a[i][j];
return1; }
intmain(){ scanf("%d", &n); for (int i = 1; i <= n; i++) for (int j = 1; j <= n+1; j++) scanf("%lf", &a[i][j]); int res = gauss(); if (res != 1) printf("%d\n", res); elsefor (int i = 1; i <= n; i++) printf("x%d=%.2lf\n", i, a[i][n+1]); return0; }
#define int long long constint N = 410, P = 1e9+7; int n, A[N][N<<1];
intqmi(int a, int k){ int res = 1; while (k) { if (k & 1) res = res * a % P; a = a * a % P; k >>= 1; } return res; }
boolgauss(){ for (int i = 1; i <= n; i++) A[i][i+n] = 1;
for (int r = 1; r <= n; r++) { int target = -1; for (int i = r; i <= n; i++) { if (A[i][r]) { target = i; break; } } if (target == -1) returnfalse; swap(A[r], A[target]);
int inv = qmi(A[r][r], P-2); for (int i = r; i <= n*2; i++) A[r][i] = A[r][i] * inv % P;
for (int i = r+1; i <= n; i++) { for (int j = n*2; j >= r; j--) { A[i][j] = (A[i][j] - A[r][j] * A[i][r]) % P; if (A[i][j] < 0) A[i][j] += P; } } }
for (int i = n; i; i--) { for (int j = i-1; j; j--) { for (int k = n*2; k >= i; k--) { A[j][k] = (A[j][k] - A[i][k] * A[j][i]) % P; if (A[j][k] < 0) A[j][k] += P; } } } returntrue; }
signedmain(){ scanf("%lld", &n); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) scanf("%lld", &A[i][j]); if (!gauss()) puts("No Solution"); elsefor (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) printf("%lld%c", A[i][j+n], " \n"[j == n]); return0; }
[JSOI2008] 球形空间产生器
在 n 维空间的球面上给定 n+1 个点的坐标,求球心坐标。数据保证唯一解。1≤n≤10,∣aij∣,∣bij∣≤20000。
#define eb emplace_back #define fi first #define se second typedef pair<int, int> PII; constint N = 510; constdouble eps = 1e-8; vector<double> vals; vector<int> g[N]; vector<PII> edge; int n, m; double a[N][N], f[N];
voidgauss(){ int p = 1, r = 1; for (; p <= n; p++) { int target = r; for (int i = r+1; i <= n; i++) if (fabs(a[i][p]) >= fabs(a[target][p])) target = i; assert(fabs(a[target][p]) > eps); swap(a[r], a[target]); // for (int j = n+1; j >= p; j--) a[r][j] /= a[r][p]; for (int i = r+1; i <= n; i++) { double k = a[i][p] / a[r][p]; for (int j = n+1; j >= p; j--) a[i][j] -= a[r][j] * k; } r++; }
for (int i = n; i; i--) { for (int j = i+1; j <= n; j++) a[i][n+1] -= f[j] * a[i][j]; f[i] = a[i][n+1] / a[i][i]; } }
intmain(){ scanf("%d%d", &n, &m); for (int i = 1, u, v; i <= m; i++) scanf("%d%d", &u, &v), g[u].eb(v), g[v].eb(u), edge.eb(u, v); for (int u = 1; u < n; u++) { for (int v: g[u]) if (v != n) a[u][v] = 1. / g[v].size(); a[u][u] = -1; } a[1][n] = -1; --n; gauss();
double ans = 0; for (const PII& e: edge) vals.eb(calc(e)); sort(vals.begin(), vals.end(), greater<double>()); for (int i = 1; i <= m; i++) ans += vals[i-1] * i; return !printf("%.3lf\n", ans); }