HDU4305 Lightning
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.
InputThere 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.
OutputOne 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
题目配图戳中笑点2333
大意:给出n个点,求生成树数量,两个点之间有无向边需要满足:两点距离小于给定的R,且两点所连线段上没有其他点
图论 矩阵树定理 模方程
n只有300,于是直接O(n^3)暴力判断两点间是否有边(判距离+判斜率)
建出Matrix-Tree定理的矩阵后,高斯消元计算矩阵行列式的值,输出答案,无解输出-1
注意到矩阵是处在模意义下的,又去学了一下高斯消元解线性模方程组
↑全程循环着シカコ的《胃が痛いんだなぁ》,灵感泉涌,一次AC,带感
↑这歌真的很棒呢(跑题)
1 /*by SilverN*/ 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 using namespace std; 8 const int mod=10007; 9 const double eps=1e-7; 10 const int mxn=330; 11 int read(){ 12 int x=0,f=1;char ch=getchar(); 13 while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();} 14 while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();} 15 return x*f; 16 } 17 struct block{ 18 int x,y; 19 }a[mxn]; 20 int n,R; 21 int D; 22 int G[mxn][mxn]; 23 bool vis[mxn]; 24 void exgcd(int a,int b,int &x,int &y){//求逆元用 25 if(!b){x=1;y=0;return;} 26 exgcd(b,a%b,x,y); 27 int t=x;x=y;y=t-a/b*x; 28 return; 29 } 30 int Gauss(){//Matrix-tree定理 高斯消元 31 int i,j,x,y; 32 int ans=1; 33 for(i=1;i<n;i++){ 34 int p=i; 35 if(!G[i][i]){ 36 for(j=i+1;j<n;j++)if(G[j][i]>G[p][i])p=j; 37 if(p==i){return -1;}//无解 38 for(j=1;j<n;j++)swap(G[p][j],G[i][j]); 39 ans=-ans; 40 } 41 exgcd(G[i][i],mod,x,y); 42 x=((x%mod)+mod)%mod; 43 for(int k=i+1;k<n;k++)(G[i][k]*=x)%=mod; 44 for(j=i+1;j<n;j++){ 45 if(G[j][i]){ 46 for(int k=i+1;k<n;k++){ 47 G[j][k]=((G[j][k]-G[i][k]*G[j][i])%mod+mod)%mod; 48 } 49 } 50 } 51 ans=ans*G[i][i]%mod; 52 } 53 return ans; 54 } 55 inline int dist(int i,int j){//距离 56 return (a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y); 57 } 58 inline double ck(int i,int j){//斜率 59 return ((double)a[i].y-a[j].y)/(double)(a[i].x-a[j].x); 60 } 61 bool check(int x,int y){ 62 int dis=dist(x,y); 63 if(dis>D)return 0;//距离大于R 64 double k=ck(x,y); 65 for(int i=1;i<=n;i++){ 66 if(i==y || i==x)continue; 67 if(dist(x,i)<dis && fabs(ck(x,i)-k)<eps)return 0;//连线上有其他机器人 68 } 69 return 1; 70 } 71 void solve(){ 72 int i,j,k; 73 for(i=1;i<=n;i++) 74 for(j=i+1;j<=n;j++){ 75 if(check(i,j)){ 76 ++G[i][i]; ++G[j][j]; 77 --G[i][j]; --G[j][i]; 78 vis[i]=vis[j]=1; 79 } 80 } 81 for(i=1;i<=n;i++) 82 if(!vis[i]){printf("-1\n");return;} 83 for(i=1;i<=n;i++) 84 for(j=1;j<=n;j++){ 85 G[i][j]=((G[i][j]%mod)+mod)%mod; 86 } 87 printf("%d\n",Gauss()); 88 return; 89 } 90 void init(){ 91 memset(G,0,sizeof G); 92 memset(vis,0,sizeof vis); 93 return; 94 } 95 int main(){ 96 int i,j; 97 int T=read(); 98 while(T--){ 99 init(); 100 n=read();R=read();D=R*R; 101 for(i=1;i<=n;i++){a[i].x=read();a[i].y=read();} 102 solve(); 103 } 104 return 0; 105 }
本文为博主原创文章,转载请注明出处。