Lightning
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1591 Accepted Submission(s): 528
Problem Description
There are N robots standing on the ground (Don't know why. Don't know how).
Suddenly the sky turns into gray, and lightning storm comes! Unfortunately, one of the robots is stuck by the lightning!
So it becomes overladen. Once a robot becomes overladen, it will spread lightning to the near one.
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.
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.
Suddenly the sky turns into gray, and lightning storm comes! Unfortunately, one of the robots is stuck by the lightning!
So it becomes overladen. Once a robot becomes overladen, it will spread lightning to the near one.
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.
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.
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 另外这两点连成的线上不能有别的点
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>
#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10007
#define MAXN 10010
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
using namespace std;
int n,R;//点的个数 距离
struct Point
{
int x,y;
Point(int _x=0,int _y=0)
{
x=_x; y=_y;
}
Point operator -(const Point &b)const
{
return Point(x-b.x,y-b.y);
}
int operator ^(const Point &b)const
{
return x*b.y-y*b.x;
}
void input()
{
scanf("%d%d",&x,&y);
}
};
Point p[330];
struct Line
{
Point s,e;
Line(){}
Line(Point _s,Point _e)
{
s=_s; e=_e;
}
};
bool onSeq(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 sqdis(Point a,Point b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
bool check(int a,int b)
{
if(sqdis(p[a],p[b])>R*R) return 0;
for(int i=0;i<n;i++)
if(i!=a&&i!=b)
if(onSeq(p[i],Line(p[a],p[b])))
return 0;
return 1;
}
int INV[MOD];
//求ax=1(mod m)的x值 就是逆元(0<a<m)
ll inv(ll a,ll m)
{
if(a==1) return 1;
return inv(m%a,m)*(m-m/a)%m;
}
struct Matrix
{
int mat[330][330];
void init()
{
MEM(mat,0);
}
int det(int n)
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
mat[i][j]=(mat[i][j]%MOD+MOD)%MOD;
int res=1;
for(int i=0;i<n;i++)
{
for(int j=i;j<n;j++)
if(mat[j][i]!=0)
{
for(int k=i;k<n;k++)
swap(mat[i][k],mat[j][k]);
if(i!=j)
res=(-res+MOD)%MOD;
break;
}
if(mat[i][i]==0)
{
res=-1; break;
}
for(int j=i+1;j<n;j++)
{
int mut=(mat[j][i]*inv(mat[i][i],MOD))%MOD;
for(int k=i;k<n;k++)
mat[j][k]=(mat[j][k]-(mat[i][k]*mut)%MOD+MOD)%MOD;
}
res=(res*mat[i][i])%MOD;
}
return res;
}
};
int g[330][330];
int main()
{
fread;
//预处理逆元
for(int i=1;i<MOD;i++)
INV[i]=inv(i,MOD);
int tc;
scanf("%d",&tc);
while(tc--)
{
scanf("%d%d",&n,&R);
for(int i=0;i<n;i++)
p[i].input();
MEM(g,0);
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
if(check(i,j))
g[i][j]=g[j][i]=1;
Matrix res;
res.init();
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(i!=j&&g[i][j])
{
res.mat[i][j]=-1;
res.mat[i][i]++;
}
printf("%d\n",res.det(n-1));
}
return 0;
}