HDU4592 Boring Game (2013 ACM-ICPC南京賽區全國邀請賽) 高斯消元
題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=4592
分析:
本題的資料量特別大,所以要對高斯消元儘可能多的優化。
因為n的範圍比較小為1~8 ,因此我們可以將係數陣的逆給求出來 然後根據逆來判斷有沒有解 ,
有解的情況下 直接用A^(-1)*B=X; 求解,但是由於係數陣是0,1的 相當於二進位制, 我們可以將其改成10進位制
然後進行運算的時候就快一點了 ,然後統計解中1的個數 直接列舉即可
程式碼如下:
#include <iostream>
#include <cstring>
#include <cstdio>
//#define debug
using namespace std;
int a[30][30];
int x[80],n,equ,var;
void init()
{
memset(a,0,sizeof(a));
memset(x,0,sizeof(x[0])*30);
equ=n*n;
var=equ;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
if(i!=0) a[i*n+j][(i-1)*n+j]=1;
if(i!=n-1) a[i*n+j][(i+1)*n+j]=1;
if(j!=0) a[i*n+j][i*n+j-1]=1;
if(j!=n-1) a[i*n+j][i*n+j+1]=1;
a[i*n+j][i*n+j]=1;
}
}
}
int Gauss()
{
int k,col,max_r;
for(k=0,col=0; k<equ&&col<var; k++,col++)
{
if(a[k][col]==0)
{
max_r=k;
for(int i=k+1; i<equ; i++)
if(a[i][col]>a[max_r][col])
{
max_r=i;
break;
}
if(max_r!=k)
for(int i=0; i<=var; i++)
swap(a[max_r][i],a[k][i]);
}
if(a[k][col]==0)
{
k--;
continue;
}
for(int i=k+1; i<equ; i++)
{
if(a[i][col])
for(int j=col; j<=var; j++)
a[i][j]=a[i][j]^a[k][j];
}
}
for(int i=k; i<equ; i++)
if(a[i][var]) return -1;
int lim=(1<<(var-k)),ans=100000;
for(int i=0; i<lim; i++)
{
int temp=i,sum=0;
for(int j=var-1; j>=k; j--)
{
x[j]=temp&1;
temp>>=1;
}
for(int j=k-1; j>=0; j--)
{
x[j]=a[j][var]&1;
for(int r=j+1; r<var; r++)
x[j]=x[j]^(a[j][r]&x[r]);
}
for(int j=0; j<var; j++)
sum+=x[j];
ans=min(ans,sum);
}
return ans;
}
int n2[4][2]=
{
3,2,3,1,2,3,1,3
};
int n3[9][3]=
{
5,1,6,0,2,7,5,4,3,1,3,1,
2,7,2,
4,6,4,
6,1,5,
7,2,0,
3,4,5
};
int n6[36][6]=
{
43,8,55,10,44,40,
3,20,53,17,14,4,
44,42,7,32,55,34,
13,21,56,1,59,17,
48,10,43,34,28,8,
53,4,59,20,13,5,
8,28,2,27,34,44,
20,54,5,59,21,14,
42,43,10,48,2,55,
21,53,20,3,16,59,
10,27,40,55,42,28,
4,14,16,54,17,13,
55,2,48,42,27,10,
53,5,56,1,59,17,
7,10,28,40,48,32,
56,20,14,5,3,1,
43,40,7,32,55,34,
59,16,3,21,54,20,
10,27,42,48,2,55,
17,59,1,56,5,53,
32,48,40,28,10,7,
1,3,5,14,20,56,
34,55,32,7,40,43,
20,54,21,3,16,59,
44,34,27,2,28,8,
14,21,59,5,54,20,
55,2,48,10,43,42,
59,16,3,20,53,21,
28,42,55,40,27,10,
13,17,54,16,14,4,
40,44,10,55,8,43,
4,14,17,53,20,3,
34,55,32,7,42,44,
17,59,1,56,21,13,
8,28,34,43,10,48,
5,13,20,59,4,53
};
int n7[49][7]=
{
109,65,14,84,88,16,96,
108,34,27,2,28,40,112,
3,20,53,81,110,84,56,
107,0,107,8,119,42,28,
96,20,86,69,59,21,14,
27,34,108,32,28,10,7,
91,65,56,21,13,4,3,
65,99,21,86,68,56,16,
34,119,32,7,42,108,40,
20,54,69,91,5,86,84,
0,0,8,28,34,107,42,
20,54,81,109,80,53,21,
34,119,2,112,42,27,10,
65,99,84,53,17,14,4,
14,21,59,5,54,68,88,
27,32,107,8,119,42,28,
53,69,88,17,99,5,110,
107,8,119,34,0,34,119,
86,81,13,68,99,80,59,
108,2,107,8,119,42,28,
56,84,110,80,54,17,13,
84,86,5,91,5,86,84,
2,7,8,27,8,7,2,
81,91,17,96,17,91,81,
8,28,34,107,34,28,8,
69,109,68,3,68,109,69,
32,112,8,108,8,112,32,
21,53,80,109,80,53,21,
88,68,54,5,59,21,14,
28,42,119,8,107,32,27,
110,5,99,17,88,69,53,
119,34,0,34,119,8,107,
59,80,99,68,13,81,86,
28,42,119,8,107,2,108,
13,17,54,80,110,84,56,
16,56,68,86,21,99,65,
40,108,42,7,32,119,34,
84,86,5,91,69,54,20,
42,107,34,28,8,0,0,
21,53,80,109,81,54,20,
10,27,42,112,2,119,34,
4,14,17,53,84,99,65,
96,16,88,84,14,65,109,
112,40,28,2,27,34,108,
56,84,110,81,53,20,3,
28,42,119,8,107,0,107,
14,21,59,69,86,20,96,
7,10,28,32,108,34,27,
3,4,13,21,56,65,91
};
int n8[64][8]=
{
220,138,7,128,199,170,108,40,
214,81,13,64,237,1,238,68,27,0,27,32,107,168,199,130,237,81,54,16,14,5,3,1,183,138,108,8,112,160,192,128,216,0,216,4,214,21,227,65,107,138,176,2,183,128,119,34,
59,81,224,1,227,85,54,20,138,219,10,192,42,171,130,108,81,219,17,224,65,3,69,238,0,0,32,112,136,172,42,199,81,219,65,56,21,13,4,3,138,219,130,28,168,176,32,192,0,0,4,14,17,53,84,227,138,219,136,7,130,192,162,119,
81,219,80,3,84,213,65,54,
7,10,28,160,172,2,171,170,
13,17,54,16,14,5,3,1,
27,32,107,136,183,10,172,168,
54,65,213,68,59,20,13,5,
108,130,171,34,220,40,176,160,
216,4,214,17,237,80,53,21,
176,136,108,8,112,160,192,128,
224,80,56,5,53,64,213,85,
128,192,160,112,136,172,42,199,
64,224,16,216,84,14,65,237,
32,112,136,172,10,183,136,107,
16,56,68,214,69,59,21,14,
8,28,34,107,162,220,168,112,
4,14,17,53,80,237,17,214,
2,7,8,27,42,112,130,183,
1,3,5,14,17,53,84,227,
199,42,172,136,112,160,192,128,
237,65,14,84,216,16,224,64,
107,136,183,10,172,136,112,32,
14,21,59,69,214,68,56,16,
112,168,220,162,107,34,28,8,
214,17,237,80,53,17,14,4,
183,130,112,42,27,8,7,2,
227,84,53,17,14,5,3,1,
170,171,2,172,160,28,10,7,
1,3,5,14,16,54,17,13,
168,172,10,183,136,107,32,27,
5,13,20,59,68,213,65,54,
160,176,40,220,34,171,130,108,
21,53,80,237,17,214,4,216,
128,192,160,112,8,108,136,176,
85,213,64,53,5,56,80,224,
108,130,171,42,192,10,219,138,
238,69,3,65,224,17,219,81,
199,42,172,136,112,32,0,0,
3,4,13,21,56,65,219,81,
192,32,176,168,28,130,219,138,
227,84,53,17,14,4,0,0,
119,162,192,130,7,136,219,138,
54,65,213,84,3,80,219,81,
40,108,170,199,128,7,138,220,
68,238,1,237,64,13,81,214,
130,199,168,107,32,27,0,27,
1,3,5,14,16,54,81,237,
128,192,160,112,8,108,138,183,
65,227,21,214,4,216,0,216,
34,119,128,183,2,176,138,107,
20,54,85,227,1,224,81,59
};
int ans[80];
int b[10];
int main()
{
int cas;
scanf("%d",&cas);
while(cas--)
{
scanf("%d",&n);
int sum=0;
for(int i=0; i<n; i++)
scanf("%d",&b[i]);
if(n==4||n==5)
{
init();
for(int i=0; i<n; i++)
{
int r=(i+1)*n-1,l=i*n;
while(r>=l)
{
a[r][var]=b[i]&1;
b[i]>>=1;
r--;
}
}
int ans=Gauss();
printf("%d\n",ans);
continue;
}
if(n==1)
{
printf("%d\n",b[0]);
continue;
}
equ=n*n;
memset(ans,0,sizeof(ans[0])*(equ+4));
if(n==2)
{
for(int j=0; j<n; j++)
if(b[j])
{
for(int i=0; i<equ; i++)
ans[i]=ans[i]^(n2[i][j]&b[j]);
}
}
else if(n==3)
{
for(int j=0; j<n; j++)
if(b[j])
{
for(int i=0; i<equ; i++)
ans[i]=ans[i]^(n3[i][j]&b[j]);
}
}
else if(n==6)
{
for(int j=0; j<n; j++)
if(b[j])
{
for(int i=0; i<equ; i++)
ans[i]=ans[i]^(n6[i][j]&b[j]);
}
}
else if(n==7)
{
for(int j=0; j<n; j++)
if(b[j])
{
for(int i=0; i<equ; i++)
ans[i]=ans[i]^(n7[i][j]&b[j]);
}
}
else if(n==8)
{
for(int j=0; j<n; j++)
if(b[j])
{
for(int i=0; i<equ; i++)
ans[i]=ans[i]^(n8[i][j]&b[j]);
}
}
for(int i=0; i<equ; i++)
{
int cnt=0;
while(ans[i])
{
cnt^=(ans[i]&1);
ans[i]>>=1;
}
sum+=cnt;
}
printf("%d\n",sum);
}
return 0;
}
相關文章
- NJUST 1746 Similar Number(南京邀請賽 J題)MILA
- ACM-ICPC 2018 南京賽區網路預賽__K The Great Nim Game【博弈論+費馬小定理+DP】ACMGAM
- ACM-ICPC 2018 南京賽區網路預賽__B The writing on the wall【列舉】ACM
- ACM-ICPC 2018 南京賽區網路預賽__E AC Challenge【狀態壓縮+DP】ACM
- POJ 1753 Flip Game(列舉變元的高斯消元)GAM
- 2018天梯賽、藍橋杯、(CCPC省賽、邀請賽、ICPC邀請賽)校內選拔賽反思總結!
- 第三屆華中地區邀請賽網路賽題解
- 【構造共軛函式+矩陣快速冪】HDU 4565 So Easy! (2013 長沙賽區邀請賽)函式矩陣
- ACM-ICPC 2018 南京賽區網路預賽__J. Sum【尤拉篩法+質因子分解+思維】ACM
- ACM-ICPC 2018 徐州賽區網路預賽ACM
- 高斯消元
- 2013長沙網路賽H題Hypersphere (蛋疼的題目 神似邀請賽A題)
- ACM-ICPC 2018 瀋陽賽區網路預賽ACM
- [比賽總結]青島理工大學邀請賽比賽總結
- ACM-ICPC 2018 南京賽區網路預賽 __G Lpl and Energy-saving Lamps【線段樹+模擬】ACMLAMP
- 高斯消元法
- 第43屆ACM-ICPC國際大學生程式設計競賽 亞洲區域賽南京站現場賽名額分配相關說明ACM程式設計
- ACM-ICPC 2018 南京賽區網路預賽__L. Magical Girl Haze 【Dijkstra演算法+分層圖思想】ACM演算法
- 2024ccpc濟南邀請賽
- 淺談高斯消元
- 《戰爭藝術大師賽》主播邀請賽賽程過半 晉級門票花落誰家?
- ACM-ICPC 2018 徐州賽區網路預賽 F. Features TrackACM
- 2024CCPC山東邀請賽 IAFCK
- 2024 CCPC 鄭州邀請賽遊記
- ICPC2024 昆明邀請賽 遊記
- 《戰爭藝術》大師賽主播邀請賽B組鏖戰 八位主播貢獻精彩賽事
- ACM-ICPC 2018 徐州賽區網路預賽 I. Characters with Hash【簽到題】ACM
- 高斯消元學習筆記筆記
- 2013程式設計之美全國挑戰賽資格賽之傳話遊戲薦程式設計遊戲
- 第十四屆全國大學生資訊保安競賽——創新實踐能力賽(東北賽區)比賽圓滿落幕
- 首屆“AIIA杯”人工智慧巡迴賽起航,5大賽區邀你來戰AI人工智慧
- HDU 4496D-City2013通化邀請賽D題(並查集 需要壓縮路徑)並查集
- 學習筆記----高斯消元(一)筆記
- 學習筆記----高斯消元(二)筆記
- Note - 高斯消元法(證明略)
- 第十四屆全國大學生資訊保安競賽創新實踐能力賽(華中賽區)比賽成功舉辦
- 2024“圖森未來杯”程式設計邀請賽程式設計
- 第十五屆全國大學生智慧車安徽賽區參賽須知和競賽日程安排