A. Sticker Album (expected dp)

Topic portal

Question: You have to collect n cards. There are some card packs on the market. There are some cards in each pack. The number of cards in each pack is independent. It is a random integer value in the interval [a, b]. How many packs do you expect to buy to get n cards together.

Idea: Consider using f[i] to indicate how many packages you expect to purchase when you have i cards in your hand. Then out of the state f[n]=0, we require f[0]. After clarifying the goal and considering the state transition, we buy a pack and get at least a cards and at most b cards. Then f[i] will be transferred from f[i+a]~f[i+b], that is, f [i] = ∑ (f [j] + 1) b − a + 1, j ∈ [i + a, i + b] f[i]=\sum \frac{(f[j]+1)}{b-a+1},j∈[i+a,i+b]f[i]=ba+1(f[j]+1),j[i+a,i+b ] . But if a is equal to 0, special treatment is required. According to this formula, when f[i] is raised to one side,f [i] = ∑ f [j] + 1 b − a + 1 + 1 b − a, j ∈ [a + 1, b] f[ i]=\sum \frac{f[j]+1}{ba}+1+\frac{1}{ba},j∈[a+1,b]f[i]=baf[j]+1+1+ba1,j[a+1,b]

Code:

#include<bits/stdc++.h>
#define endl '\n'
#define mp make_pair
#define pb push_back
#define ll long long
#define int long long
#define pii pair<int,int>
#define sz(x) (int)(x).size()
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
char *fs,*ft,buf[1<<20];
#define gc() (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<20,stdin),fs==ft))?0:*fs++;
inline int read()
{
    
    
    int x=0,f=1;
    char ch=gc();
    while(ch<'0'||ch>'9')
    {
    
    
        if(ch=='-')
            f=-1;
        ch=gc();
    }
    while(ch>='0'&&ch<='9')
    {
    
    
        x=x*10+ch-'0';
        ch=gc();
    }
    return x*f;
}
using namespace std;
const int N=1e6+10;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
const double eps=1e-7;

double f[N],suf[N];

int solve()
{
    
    
    int n,a,b;
    cin>>n>>a>>b;
    f[n] = 0;
    suf[n] = 0;
    for(int i=n-1; i>=0; i--)
    {
    
    
        if(a!=0)
            f[i] = (suf[i+a]-suf[i+b+1])/(b-a+1)+1;
        else
            f[i] = (suf[i+a+1]-suf[i+b+1]+(b-a+1))/(b-a);
        suf[i] = suf[i+1]+f[i];
    }
    printf("%.7f",f[0]);
}

signed main()
{
    
    
//    int _;
//    cin>>_;
//    while(_--)
    solve();

    return 0;
}

Guess you like

Origin blog.csdn.net/Joker_He/article/details/113201022