标签:
题目链接:
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> //#include <bits/stdc++.h> #include <stack> #include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++) #define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) { char CH; bool F=false; for(CH=getchar();CH<‘0‘||CH>‘9‘;F= CH==‘-‘,CH=getchar()); for(num=0;CH>=‘0‘&&CH<=‘9‘;num=num*10+CH-‘0‘,CH=getchar()); F && (num=-num); } int stk[70], tp; template<class T> inline void print(T p) { if(!p) { puts("0"); return; } while(p) stk[++ tp] = p%10, p/=10; while(tp) putchar(stk[tp--] + ‘0‘); putchar(‘\n‘); } const LL mod=1e9+7; const double PI=acos(-1.0); const int inf=1e9; const int N=1e5+10; const int maxn=(1<<20)+14; const double eps=1e-12; LL dp[maxn],f[23]; int num[maxn],p,n,m,a[30]; inline void Init() { for(int i=0;i<maxn;i++) { for(int j=0;j<20;j++) { if(i&(1<<j))num[i]++; } } f[0]=1; for(int i=1;i<=20;i++)f[i]=f[i-1]*i; } int cal(int x) { int sum=0; for(int i=0;i<m;i++) { if(x&(1<<i))sum+=a[i]; } return sum; } LL gcd(LL x,LL y) { if(y==0)return x; return gcd(y,x%y); } int counter(int x) { int sum=0; for(int i=0;i<m;i++) { if(x&(1<<i))sum++; } return num[x]-2*sum+1; } int main() { Init(); int t; read(t); while(t--) { mst(dp,0); read(p);read(n);read(m); For(i,0,m-1)read(a[i]); LL ans=0; int tot=(1<<(n+m))-1; dp[0]=1; for(int i=0;i<=tot;i++) { if(!dp[i])continue; if(i==tot||counter(i)==0) { if(cal(i)>=p)ans=ans+dp[i]*f[n+m-num[i]]; } else { for(int j=0;j<n+m;j++) { if(i&(1<<j))continue; dp[i|(1<<j)]+=dp[i]; } } } LL g=gcd(ans,f[n+m]); cout<<ans/g<<"/"<<f[n+m]/g<<"\n"; } return 0; }
hdu-5816 Hearthstone(状压dp+概率期望)
标签:
原文地址:http://www.cnblogs.com/zhangchengc919/p/5759311.html