[UVALive 7143]Room Assignment(Dp)
Description
There are N guests checking in at the front desk of the hotel. 2K (0 ≤ 2K ≤ N) of them are twins.
There are M rooms available. Each room has capacity ci which means how many guests it can hold.
It happens that the total room capacity is N, i.e. c1 + c2 + . . . + cM = N.
The hotel receptionist wonders how many different room assignments to accommodate all guests.
Since the, receptionist cannot tell the two twins in any pair of twins apart, two room assignments are
considered the same if one can be generated from the other by swapping the two twins in each of some
number of pairs. For rooms with capacity greater than 1, it only matters which people are in the room;
they are not considered to be in any particular order within the room.
Solution
題意:m個房間,每個房間有容量ci(總容量為n),n位客人,其中有k對雙胞胎,雙胞胎被看做同樣的人,求方案數
用f[i][j]表示分配到i個房子,還剩j對完整的雙胞胎沒有分配:
f[i][j-k]+=f[i-1][j]*C(j,k)*C(sum-(j-k)*2-k,c[i]-k) sum表示剩下的還需分配的人
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #define Mod 1000000007 #define N 100005 typedef long long LL; using namespace std; int T,kase=0,n,m,K,c[20],p[20]; LL f[20][110],fac[N],inv[N]; void init() {fac[0]=1,inv[1]=1;for(int i=1;i<N;i++)fac[i]=(fac[i-1]*i)%Mod;for(int i=2;i<N;i++)inv[i]=(inv[Mod%i]*(Mod-Mod/i))%Mod;inv[0]=1;for(int i=1;i<N;i++)inv[i]=(inv[i-1]*inv[i])%Mod; } LL C(int m,int n) {if(m<n||m<0||n<0)return 0;return ((fac[m]*inv[n])%Mod*inv[m-n])%Mod; } int main() {init();scanf("%d",&T);while(T--){++kase;memset(f,0,sizeof(f));scanf("%d%d%d",&n,&m,&K);for(int i=1;i<=m;i++){scanf("%d",&c[i]);p[i]=p[i-1]+c[i];}f[0][K]=1;for(int i=1;i<=m;i++){int sum=p[m]-p[i-1];for(int j=0;j<=K;j++){for(int k=0;k<=j;k++){f[i][j-k]+=(f[i-1][j]*C(j,k)%Mod)*C(sum-(j-k)*2-k,c[i]-k)%Mod;f[i][j-k]%=Mod;} }}printf("Case #%d: %d\n",kase,f[m][0]);}return 0; }?
轉載于:https://www.cnblogs.com/Zars19/p/6915665.html
總結
以上是生活随笔為你收集整理的[UVALive 7143]Room Assignment(Dp)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [51nod1384]全排列
- 下一篇: 控制器view生命周期