ZOJ-3777 Problem Arrangement(dp状态压缩)

dp[i][j],若i中有temp个1则表示为前temp个物品已选好位置,且他们的位置情况用i的二进制位表示.j表示在i情况下达到intersting value值为j是的选法个数。

#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <queue>
#include <cmath>
#include <stack>
#include <queue>
#define maxn 40005
#define INF 1e17
using namespace std;
typedef long long ll;

ll dp[1<<13][505];
int num[13][13];
ll gcd(ll a, ll b){
    return b ? gcd(b, a % b) : a;
}
int main(){

//  freopen("in.txt", "r", stdin); 
    int t;

    scanf("%d", &t);
    while(t--){

        int n, m;
        scanf("%d%d", &n, &m);
        for(int i = 0; i < n; i++)
          for(int j = 0; j < n; j++)
           scanf("%d", &num[i][j]);
        memset(dp, 0, sizeof(dp));
        dp[0][0] = 1;
        for(int i = 0; i < (1<<n); i++){    
            int temp = 0;
            for(int j = 0; j < n; j++){
                if(i & (1<<j))
                  temp++;
            }
            for(int j = 0; j < n; j++){
                if(i & (1<<j))
                  continue;
                for(int h = 0; h <= m; h++)
                  if(h + num[temp][j] >= m)
                   dp[i+(1<<j)][m] += dp[i][h];
                  else
                   dp[i+(1<<j)][h+num[temp][j]] += dp[i][h];
            } 
        } 
        ll p = 1;
        for(int i = 1; i <= n; i++)
         p *= i;
        if(dp[(1<<n)-1][m] == 0)
          cout << "No solution" << endl;
        else{
            ll d = gcd(dp[(1<<n)-1][m], p);
            cout << p/d << "/" << dp[(1<<n)-1][m]/d << endl; 
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值