http://www.spoj.pl/problems/HIGH/
题目写着答案不超过long long,应该是中间过程爆long long了,所以用整型做的时候在辗转相除的过程超时了,后来改成double就过了!
代码如下:
View Code
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <cmath> 5 6 using namespace std; 7 8 //typedef long long ll; 9 typedef double ll; 10 const double eps = 1e-8; 11 const int maxSize = 15; 12 int curSize = maxSize; 13 14 struct Mat { 15 ll val[maxSize][maxSize]; 16 17 Mat() { 18 for (int i = 0; i < curSize; i++) { 19 for (int j = 0; j < curSize; j++) { 20 val[i][j] = 0; 21 } 22 } 23 } 24 ll Det() { 25 ll ret = 1; 26 27 for (int p = 1; p < curSize; p++) { 28 for (int l = p + 1; l < curSize; l++) { 29 // while (val[l][p]) { 30 while (fabs(val[l][p]) > eps) { 31 // int k = val[p][p] / val[l][p]; 32 double k = val[p][p] / val[l][p]; 33 34 for (int i = p; i < curSize; i++) { 35 val[p][i] -= val[l][i] * k; 36 swap(val[p][i], val[l][i]); 37 } 38 ret = -ret; 39 } 40 } 41 // if (!val[p][p]) return 0; 42 } 43 44 for (int i = 1; i < curSize; i++) { 45 ret *= val[i][i]; 46 } 47 48 return ret; 49 } 50 } mat; 51 52 void makeMat(int m) { 53 int s, t; 54 55 mat = Mat(); 56 while (m--) { 57 scanf("%d%d", &s, &t); 58 s--; t--; 59 // if (!mat.val[s][t]) { 60 if (fabs(mat.val[s][t]) < eps) { 61 mat.val[s][t] = mat.val[t][s] = -1; 62 mat.val[s][s]++; mat.val[t][t]++; 63 } 64 } 65 } 66 67 int main() { 68 int T, m; 69 70 // freopen("in", "r", stdin); 71 scanf("%d", &T); 72 while (T-- && ~scanf("%d%d", &curSize, &m)) { 73 makeMat(m); 74 // printf("%lld\n", mat.Det()); 75 printf("%.0f\n", mat.Det()); 76 } 77 78 return 0; 79 }
——written by Lyon