暑假训练-训练7 概率DP

A - Aeroplane chess
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit

Status
Description
Hzz loves aeroplane chess very much. The chess map contains N+1 grids labeled from 0 to N. Hzz starts at grid 0. For each step he throws a dice(a dice have six faces with equal probability to face up and the numbers on the faces are 1,2,3,4,5,6). When Hzz is at grid i and the dice number is x, he will moves to grid i+x. Hzz finishes the game when i+x is equal to or greater than N.

There are also M flight lines on the chess map. The i-th flight line can help Hzz fly from grid Xi to Yi (0



#include<stdio.h>
#include<string>
#include<cstring>
#include<queue>
#include<algorithm>
#include<functional>
#include<vector>
#include<iomanip>
#include<math.h>
#include<iostream>
#include<sstream>
#include<set>
using namespace std;
const int MAX=100010;
int N,M,A[MAX],a,b;
double F[MAX];
int main()
{
    cout<<fixed<<setprecision(4);
    while (cin>>N>>M)
    {
        if (N==0&&M==0) break;
        memset(F,0,sizeof(F));
        memset(A,-1,sizeof(A));
        for (int i=0; i<M; i++)
        {
            cin>>a>>b;
            A[a]=b; //a->b
        }
        for (int i=N-1;i>=0;i--)
        {
            if (A[i]!=-1)
                F[i]=F[A[i]];
            else
                F[i]=F[i+1]/6+F[i+2]/6+F[i+3]/6+F[i+4]/6+F[i+5]/6+F[i+6]/6+1.0;
        }
        cout<<F[0]<<endl;
    }
    return 0;
}

B - Collecting Bugs
Time Limit:10000MS Memory Limit:64000KB 64bit IO Format:%lld & %llu
Submit

Status
Description
Ivan is fond of collecting. Unlike other people who collect post stamps, coins or other material stuff, he collects software bugs. When Ivan gets a new program, he classifies all possible bugs into n categories. Each day he discovers exactly one bug in the program and adds information about it and its category into a spreadsheet. When he finds bugs in all bug categories, he calls the program disgusting, publishes this spreadsheet on his home page, and forgets completely about the program.
Two companies, Macrosoft and Microhard are in tight competition. Microhard wants to decrease sales of one Macrosoft program. They hire Ivan to prove that the program in question is disgusting. However, Ivan has a complicated problem. This new program has s subcomponents, and finding bugs of all types in each subcomponent would take too long before the target could be reached. So Ivan and Microhard agreed to use a simpler criteria — Ivan should find at least one bug in each subsystem and at least one bug of each category.
Macrosoft knows about these plans and it wants to estimate the time that is required for Ivan to call its program disgusting. It’s important because the company releases a new version soon, so it can correct its plans and release it quicker. Nobody would be interested in Ivan’s opinion about the reliability of the obsolete version.
A bug found in the program can be of any category with equal probability. Similarly, the bug can be found in any given subsystem with equal probability. Any particular bug cannot belong to two different categories or happen simultaneously in two different subsystems. The number of bugs in the program is almost infinite, so the probability of finding a new bug of some category in some subsystem does not reduce after finding any number of bugs of that category in that subsystem.
Find an average time (in days of Ivan’s work) required to name the program disgusting.
Input
Input file contains two integer numbers, n and s (0 < n, s <= 1 000).
Output
Output the expectation of the Ivan’s working days needed to call the program disgusting, accurate to 4 digits after the decimal point.
Sample Input
1 2
Sample Output
3.0000



B:


#include<stdio.h>
#include<string>
#include<cstring>
#include<queue>
#include<algorithm>
#include<functional>
#include<vector>
#include<iomanip>
#include<math.h>
#include<iostream>
#include<sstream>
#include<set>
using namespace std;
const int MAX=1010;
double F[MAX][MAX];
double n,s;
int main()
{
    cout<<fixed<<setprecision(4);
    while (cin>>n>>s)
    {
        memset(F,0,sizeof(F));
        for (int i = n; i >= 0; i--)
            for (int j = s; j >= 0; j--)
            {
                if ( i == n && j == s ) continue;
                F[i][j] = (n*s + (n-i)*j*F[i+1][j] + i*(s-j)*F[i][j+1] + (n-i)*(s-j)*F[i+1][j+1] )/(n*s - i*j );
            }
        cout<<F[0][0]<<endl;
    }
    return 0;
}

C - Kids and Prizes
Time Limit:250MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit

Status
Description

ICPC (International Cardboard Producing Company) is in the business of producing cardboard boxes. Recently the company organized a contest for kids for the best design of a cardboard box and selected M winners. There are N prizes for the winners, each one carefully packed in a cardboard box (made by the ICPC, of course). The awarding process will be as follows:
All the boxes with prizes will be stored in a separate room.
The winners will enter the room, one at a time.
Each winner selects one of the boxes.
The selected box is opened by a representative of the organizing committee.
If the box contains a prize, the winner takes it.
If the box is empty (because the same box has already been selected by one or more previous winners), the winner will instead get a certificate printed on a sheet of excellent cardboard (made by ICPC, of course).
Whether there is a prize or not, the box is re-sealed and returned to the room.
The management of the company would like to know how many prizes will be given by the above process. It is assumed that each winner picks a box at random and that all boxes are equally likely to be picked. Compute the mathematical expectation of the number of prizes given (the certificates are not counted as prizes, of course).
Input
The first and only line of the input file contains the values of N and M ( ).
Output
The first and only line of the output file should contain a single real number: the expected number of prizes given out. The answer is accepted as correct if either the absolute or the relative error is less than or equal to 10 -9.
Sample Input
sample input
sample output
5 7
3.951424

sample input
sample output
4 3
2.3125


C: F[i]为第i个人抽中奖品的概率

#include<stdio.h>
#include<string>
#include<cstring>
#include<queue>
#include<algorithm>
#include<functional>
#include<vector>
#include<iomanip>
#include<math.h>
#include<iostream>
#include<sstream>
#include<set>
using namespace std;
const int MAX=100005;
int N,M;
double F[MAX];
int main()
{
    cout<<fixed<<setprecision(10);
    while (cin>>N>>M)
    {
        memset(F,-1,sizeof(F));
        F[1]=1;
        for (int i=2;i<=M;i++)
            F[i]=(1-F[i-1])*F[i-1]+F[i-1]*(F[i-1]-(double)(1.0/N));
        double ans=0;
        for (int i=1;i<=M;i++) ans+=F[i];
        cout<<ans<<endl;
    }
    return 0;
}

D - LOOPS
Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit

Status
Description
Akemi Homura is a Mahou Shoujo (Puella Magi/Magical Girl).

Homura wants to help her friend Madoka save the world. But because of the plot of the Boss Incubator, she is trapped in a labyrinth called LOOPS.

The planform of the LOOPS is a rectangle of R*C grids. There is a portal in each grid except the exit grid. It costs Homura 2 magic power to use a portal once. The portal in a grid G(r, c) will send Homura to the grid below G (grid(r+1, c)), the grid on the right of G (grid(r, c+1)), or even G itself at respective probability (How evil the Boss Incubator is)!
At the beginning Homura is in the top left corner of the LOOPS ((1, 1)), and the exit of the labyrinth is in the bottom right corner ((R, C)). Given the probability of transmissions of each portal, your task is help poor Homura calculate the EXPECT magic power she need to escape from the LOOPS.

Input
The first line contains two integers R and C (2 <= R, C <= 1000).

The following R lines, each contains C*3 real numbers, at 2 decimal places. Every three numbers make a group. The first, second and third number of the cth group of line r represent the probability of transportation to grid (r, c), grid (r, c+1), grid (r+1, c) of the portal in grid (r, c) respectively. Two groups of numbers are separated by 4 spaces.

It is ensured that the sum of three numbers in each group is 1, and the second numbers of the rightmost groups are 0 (as there are no grids on the right of them) while the third numbers of the downmost groups are 0 (as there are no grids below them).

You may ignore the last three numbers of the input data. They are printed just for looking neat.

The answer is ensured no greater than 1000000.

Terminal at EOF

Output
A real number at 3 decimal places (round to), representing the expect magic power Homura need to escape from the LOOPS.

Sample Input
2 2
0.00 0.50 0.50 0.50 0.00 0.50
0.50 0.50 0.00 1.00 0.00 0.00
Sample Output
6.000


D:要注意stay==1的情况 F[i][j]应该要等于0 而且double不能直接用==判断 要fabs()相减

#include<stdio.h>
#include<string>
#include<cstring>
#include<queue>
#include<algorithm>
#include<functional>
#include<vector>
#include<iomanip>
#include<math.h>
#include<iostream>
#include<sstream>
#include<set>
using namespace std;
const int MAX=1005;
const int MIN=1e-6;
struct portal
{
    double stay,right,down;
};
double F[MAX][MAX];
bool used[MAX][MAX];
portal pic[MAX][MAX];
int R,C;
double dfs(int i,int j)
{
    if (i<=0||i>R||j<=0||j>C) return 0;
    if (used[i][j]) return F[i][j];
    if (fabs(double(1)-pic[i][j].stay)<=MIN)
    {
        used[i][j]=true;
        return F[i][j]=0.0;
    }
    used[i][j]=true;
    return F[i][j]=(double(2)+pic[i][j].right*dfs(i,j+1)+pic[i][j].down*dfs(i+1,j))/(double(1)-pic[i][j].stay);
}
int main()
{
    cout<<fixed<<setprecision(3);
    while(scanf("%d%d",&R,&C)!=EOF)
    {
        for (int i=0; i<MAX; i++)
            for (int j=0; j<MAX; j++)
            {
                F[i][j]=-1;
                used[i][j]=false;
            }
        F[R][C]=0;
        used[R][C]=true;
        for (int i=1; i<=R; i++)
            for (int j=1; j<=C; j++)
                scanf("%lf%lf%lf",&pic[i][j].stay,&pic[i][j].right,&pic[i][j].down);
        printf("%.3f\n",dfs(1,1));
    }
    return 0;
}

E - 加尔鲁什·地狱咆哮对阵虚灵大盗拉法姆
Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%lld & %llu
Submit

Status
Description
加尔鲁什·地狱咆哮看虚灵大盗拉法姆不顺眼已经很久了,终于一天,加尔鲁什·地狱咆哮带着自己的手下恐怖的奴隶主堵住了虚灵大盗拉法姆,虚灵大盗拉法姆拉法姆见势不妙,掏出神器死亡丧钟准备还击。场面十分壮观,如图:

已知恐怖丧钟每一发击中敌方单位都将令对方的生命值减一,且击中每个敌方单位的概率是相等的。而恐怖的奴隶主有3点生命值,当其在场上受到非致命伤害且场上恐怖的奴隶主总数小于7时会召唤一个新的3点生命值的恐怖的奴隶主,受到致命伤害(受到攻击后生命为0)时则会直接死去。如场上有1个生命值为3的“恐怖的奴隶主”,当恐怖丧钟打中他时,他的生命值变为2,且召唤一个新的奴隶主。而当恐怖丧钟击中加尔鲁什时,恐怖的奴隶主只会强力围观却什么都不会做。

现知,恐怖丧钟共计会发射X次,加尔鲁什有Y点体力值,而他手下共有Z名生命值为三的奴隶主。问在死亡丧钟使用完毕后,有多大的概率杀死加尔鲁什?(答案保留小数点后6位)

Input
多组数据,第一行有一个整数T,表示有T组数据。(T<=100)
以下T行,每行有三个整数X,Y和Z。(1<=X,Y<=20,0<=Z<=7)

Output
一个小数(保留小数点后六位)。

Sample Input
4
1 1 1
2 1 1
2 1 2
2 2 2
Sample Output
0.500000
0.666667
0.500000
0.111111


#include<stdio.h>
#include<string>
#include<cstring>
#include<queue>
#include<algorithm>
#include<functional>
#include<vector>
#include<iomanip>
#include<math.h>
#include<iostream>
#include<sstream>
#include<set>
using namespace std;
double F[22][22][10][10][10]; //F[X][Y][3][2][1]
int X,Y,Z;
double dfs(int x,int y,int aaa,int aa,int a)
{
    if (F[x][y][aaa][aa][a]!=-1) return F[x][y][aaa][aa][a];
    if (y==0) return F[x][y][aaa][aa][a]=1.0;
    if (x<y||x==0)  return F[x][y][aaa][aa][a]=0.0;
    F[x][y][aaa][aa][a]=double(1)/(aaa+aa+a+1)*dfs(x-1,y-1,aaa,aa,a);
    if (aaa+aa+a<7)
    {
        if (aaa>0) F[x][y][aaa][aa][a]+=double(aaa)/(aaa+aa+a+1)*dfs(x-1,y,aaa,aa+1,a);
        if (aa>0) F[x][y][aaa][aa][a]+=double(aa)/(aaa+aa+a+1)*dfs(x-1,y,aaa+1,aa-1,a+1);
        if (a>0) F[x][y][aaa][aa][a]+=double(a)/(aaa+aa+a+1)*dfs(x-1,y,aaa,aa,a-1);
    }
    else
    {
        if (aaa>0) F[x][y][aaa][aa][a]+=double(aaa)/(aaa+aa+a+1)*dfs(x-1,y,aaa-1,aa+1,a);
        if (aa>0) F[x][y][aaa][aa][a]+=double(aa)/(aaa+aa+a+1)*dfs(x-1,y,aaa,aa-1,a+1);
        if (a>0) F[x][y][aaa][aa][a]+=double(a)/(aaa+aa+a+1)*dfs(x-1,y,aaa,aa,a-1);
    }
    return F[x][y][aaa][aa][a];
}
int main()
{
    cout<<fixed<<setprecision(6);
    int T;
    cin>>T;
    while(T--)
    {
        for (int x=0;x<22;x++)
            for (int y=0;y<22;y++)
                for (int aaa=0;aaa<10;aaa++)
                    for (int aa=0;aa<10;aa++)
                        for (int a=0;a<10;a++)
                            F[x][y][aaa][aa][a]=-1;
        cin>>X>>Y>>Z;
        cout<<dfs(X,Y,Z,0,0)<<endl;
    }
    return 0;
}

G - Bag of mice
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit

Status
Description
The dragon and the princess are arguing about what to do on the New Year’s Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance.

They take turns drawing a mouse from a bag which initially contains w white and b black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn’t scare other mice). Princess draws first. What is the probability of the princess winning?

