题目链接:https://cn.vjudge.net/problem/ZOJ-3777
The 11th Zhejiang Provincial Collegiate Programming Contest is coming! As a problem setter, Edward is going to arrange the order of the problems. As we know, the arrangement will have a great effect on the result of the contest. For example, it will take more time to finish the first problem if the easiest problem hides in the middle of the problem list.
There are N problems in the contest. Certainly, it's not interesting if the problems are sorted in the order of increasing difficulty. Edward decides to arrange the problems in a different way. After a careful study, he found out that the i-th problem placed in the j-th position will add Pij points of "interesting value" to the contest.
Edward wrote a program which can generate a random permutation of the problems. If the total interesting value of a permutation is larger than or equal to M points, the permutation is acceptable. Edward wants to know the expected times of generation needed to obtain the first acceptable permutation.
Input
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:
The first line contains two integers N (1 <= N <= 12) and M (1 <= M <= 500).
The next N lines, each line contains N integers. The j-th integer in the i-th line is Pij (0 <= Pij <= 100).
Output
For each test case, output the expected times in the form of irreducible fraction. An irreducible fraction is a fraction in which the numerator and denominator are positive integers and have no other common divisors than 1. If it is impossible to get an acceptable permutation, output "No solution" instead.
Sample Input
2
3 10
2 4 1
3 2 2
4 5 3
2 6
1 3
2 4
Sample Output
3/1
No solution
题意:输入n和m,接下来一个n*n的矩阵,a[i][j]表示第i道题放在第j个顺序做可以加a[i][j]的分数,问做完n道题所得分数大于等于m的概率。用分数表示,分母为上述满足题意的方案数,分子是总的方案数,输出最简形式。
思路:由于总的方案数为n! ,简化为求给一个n*n的矩阵,每一行每一列各选一个数使得n个数之和大于等于m的方案数。
n的范围是1 <= n <= 12,每一列选与不选分别用1和0表示,状态数最多达到(1<<12)-1。
dp[sta][score]表示状态为sta得分为score的方案数。
当递推到任意一行i时,都有一个确定的状态数sta对应当前状态哪些列已经被选过。
在当前状态下,对于某一列j,若sta&(1<<j) == 0说明第j列还未选,继而可以由第j列来更新,否则说明第j列已经被选。
https://blog.csdn.net/u013081425/article/details/23677585
#include<bits/stdc++.h>
using namespace std;
int dp[1<<12][505];
int a[15][15];
int f[15];
void initf(){
f[0]=1;
for(int i=1;i<=12;i++)
f[i]=f[i-1]*i;
}
int gcd(int a,int b){
return b?gcd(b,a%b):a;
}
int main(){
initf();
int T;
scanf("%d",&T);
while(T--){
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
scanf("%d",&a[i][j]);
}
}
//初始化
int up=1<<n;
for(int i=0;i<up;i++){
for(int j=0;j<=m;j++){
dp[i][j]=0;
}
}
dp[0][0]=1;
for(int i=0;i<up;i++){
int cnt=0;//cnt表示已经选了前cnt道题(或者说已经确定了前cnt行选了哪几列)
for(int j=1;j<=n;j++){
if(i&(1<<(j-1))) cnt++;
}
for(int j=1;j<=n;j++){
if(i&(1<<(j-1))) continue;//如果这一列已经被选过了,continue
for(int k=0;k<=m;k++){
if(k+a[cnt+1][j]>=m){
dp[i+(1<<(j-1))][m]+=dp[i][k];
}
else{
dp[i+(1<<(j-1))][k+a[cnt+1][j]]+=dp[i][k];
}
}
}
}
if(dp[(1<<n)-1][m]==0) printf("No solution\n");
else{
int k=gcd(f[n],dp[(1<<n)-1][m]);
printf("%d/%d\n",f[n]/k,dp[(1<<n)-1][m]/k);
}
}
return 0;
}