乍一看不知道该从哪个角度下手
试图考虑每个苹果对答案的贡献
那我们就要先知道这个苹果所在的箱子对答案的贡献
假设我们知道第i个箱子被选中的概率 pipi,能不能直接算每个苹果的贡献呢?
答案是否定的,因为第i个箱子被选中的方案中,每种方案都有不同的苹果总数,这样在题目中的第二轮挑苹果中就不知道拿什么作分母
考虑到所有苹果的数量和不大于5e5,考虑背包
dp[i][j]表示当前考虑完第i个箱子,总共选中了j个球的选法有多少种
注意到 n<=50n<=50,所以用long long是存的下的
显然有转移方程 dp[i][j]=dp[i−1][j]+dp[i−1][j−a[i]](j>=a[i])dp[i][j]=dp[i−1][j]+dp[i−1][j−a[i]](j>=a[i])
这样我们枚举每一个箱子,然后对剩下的n-1个箱子做一遍背包,然后对于每一种球数加上当前箱子里的球数算一算就好了
但是到这还没完,这样的总复杂度是 O(n2W)O(n2W)的,其中 W=200∗n∗kW=200∗n∗k ,不出意外会超时
考虑到我们之前做了n-1次背包,看似十分浪费,考虑从这里优化
我们还用这个dp状态和转移方程,对n个箱子做dp
然后再来一个dp2[i][j]表示在不选i号盒子的前提下,选出j个小球的方案数
转移考虑用所有选出j个球的方案数减去包含了i号箱子的j个球方案数
我们有dp2的方程:dp2[i][j]=dp[i][j]−dp2[i][j−a[i]](j>=a[i])dp2[i][j]=dp[i][j]−dp2[i][j−a[i]](j>=a[i])
减的那部分表示我钦定i号盒子必选,那么只要再选j-a[i]个球就行了,这再选的球中也不含盒子i
这样对于每个盒子里面的每个球,被选中的概率是
于是这道题就做完了,非常好的dp题
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
#include <utility>
#include <cctype>
#include <algorithm>
#include <bitset>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <cmath>
#define LL long long
#define LB long double
#define x first
#define y second
#define Pair pair<int,int>
#define pb push_back
#define pf push_front
#define mp make_pair
#define LOWBIT(x) x & (-x)
using namespace std;
const int MOD=1e9+7;
const LL LINF=2e16;
const int INF=2e9;
const int magic=348;
const double eps=1e-10;
const double pi=3.14159265;
inline int getint()
{
char ch;int res;bool f;
while (!isdigit(ch=getchar()) && ch!='-') {}
if (ch=='-') f=false,res=0; else f=true,res=ch-'0';
while (isdigit(ch=getchar())) res=res*10+ch-'0';
return f?res:-res;
}
class RandomApple
{
public:
LL dp[500048],dp2[500048];int cnt[58][58];
int n,k,sum=0;
inline vector<double> theProbability(vector<string> hundred,vector<string> ten,vector<string> one)
{
int i,j;
n=int(hundred.size());k=int(hundred[0].size());
for (i=1;i<=n;i++)
{
cnt[i][0]=0;
for (j=1;j<=k;j++)
{
cnt[i][j]=(hundred[i-1][j-1]-'0')*100+(ten[i-1][j-1]-'0')*10+one[i-1][j-1]-'0';
sum+=cnt[i][j];cnt[i][0]+=cnt[i][j];
}
}
memset(dp,0ll,sizeof(dp));
dp[0]=1;
for (i=1;i<=n;i++)
for (j=sum;j>=0;j--)
if (j-cnt[i][0]>=0) dp[j]+=dp[j-cnt[i][0]];
vector<double> res(k,0);
for (i=1;i<=n;i++)
{
memset(dp2,0ll,sizeof(dp2));double p=0;
for (j=0;j<=sum;j++)
{
dp2[j]=dp[j];if (j>=cnt[i][0]) dp2[j]-=dp2[j-cnt[i][0]];
p+=double(dp2[j])/((1ll<<n)-1)/(j+cnt[i][0]);
}
for (j=0;j<=k-1;j++)
res[j]+=cnt[i][j+1]*p;
}
return res;
}
};
/*---Debug Part---*/
/*
int main ()
{
RandomApple A;
vector<string> hh,tt,oo;
int nn;
while (scanf("%d",&nn)!=EOF)
{
string ins="";
hh.clear();tt.clear();oo.clear();
int i;
for (i=1;i<=nn;i++) cin>>ins,hh.pb(ins);
for (i=1;i<=nn;i++) cin>>ins,tt.pb(ins);
for (i=1;i<=nn;i++) cin>>ins,oo.pb(ins);
vector<double> res=A.theProbability(hh,tt,oo);
for (i=0;i<int(res.size());i++) printf("%.10lf ",res[i]);
cout<<endl;
}
return 0;
}
*/