http://acm.hust.edu.cn/thx/problem.php?id=1425
再次orz青蛙牛&菊神的神思路,已經把DFS理解得出神入化
思路:首先要知道
(x1,y1,z1)
(x2,y2,z2)
(x3,y3,z3)
的結果除以3模數,和
(x1%3,y1%3,z1%3)
(x2%3,y2%3,z2%3)
(x3%3,y3%3,z3%3)
的結果除以3模數
是一樣的,然後因為abc可能是負數,負數%3=負數或0。
用cnt數組記錄同類的有多少,然後DFS。。。額,比較難說清,看代碼吧。總之很神!
#include <iostream><br />#include <stdio.h><br />#include <string.h></p><p>using namespace std;<br />typedef long long LL;</p><p>int cnt[3][3][3];<br />int tmp[30];<br />int x[4],y[4],z[4];<br />LL cc[4];<br />LL res;</p><p>int cal(){<br /> return x[1]*y[2]*z[3]+x[2]*y[3]*z[1]+x[3]*y[1]*z[2]<br /> -x[3]*y[2]*z[1]-x[2]*y[1]*z[3]-x[1]*y[3]*z[2];<br />}</p><p>void DFS(int depth) {<br /> if (depth == 4) {<br /> if (cal()%3 == 0) {<br /> res = res + cc[1]*cc[2]*cc[3];<br /> }<br /> return ;<br /> }<br /> int i,j,k;<br /> for (i=0; i<=2; ++i){<br /> for (j = 0; j <= 2; ++j){<br /> for (k = 0; k <= 2 ; ++k){<br /> if (cnt[i][j][k]){<br /> cc[depth] = cnt[i][j][k];<br /> cnt[i][j][k]--;<br /> x[depth]=i;<br /> y[depth]=j;<br /> z[depth]=k;<br /> DFS(depth+1);<br /> cnt[i][j][k]++;<br /> }<br /> }<br /> }<br /> }<br />}</p><p>int main()<br />{<br /> //freopen("a.txt","r",stdin);<br /> int t,cas = 0;<br /> scanf ("%d", &t);<br /> while (t --) {<br /> int n;<br /> cas++;<br /> scanf ("%d", &n);<br /> memset(cnt, 0,sizeof (cnt));<br /> for (int i = 0; i < n; i ++) {<br /> int a, b, c;<br /> scanf ("%d%d%d", &a, &b, &c);<br /> cnt[(a % 3 + 3) % 3][(b % 3 + 3) % 3][(c % 3 + 3) % 3] ++; }<br /> res = 0;<br /> DFS(1);<br /> printf("Case #%d: ",cas);<br /> printf("%lld/n",res);<br /> }<br /> return 0;<br />}