HSI(數學期望)

題目描述

Takahashi is now competing in a programming contest, but he received TLE in a problem where the answer is YES or NO.
When he checked the detailed status of the submission, there were N test cases in the problem, and the code received TLE in M of those cases.
Then, he rewrote the code to correctly solve each of those M cases with 1⁄2 probability in 1900 milliseconds, and correctly solve each of the other N−M cases without fail in 100 milliseconds.
Now, he goes through the following process:
Submit the code.
Wait until the code finishes execution on all the cases.
If the code fails to correctly solve some of the M cases, submit it again.
Repeat until the code correctly solve all the cases in one submission.
Let the expected value of the total execution time of the code be X milliseconds. Print X (as an integer).

Constraints
All input values are integers.
1≤N≤100
1≤M≤min(N,5)

 

輸入

Input is given from Standard Input in the following format:
N M

 

輸出

Print X, the expected value of the total execution time of the code, as an integer. It can be proved that, under the constraints in this problem, X is an integer not exceeding 109.

 

樣例輸入

1 1

 

樣例輸出

3800

 

提示

In this input, there is only one case. Takahashi will repeatedly submit the code that correctly solves this case with 1⁄2 probability in 1900 milliseconds.
The code will succeed in one attempt with 1⁄2 probability, in two attempts with 1⁄4 probability, and in three attempts with 1⁄8 probability, and so on.
Thus, the answer is 1900×1⁄2+(2×1900)×1⁄4+(3×1900)×1⁄8+…=3800.

題解:數學期望題,一開始真的沒弄懂,都快做瘋了。我的理解是先算出每道題都做對的期望,然後再進行10000次左右的模擬。

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m;
    double x,y,z=1.0,sum=0,s,ti;
    cin>>n>>m;
    s=pow(0.5,m);//算出所有TLE題目做對的概率
    x=ti=(1900*m)+(n-m)*100;
    sum=ti*s;//第一次提交的時間
    for(int i=1;i<10000;i++)
    {
        z*=(1-s);//做不對的概率
        x+=ti;//時間增加
        sum+=x*s*z;//此時用的時間乘以做不對的概率再乘以做對的概率
    }
    cout<<sum<<endl;
}

下面的是題解給出的AC代碼,有看懂的同學能不教教我。。——。。——


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;

int main()
{
    ll n,m;
    cin>>n>>m;
    cout<<(1ll<<m)<<endl;
    cout<<(1ll<<m)*(m*1900+(n-m)*100)<<endl;
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章