POJ 3744 Scout YYF I
来源:互联网 发布:南京行知基地 军训 编辑:程序博客网 时间:2024/06/05 01:53
分段的概率DP+矩阵快速幂
Scout YYF I
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 4180 Accepted: 1076
Description
YYF is a couragous scout. Now he is on a dangerous mission which is to penetrate into the enemy's base. After overcoming a series difficulties, YYF is now at the start of enemy's famous "mine road". This is a very long road, on which there are numbers of mines. At first, YYF is at step one. For each step after that, YYF will walk one step with a probability of p, or jump two step with a probality of 1-p. Here is the task, given the place of each mine, please calculate the probality that YYF can go through the "mine road" safely.
Input
The input contains many test cases ended with EOF.
Each test case contains two lines.
The First line of each test case is N (1 ≤ N ≤ 10) and p (0.25 ≤ p ≤ 0.75) seperated by a single blank, standing for the number of mines and the probability to walk one step.
The Second line of each test case is N integer standing for the place of N mines. Each integer is in the range of [1, 100000000].
Each test case contains two lines.
The First line of each test case is N (1 ≤ N ≤ 10) and p (0.25 ≤ p ≤ 0.75) seperated by a single blank, standing for the number of mines and the probability to walk one step.
The Second line of each test case is N integer standing for the place of N mines. Each integer is in the range of [1, 100000000].
Output
For each test case, output the probabilty in a single line with the precision to 7 digits after the decimal point.
Sample Input
1 0.5
2
2 0.5
2 4
Sample Output
0.5000000
0.2500000
Source
POJ Monthly Contest - 2009.08.23, Simon
如果不用快速幂(TLE的)。。。。。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 6 using namespace std; 7 8 int n,mine[15]; 9 double p,d1,d2,d3,ans;10 11 int main()12 {13 while(scanf("%d%lf",&n,&p)!=EOF)14 {15 for(int i=1;i<=n;i++)16 {17 scanf("%d",mine+i);18 }19 sort(mine,mine+1+n);20 if(mine[1]==1)21 {22 printf("0.0000000\n"); continue;23 }24 else if(n==0)25 {26 printf("1.0000000\n"); continue;27 }28 bool flag=false;29 for(int i=1;i<n;i++)30 {31 if(mine[i]+1==mine[i+1])32 {33 printf("0.0000000\n"); flag=true; break;34 }35 }36 if(flag==true) continue;37 ans=1.;38 for(int i=1;i<=n;i++)39 {40 int st=mine[i-1]+1,ed=mine[i];41 d2=0.,d1=1.;42 for(int j=st+1;j<=ed;j++)43 {44 d3=d1*p+d2*(1-p);45 d2=d1; d1=d3;46 }47 ans*=(1-d3);48 }49 printf("%.7lf\n",ans);50 }51 return 0;52 }
快速幂的。。。。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 6 using namespace std; 7 8 struct Matrix 9 {10 double a[2][2];11 Matrix() {}12 Matrix(double A,double B,double C,double D)13 {14 a[0][0]=A;a[0][1]=B;a[1][0]=C;a[1][1]=D;15 }16 Matrix operator* (const Matrix& b) const17 {18 Matrix temp;19 memset(temp.a,0,sizeof(temp.a));20 for(int i=0;i<2;i++)21 {22 for(int j=0;j<2;j++)23 {24 for(int k=0;k<2;k++)25 {26 temp.a[i][j]+=a[i][k]*b.a[k][j];27 }28 }29 }30 return temp;31 }32 Matrix Show()33 {34 for(int i=0;i<2;putchar(10),i++) for(int j=0;j<2;putchar(' '),j++) cout<<a[i][j];35 }36 };37 38 Matrix QuickPow(Matrix m,int n)39 {40 Matrix E(1,0,0,1);41 while(n>1)42 {43 if(n&1) E=E*m;44 m=m*m;45 n=n>>1;46 }47 E=E*m;48 return E;49 }50 51 int n,mine[20];52 double p,ans;53 54 int main()55 {56 while(scanf("%d%lf",&n,&p)!=EOF)57 {58 for(int i=1;i<=n;i++)59 scanf("%d",mine+i);60 sort(mine,mine+n+1);61 if(mine[1]==1)62 {63 printf("0.0000000\n"); continue;64 }65 else if(n==0)66 {67 printf("1.0000000\n"); continue;68 }69 bool flag=false;70 for(int i=1;i<n;i++)71 {72 if(mine[i]+1==mine[i+1])73 {74 printf("0.0000000\n"); flag=true; break;75 }76 }77 if(flag==true) continue;78 ans=1.;79 for(int i=1;i<=n;i++)80 {81 Matrix m(p,1-p,1,0);82 m=QuickPow(m,mine[i]-mine[i-1]-1);83 ans*=1-m.a[0][0];84 }85 printf("%.7lf\n",ans);86 }87 return 0;88 }
0 0
- poj 3744 Scout YYF I
- poj 3744 Scout YYF I
- poj 3744 Scout YYF I
- POJ 3744 Scout YYF I
- POJ 3744 Scout YYF I
- POJ 3744 Scout YYF I
- POJ 3744 Scout YYF I
- POJ 3744 Scout YYF I
- POJ 3744 Scout YYF I
- poj #3744 Scout YYF I
- POJ 3744 Scout YYF I
- POJ 3744 Scout YYF I
- POJ-3744-Scout YYF I
- poj-Scout YYF I
- Scout YYF I POJ
- Scout YYF I POJ
- poj 3744 Scout YYF I 概率dp
- poj 3744 Scout YYF I 矩阵
- 第十周项目 4 大奖赛计分(拓展1:不定评委数)
- free programming books zh
- sdfsdfsdfds f
- 应用系统中常见报表类型解析
- Tcpdump 的用法
- POJ 3744 Scout YYF I
- 应用系统中交互式报表功能解析
- POJ 3071 Football
- CodeForces 148D. Bag of mice
- ASP.NET MVC 5 - 查询Details和Delete方法
- POJ 2151 Check the difficulty of problems
- [LeetCode]Word Ladder 字符串的最短转换距离 (Dijkstra)
- 报表应用系统中如何正确使用图表功能
- SGU 495. Kids and Prizes