1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
| #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<cassert> #include<vector> using namespace std; #define MAXN 1002 #define pb push_back int att[MAXN],n,m,p,T; typedef long long ll; struct frac { ll up, low; frac(ll up = 0, ll low = 1) { if (low < 0) up = -up, low = -low; assert(low); ll g = __gcd(abs(up), low); this->up = up / g, this->low = low / g; } void output() { ll g = __gcd(abs(up), low); this->up /= g; this->low /= g; if(up==0) low=1; printf("%lld/%lld\n", up, low); } frac operator + (const frac &b) const { return frac(up * b.low + low * b.up, low * b.low); } frac operator - (const frac &b) const { return frac(up * b.low - low * b.up, low * b.low); } frac operator * (const frac &b) const { return frac(up * b.up, low * b.low); } frac operator / (const frac &b) const { return frac(up * b.low, low * b.up); } bool operator < (const frac &b) const { return up * b.low < low * b.up; } bool operator == (const frac &b) const { return up * b.low == low * b.up; } bool operator > (const frac& b) const { return b < *this; } bool operator <= (const frac& b) const { return !(b < *this); } bool operator >= (const frac &b) const { return !(*this < b); } bool operator != (const frac &b) const { return up * b.low != low * b.up; } frac operator += (const frac &b) { return *this = *this + b; } frac operator -= (const frac &b) { return*this = *this - b; } frac operator *= (const frac &b) { return *this = *this * b; } frac operator /= (const frac &b) { return *this = *this / b; } }; vector <frac> dp[22];
frac dfs(int n,int st,int p,int left) { if(n<0) return frac(0,1); frac &res=dp[n][st]; if(res!=frac(-1,1)) return res; res=frac(0,1); if(p<=0) return res=frac(1,1); if(left<=0) return res=frac(0,1); int cm=0; for(int i=1;i<=m;i++){ int mm=(1<<(i-1)); if((st&mm)==0) cm++; } if(left>=n+cm){ int tot=0; for(int i=1;i<=m;i++){ int mm=(1<<(i-1)); if((st&(1<<(i-1)))==0) tot+=att[i]; } if(p<=tot) return res=frac(1,1); else return res=frac(0,1); } res+=dfs(n-1,st,p,left+1)*frac(n,n+cm); for(int i=1;i<=m;i++){ int mm=(1<<(i-1)); if((st&mm)==0){ res+=(dfs(n,st|mm,p-att[i],left-1)*frac(1,n+cm)); } } return res; } void init() { int lim=((1<<(m))); for(int i=0;i<=n;i++){ dp[i].resize(lim+1); for(int k=0;k<=lim;k++) dp[i][k]=frac(-1,1); } } int main() { scanf("%d",&T); while(T--){ scanf("%d%d%d",&p,&n,&m); init(); for(int i=1;i<=m;i++) scanf("%d",&att[i]); frac ans=dfs(n,0,p,1); ans.output(); } return 0; }
|