HDU 4305 lightning (Gaussian demetabase Kirchhoff matrix + inverse element)

Source: Internet
Author: User

For some coordinate points, if the distance between the two points is smaller than R and there are no other points between the two points, the two points are connected, which forms a graph. Ask the number of spanning trees in this graph.

Because the data volume is not large, the O (N ^ 3) graph creation is no problem.

After creating the graph, you can use the Kirchhoff matrix to calculate the number of spanning trees. The reason for writing the problem-solving report is that the inverse element is required when Gaussian deyuan solves the Kirchhoff matrix.

(A/B) % mod. If the range of A and B is large, there will be a large error in the result. Here we can convert B * x = 1 (% mod) then X is the reverse element of B (a/B) % mod = (A * X) % mod

The inverse element is the process of solving the linear homogeneous equation B * x reg1 (% MoD ).

For details, see the code:

/* A negative number is not allowed in the whole process of Gaussian element elimination * // # pragma comment (linker, "/Stack: 327680000,327680000 ") # include <iostream> # include <cstdio> # include <cmath> # include <vector> # include <cstring> # include <algorithm> # include <string> # include <set> # include <functional> # include <numeric> # include <sstream> # include <stack> # include <map> # include <queue> # define Cl (ARR, val) memset (ARR, Val, sizeof (ARR) # define rep (I, n) for (I) = 0; (I) <(N); ++ (I) # define for (I, L, H) for (I) = (l); (I) <= (h ); ++ (I) # define Ford (I, H, L) for (I) = (h); (I)> = (l); -- (I )) # define L (x) <1 # define R (x) <1 | 1 # define mid (L, R) (L + r)> 1 # define min (x, y) x <Y? X: y # define max (x, y) x <Y? Y: X # define e (x) (1 <(x) # define iabs (x) <0? -(X): (x) # define out (x) printf ("% i64d \ n", x) # define lowbit (x) & (-x) # define read () freopen ("data. in "," r ", stdin) # define write () freopen (" data. out "," W ", stdout); const double EPS = 1e-8; typedef long ll; const int INF = ~ 0u> 2; using namespace STD; const int n = 330; const int mod = 10007; struct node {Double X; Double Y; double DIS (node CMP) {return (CMP. x-x) * (CMP. x-x) + (CMP. y-y) * (CMP. y-y) ;}} P [N]; int exp_gcd (int A, int B, Int & X, Int & Y) {If (B = 0) {x = 1; y = 0; return a;} int P = exp_gcd (B, A % B, x, y); int TMP = x; X = y; y = TMP-(A/B) * Y; return P;} int det (int A [] [N], int N) {int I, j, k; int ans = 1, x, y; bool flag = true; for (I = 0; I <n; ++ I) {If (! A [I] [I]) {for (j = I + 1; j <n; ++ J) {if (a [J] [I]) {for (k = I; k <n; ++ K) Swap (A [I] [K], a [J] [k]); flag =! Flag; break ;}}if (j = N) Return-1;} ans = (ANS * A [I] [I] % mod + mod) % MOD; exp_gcd (A [I] [I], Mod, x, y); X = (X % mod + mod) % MOD; For (k = I + 1; k <n; ++ k) A [I] [k] = (a [I] [k] * x % mod + mod) % MOD; for (j = I + 1; j <n; ++ J) {if (a [J] [I]) {for (k = I + 1; k <N; ++ K) {A [J] [k] = (A [J] [k]-A [J] [I] * A [I] [k]) % mod + mod) % MOD;} A [J] [I] = 0 ;}} if (FLAG) return ans; Return (-ans % mod + mod) % MOD;} int a [n] [N], D [N] [N], C [N] [N]; int main () {// read (); int t, n, R, I, J, k; CIN> T; while (t --) {CIN> N> r; rep (I, n) CIN> P [I]. x> P [I]. y; CL (A, 0); rep (I, n) {for (j = I + 1; j <n; ++ J) {// printf ("% d % F \ n", I, j, P [I]. DIS (P [J]); If (P [I]. DIS (P [J])> r * r) continue; For (k = 0; k <n; ++ K) {If (k = I | K = J) continue; If (P [K]. y-P [I]. y) * (p [J]. x-P [K]. x) = (P [K]. x-P [I]. x) * (p [J]. y-P [k]. Y) & P [I]. DIS (P [J])> P [I]. DIS (P [k]) & P [I]. DIS (P [J])> P [K]. DIS (P [J]) break;} If (k = n) A [I] [J] = A [J] [I] = 1 ;}} CL (D, 0); bool flag = true; rep (I, n) {d [I] [I] = 0; rep (J, n) {if (a [I] [J]) d [I] [I] ++;} If (d [I] [I] = 0) {flag = false; break;} If (! Flag) {puts ("-1"); continue;} rep (I, n) {rep (J, n) {c [I] [J] = d [I] [J]-A [I] [J]; c [I] [J] = (C [I] [J] % mod + mod) % MOD;} printf ("% d \ n", det (C, n-1);} return 0 ;}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.