P2455 [SDOI2006]線性方程組 題解
阿新 • • 發佈:2022-05-25
亂搞思路
他卡我順序,那我打亂不就行了嘛
我直接一個 rand()
,不斷的交換就行了。
交了 \(10\) 遍才過。
/* Work by: TLE_Automation */ #include<cmath> #include<queue> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define LL long long #define int long long using namespace std; const int N = 1e6 + 10; const int MAXN = 2e5 + 10; inline char readchar() { static char buf[100000], *p1 = buf, *p2 = buf; return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++; } inline int read() { #define readchar getchar int res = 0, f = 0;char ch = readchar(); for(; !isdigit(ch); ch = readchar()) if(ch == '-') f = 1; for(; isdigit(ch); ch = readchar()) res = (res << 1) + (res << 3) + (ch ^ '0'); return f ? -res : res; } inline void print(int x) { if (x < 0 ) putchar('-'), x = -x; if (x > 9 ) print(x / 10); putchar(x % 10 + '0'); } double a[102][102]; int n; void Gauss() { for(int i = 1; i <= n; i++) { int Max = i; for(int j = i + 1; j <= n; j++) if(fabs(a[j][i]) > fabs(a[Max][i])) Max = j; for(int j = 1; j <= n + 1; j++) swap(a[i][j], a[Max][j]); if(a[i][i] == 0) continue; for(int j = 1; j <= n; j++) { if(j == i) continue; double tmp = a[j][i] / a[i][i]; for(int k = 1; k <= n + 1; k++) a[j][k] -= a[i][k] * tmp; } } } #include<ctime> signed main() { srand(time(0)); n = read(); for(int i = 1; i <= n; i++) { for(int j = 1; j <= n + 1; j++) { scanf("%lf", &a[i][j]); } } int T = 10; while(T--) { for(int i = 1; i <= n; i++) { int rd = rand() % n + 1; for(int j = 1; j <= n + 1; j++) { swap(a[i][j], a[rd][j]); } } } Gauss(); for(int i = 1; i <= n; i++) { if(a[i][i] == 0 && a[i][n + 1] == 0) return puts("0"), 0; if(a[i][i] == 0 && a[i][n + 1] != 0) return puts("-1"), 0; } for(int i = 1; i <= n; i++) { printf("x%lld=%.2lf\n", i, a[i][n + 1] / a[i][i]); } }