If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one.

Input
The only line of input data contains two integers w and b (0 ≤ w, b ≤ 1000).

Output
Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 9.

Sample Input
Input
1 3
Output
0.500000000
Input
5 5
Output
0.658730159
Hint
Let’s go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 * 2/3 = 1/2. After this there are two mice left in the bag — one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. If the princess’ mouse is white, she wins (probability is 1/2 * 1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins.


G: 自己写的一直有问题 感觉逻辑没毛病..交的网上的代码

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<iomanip>
using namespace std;
double dp[1005][1005];
int main()
{
    int w,b;
    scanf("%d%d",&w,&b);
    for(int i=1; i<=b; i++)dp[0][i]=0;
    for(int i=1; i<=w; i++)dp[i][0]=1.0;
    for(int i=1; i<=w; i++)
        for(int j=1; j<=b; j++)
        {
            if(dp[i][j])continue;
            if(j>=2)
                dp[i][j]+=(j*1.0/(i+j)*(j-1)*1.0/(i+j-1)*i*1.0/(i+j-2))*dp[i-1][j-2];
            if(j>=3)
                dp[i][j]+=(j*1.0/(i+j)*(j-1)*1.0/(i+j-1)*(j-2)*1.0/(i+j-2))*dp[i][j-3];
            dp[i][j]+=i*1.0/(i+j);
        }
    printf("%.10f\n",dp[w][b]);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值