zoj 3777 Problem Arrangement(状压dp)

题目链接

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

下午训练赛,再次因为拖了队友后腿感到什么惭愧……
看到数据量其实想到了状压,但是比赛的时候想的是根据行数枚举状态,然后就是一个三维的数组,dp[i][s][sum]然后跑出来很慢,交了一发结果sf,虽然后来没找出来原因但是应该也会tle。
仔细想想,其实我们没有必要一行行的递推,对于一个状态s,它一定有可以推过去的更大的状态,这个状态所对应的sum值就是当前的sum值+p[i][j],i为s中已有的个数加一,j为当前枚举的点的位置。

//#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdio>
#include <vector>
#include <set>
using namespace std;
const double eps = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
#define ll long long
#define CL(a) memset(a,0,sizeof(a))
const int maxn=5e5+10;
int t;
int dp[1<<13][520];
int a[13][13];
int c[15];
int cnt(int x)
{
    int sum=0;
    for(int i=0; i<13; i++)
        if(x&(1<<i)) sum++;
    return sum;
}


int main()
{
    c[0]=1;
    for(int i=1; i<=12; i++)
        c[i]=c[i-1]*i;
    cin>>t;
    while(t--)
    {
        memset(dp,0,sizeof(dp));
        int n,m;
        cin>>n>>m;
        for(int i=0; i<n; i++)
            for(int j=0; j<n; j++)
                cin>>a[i][j];
        dp[0][0]=1;
        for(int s=0; s<1<<n; s++)
        {
            int num=cnt(s);
            for(int sum=0;sum<=m;sum++)
            {
                if(dp[s][sum]==0) continue;
                for(int j=0;j<n;j++)
                {
                    if(s&(1<<j)) continue;
                    dp[s|(1<<j)][min(m,sum+a[num][n-1-j])]+=dp[s][sum];
                }

            }

        }
        int ans=dp[(1<<n)-1][m];
     //   cout<<ans<<endl;
        if(ans==0) cout<<"No solution"<<endl;
        else
        {
            int p=__gcd(c[n],ans);
            cout<<c[n]/p<<'/'<<ans/p<<endl;
        }
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值