码迷,mamicode.com
首页 > 其他好文 > 详细

BZOJ 1002 + SPOJ 104 基尔霍夫矩阵 + 一个递推式。

时间:2016-02-12 22:12:25      阅读:323      评论:0      收藏:0      [点我收藏+]

标签:

BZOJ 1002

高精度 + 递推

f[1] = 1; f[2] = 5; f[i] = f[i - 1] * 3 - f[i - 2] + 2;

SPOJ 104

裸 + 不用Mod

技术分享
  1 #include <cstdio>
  2 #include <cstring>
  3 #include <cstdlib>
  4 #include <algorithm>
  5 #include <iostream>
  6  
  7 using namespace std;
  8  
  9 const int rad = 10;
 10  
 11 int n;
 12  
 13 struct BigInt {
 14   int v[1005];
 15   int len;
 16  
 17   BigInt() {
 18     memset(v, 0, sizeof v);
 19     len = 0;
 20   }
 21  
 22   friend BigInt operator + (BigInt a, BigInt b) {
 23     int tmp = max(a.len, b.len);
 24  
 25     for(int i = 1; i <= tmp; ++ i) {
 26       a.v[i] += b.v[i];
 27       if(a.v[i] >= rad) {
 28         a.v[i + 1] += a.v[i] / rad;
 29         a.v[i] %= rad;
 30       }
 31     }
 32     while(a.v[tmp + 1]) tmp ++;
 33     a.len = tmp;
 34  
 35     return a;
 36   }
 37  
 38   friend BigInt operator * (BigInt a, BigInt b) {
 39     BigInt c;
 40     int tmp = a.len + b.len;
 41  
 42     for(int i = 1; i <= a.len; ++ i) {
 43       for(int j = 1; j <= b.len; ++ j) {
 44         c.v[i + j - 1] += a.v[i] * b.v[j];
 45       }
 46     }
 47     for(int i = 1; i <= tmp; ++ i) {
 48       if(c.v[i] >= rad) {
 49         c.v[i + 1] += c.v[i] / rad;
 50         c.v[i] %= rad;
 51       }
 52       if(c.v[i]) c.len = i;
 53     }
 54  
 55     return c;
 56   }
 57  
 58   friend BigInt operator * (BigInt a, int k) {
 59     for(int i = 1; i <= a.len; ++ i) {
 60       a.v[i] *= k;
 61     }
 62     for(int i = 1; i <= a.len; ++ i) {
 63       a.v[i + 1] += a.v[i] / rad;
 64       a.v[i] %= rad;
 65     }
 66     if(a.v[a.len + 1]) a.len ++;
 67     return a;
 68   }
 69  
 70   friend BigInt operator - (BigInt a, BigInt b) {
 71     for(int i = 1; i <= a.len; ++ i) {
 72       a.v[i] -= b.v[i];
 73       if(a.v[i] < 0) {
 74         a.v[i + 1] -= 1;
 75         a.v[i] += rad;
 76       }
 77     }
 78     while(a.v[a.len] == 0)a.len --;
 79     return a;
 80   }
 81   void getint(int k) {
 82     while(k) {
 83       v[++ len] = k % 10;
 84       k /= 10;
 85     }
 86   }
 87    
 88   void print() {
 89     for(int i = len; i >= 1; -- i) {
 90       printf("%d", v[i]);
 91     }
 92   }
 93 }f[105], cst;
 94  
 95 void Input() {
 96   scanf("%d", &n);
 97 }
 98  
 99 void Solve() {
100   f[1].getint(1);
101   f[2].getint(5);
102   cst.getint(2);
103   for(int i = 3; i <= n; ++ i) {
104     f[i] = f[i - 1] * 3 - f[i - 2] + cst;
105   }
106 }
107  
108 void Output() {
109   f[n].print();
110 }
111  
112 int main() {
113   Input();
114   Solve();
115   Output();
116  
117   return 0;
118 }
BZOJ 1002
技术分享
 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <algorithm>
 6  
 7 using namespace std;
 8  
 9 typedef long long ll;
10 const int N = 15;
11  
12 ll ans;
13 int n, m;
14 ll degree[N][N], G[N][N], self[N][N];
15  
16 ll det(ll a[N][N]) {
17   ll res = 1;
18   
19   for(int i = 1; i < n; ++ i) {
20     for(int j = i + 1; j < n; ++ j) {
21       while(a[j][i]) {
22         ll t = a[i][i] / a[j][i];
23  
24         for(int k = i; k < n; ++ k)
25           a[i][k] = (a[i][k] - a[j][k] * t);
26         for(int k = i; k < n; ++ k)
27           swap(a[i][k], a[j][k]);
28         res = -res;
29       }
30     }
31     if(a[i][i] == 0) return 0;
32     res = 1LL * res * a[i][i];
33   }
34   return res;
35 }
36  
37 void Input() {
38   int x, y;
39  
40   memset(self, 0, sizeof self);
41   memset(degree, 0, sizeof degree);
42   
43   scanf("%d%d", &n, &m);
44   for(int i = 1; i <= m; ++ i) {
45     scanf("%d%d", &x, &y);
46     degree[x][y] ++;
47     degree[y][x] ++;
48     self[x][x] ++; self[y][y] ++;
49   }
50   for(int i = 1; i <= n; ++ i) {
51     for(int j = 1; j <= n; ++ j) {
52       G[i][j] = self[i][j] - degree[i][j];
53     }
54   }
55 }
56  
57 void Solve() {
58   ans = det(G);
59 }
60  
61 void Output() {
62   printf("%lld\n", ans);
63 }
64  
65 int main() {
66   int t;
67  
68   scanf("%d", &t);
69  
70   while(t --) {
71     Input();
72     Solve();
73     Output();
74   }
75  
76   return 0;
77 }
SPOJ 104

BZOJ 1002 + SPOJ 104 基尔霍夫矩阵 + 一个递推式。

标签:

原文地址:http://www.cnblogs.com/sxprovence/p/5187294.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!