Channel
Test instructions: There are 3*n individuals, divided into n groups, each group of three people. Given the K triples, these three people can not team up, ask the last total number of programs to Team
Ideas:
When k=0, there are (c[3*n][3]*c[3*n-3][3]*......*c[3][3])/n! scheme, the expansion can be dp[n]= (3*n)!/n!/6^n.
Obviously it can be written as a recursive: dp[n]=dp[n-1]* (3*n-1) * (3*n-2)/2.
So, let's just say, the answer = Total Scheme number-at least one prohibited combination of +-...... with at least two forbidden combinations
The binary violence tle. DFS will have a lot of pruning, the current several conflicts, nature will not go back to search
Code:
ImportJava.util.*;Importjava.math.*; Public classSolution {Static intGroup[][] =New int[25] [3]; Static intCnt[] =New int[25]; Static BooleanVis[] =New Boolean[3001]; Static intN, k, depth; Static voidInit () { for(inti = 0; I < 25; i++) Cnt[i]= 0; for(inti = 0; i < 3001; i++) Vis[i]=false; } Static voidDFS (intIndexintNow ) { if(now = =depth) { if(depth% 2 = = 1) cnt[depth]++; ElseCnt[depth]--; } Else { for(inti = index; I < K; i++) { if(Vis[group[i][0]] = =false&& Vis[group[i][1]] = =false&& Vis[group[i][2]] = =false) {vis[group[i][0]] = vis[group[i][1]] = vis[group[i][2]] =true; DFS (i+ 1, now + 1); vis[group[i][0]] = vis[group[i][1]] = vis[group[i][2]] =false; } } } } Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in); BigInteger dp[]=Newbiginteger[1001]; BigInteger Res; dp[0] = dp[1] =Biginteger.one; for(inti = 2; I < 1001; i++) Dp[i]= Dp[i-1].multiply (biginteger.valueof (3 * i-1). Multiply (biginteger.valueof (3 * i-2). Divide (Biginteger.valueof (2)); while(In.hasnext ()) {Init (); N=In.nextint (); K=In.nextint (); for(inti = 0; I < K; i++) for(intj = 0; J < 3; J + +) Group[i][j]=In.nextint (); for(depth = 1; depth <= K; depth++) DFS (0, 0); Res=Biginteger.zero; for(inti = 1; I <= K; i++) if(cnt[i]! = 0) Res= Res.add (Dp[n-i].multiply (biginteger.valueof (cnt[i))); SYSTEM.OUT.PRINTLN (Dp[n].subtract (res)); } }}
View Code
"Tolerance + large number" SGU 476 Coach ' s trouble