欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

HDU 4305 Lightning (生成树计数,判断点共线,逆元)

程序员文章站 2022-03-25 13:20:43
...

Lightning

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2550    Accepted Submission(s): 952

Problem Description

There are N robots standing on the ground (Don't know why. Don't know how). 

HDU 4305 Lightning (生成树计数,判断点共线,逆元)
Suddenly the sky turns into gray, and lightning storm comes! Unfortunately, one of the robots is stuck by the lightning!

HDU 4305 Lightning (生成树计数,判断点共线,逆元)
So it becomes overladen. Once a robot becomes overladen, it will spread lightning to the near one.

HDU 4305 Lightning (生成树计数,判断点共线,逆元)
The spreading happens when: 
  Robot A is overladen but robot B not.
  The Distance between robot A and robot B is no longer than R.
  No other robots stand in a line between them.
In this condition, robot B becomes overladen. 

We assume that no two spreading happens at a same time and no two robots stand at a same position. 

HDU 4305 Lightning (生成树计数,判断点共线,逆元)
The problem is: How many kind of lightning shape if all robots is overladen? The answer can be very large so we output the answer modulo 10007. If some of the robots cannot be overladen, just output -1. 

Input

There are several cases.
The first line is an integer T (T < = 20), indicate the test cases.
For each case, the first line contains integer N ( 1 < = N < = 300 ) and R ( 0 < = R < = 20000 ), indicate there stand N robots; following N lines, each contains two integers ( x, y ) ( -10000 < = x, y < = 10000 ), indicate the position of the robot. 

Output

One line for each case contains the answer.

Sample Input

3

3 2

-1 0

0 1

1 0

3 2

-1 0

0 0

1 0

3 1

-1 0

0 1

1 0

Sample Output

3

1

-1

生成树计数问题,

这道题对点与点的连接情况有限制,在建边的时候需要比较两点距离是否大于r,而且这两点的连线上不能有其他点。

生成树的个数要取模

难死我吧

/*在建边的时候需要比较两点之间的距离是否大于R 另外这两点连成的线上不能有别的点*/

#include <bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int MAXN = 350;
const int MOD = 10007;
int n, r;
int inv[11000];
//费马小定理求逆元
int pow_mod(int a, int p, int n) {
	if(p == 0) return 1;
	LL ans = pow_mod(a, p >> 1, n);
	ans = ans * ans % n;
	if(p & 1) ans = ans * a % n;
	return ans;
} 
//逆元打表
void init() {
	inv[0] = 1;
	for(int i = 1; i < MOD; i++) inv[i] = pow_mod(i, MOD-2, MOD);
}
//点
struct Point {
	int x, y;
	Point(int x=0, int y=0) : x(x), y(y) {} 
} p[MAXN];

//线
struct Line {
	Point s, e;
	Line() {}
	Line(Point _s, Point _e) {
		s = _s;
		e = _e;
	} 
}; 

//点的各种操作
typedef Point Vector;
Vector operator + (Vector A, Vector B) { return Vector(A.x+B.x, A.y+B.y); }
Vector operator - (Point A, Point B) { return Vector(A.x-B.x, A.y-B.y); }
Vector operator * (Vector A, int p) { return Vector(A.x*p, A.y*p); }
int operator * (Vector A, Vector B) { return A.x*B.y - A.y*B.x; }
Vector operator / (Vector A, int p) { return Vector(A.x/p, A.y/p); }

//判断一个点是否在线段上 
bool onSeg(Point P, Line L) {
	return 
	((L.s-P)*(L.e-P)) == 0 &&
    (P.x-L.s.x)*(P.x-L.e.x) <= 0 &&
    (P.y-L.s.y)*(P.y-L.e.y) <= 0;
}
//两点的距离
int get_dis(int i, int j) {
	return (p[i].x-p[j].x)*(p[i].x-p[j].x) + (p[i].y-p[j].y)*(p[i].y-p[j].y);
}
// 是否符合连边的要求
bool check(int a, int b) {
	if(get_dis(a, b) > r*r) return false;
	Line tmp = Line(p[a], p[b]);
	for(int k = 0; k < n; k++) if(k!=a && k!=b) {
		if(onSeg(p[k], tmp)) return false;
	}
	return true;
}
//矩阵
struct Matrix {
	int a[MAXN][MAXN];
	Matrix() {
		memset(a, 0, sizeof(a));
	}
	void clear() {
		memset(a, 0, sizeof(a));
	}
    //矩阵相加
	Matrix operator + (const Matrix &b) const {
		Matrix tmp;
		for(int i = 0; i < n; i++)
			for(int j = 0; j < n; j++) 
				tmp.a[i][j] = a[i][j] + b.a[i][j];
		return tmp;
	}
    //矩阵相乘
	Matrix operator * (const Matrix& b) const {
		Matrix tmp;
		tmp.clear();
		for(int i = 0; i < n; i++) 
			for(int j = 0; j < n; j++) 
				for(int k = 0; k < n; k++) tmp.a[i][j] += a[i][k]*b.a[k][j];
		return tmp;
	}
    //矩阵求值
	Matrix operator ^ (int k) {
		Matrix ans, tmp = *this;
		while(k) {
			if(k&1) ans = ans * tmp;
			k >>= 1;
			tmp = tmp * tmp;
		}
		return ans;
	}
    //求行列式的值模上MOD,需要使用逆元
	int det(int n)
    {
        for(int i = 0; i < n; i++)
            for(int j = 0;j < n;j++)
                a[i][j] = (a[i][j]%MOD+MOD)%MOD;
        int res = 1;
        for(int i = 0; i < n; i++)
        {
            for(int j = i; j < n; j++)
                if(a[j][i]!=0)
                {
                    for(int k = i;k < n;k++)
                        swap(a[i][k],a[j][k]);
                    if(i != j)
                        res = (-res+MOD)%MOD;
                    break;
                }
            if(a[i][i] == 0)
            {
                res = -1;//不存在(也就是行列式值为0)
                break;
            }

            for(int j = i+1; j < n; j++)
            {
                int mut = (a[j][i]*inv[a[i][i]])%MOD;//打表逆元
                for(int k = i; k < n; k++)
                    a[j][k] = (a[j][k]-(a[i][k]*mut)%MOD+MOD)%MOD;
            }
            res = (res * a[i][i])%MOD;
        }
        return res;
    }
}; 
Matrix C;
int main() {
	int t, u, v; 
    cin >> t;
	init();
	while(t--) {
		cin >> n >> r;
		C.clear();
		for(int i = 0; i < n; i++) {
			cin >> u >> v;
			p[i] = Point(u, v);
		}
		for(int i = 0; i < n; i++) {
			for(int j = i+1; j < n; j++) {
				if(check(i, j)) {
					C.a[i][j]--;
					C.a[j][i]--;
					C.a[i][i]++;
					C.a[j][j]++;
				}
			}
		}
		cout << C.det(n-1) << endl;
	}
    return 0;
}

 

相关标签: 图论