题目描述
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;
}