HDU4870_Rating_双号从零单排_高斯消元求期望

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4870

原题:

Rating

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 654    Accepted Submission(s): 415 Special Judge

Problem Description
   A little girl loves programming competition very much. Recently, she has found a new kind of programming competition named "TopTopTopCoder". Every user who has registered in "TopTopTopCoder" system will have a rating, and the initial value of rating equals to zero. After the user participates in the contest held by "TopTopTopCoder", her/his rating will be updated depending on her/his rank. Supposing that her/his current rating is X, if her/his rank is between on 1-200 after contest, her/his rating will be min(X+50,1000). Her/His rating will be max(X-100,0) otherwise. To reach 1000 points as soon as possible, this little girl registered two accounts. She uses the account with less rating in each contest. The possibility of her rank between on 1 - 200 is P for every contest. Can you tell her how many contests she needs to participate in to make one of her account ratings reach 1000 points?
 
Input
   There are several test cases. Each test case is a single line containing a float number P (0.3 <= P <= 1.0). The meaning of P is described above.
 
Output
   You should output a float number for each test case, indicating the expected count of contest she needs to participate in. This problem is special judged. The relative error less than 1e-5 will be accepted.
 
Sample Input
1.000000 0.814700
 
Sample Output
39.000000 82.181160
 
Author
FZU
 
Source
 
Recommend
We have carefully selected several similar problems for you:  4882 4881 4880 4879 4878 

题意大概是这样:09开了两个号从零单排,势要上2000分。每个号都是0分开始,赢一场加50分,输一场扣100分,不会跌破0分。09每次都用分低的号来打,给出09打一场的胜率P,求有号达到2000分所要打的场数的期望值。

题解:

就是高斯消元求期望!(其实可以DP,就是先把公式变形一下,弄成可以DP的形式,然后只用写20行……尿了,下面是说高斯消元的,懂了的话就可以自己去DP了……

首先看一下期望怎么求,先把一个号的状态分成21个点,代表0分、50分、100分、……2000分,以后就以1代替50,从零单排上2000分变为从零单排上20分。两个号的话,用(x,y)来表示,为减少状态,设x<=y,这样就去掉了很多个点,最后只剩下两百多个点了。两百多个点与各个状态的对应,这个可以用map、pair来实现。

然后把每个点的输和赢都当一条边,这样能画出一个超碉的有向图。观察图,可以发现每个点都有两个出边(因为都有输和赢嘛,除了最后一个点,因为09刷到2000分就不打了)。设E(P)为P点到终点的步数的期望,E(终点)=0。

有:E(P)=p*E(Q)+q*E(R)+1,Q为P赢了到达的点,R为P输了到达的点,R可以等于P。

对每个P列这个式子,这样就可以列一个21元一次方程组,然后用高斯消元求解方程组,E(0,0)就为所求的期望。

我的高斯消元是从http://blog.csdn.net/duanxian0621/article/details/7408887 弄来的,把整数改成小数,整了挺久才弄好。

代码:

 /* 用于求整数解得方程组. */

 #include <iostream>
#include<cstdio>
#include <cstring>
#include <cmath> #include<map> #define mp make_pair
using namespace std; const int maxn = ; int equ, var; // 有equ个方程,var个变元。增广阵行数为equ, 分别为0到equ - 1,列数为var + 1,分别为0到var.
double a[maxn][maxn];
double x[maxn]; // 解集.
bool free_x[maxn]; // 判断是否是不确定的变元.
int free_num; map<pair<int,int>,int> S;
int lit[maxn];
int big[maxn]; void Debug(void)
{
int i, j;
for (i = ; i < equ; i++)
{
for (j = ; j < var + ; j++)
{
printf("%6.2lf",a[i][j]);
}
cout << endl;
}
cout << endl;
} //inline int gcd(int a, int b)
//{
// int t;
// while (b != 0)
// {
// t = b;
// b = a % b;
// a = t;
// }
// return a;
//}
//
//inline int lcm(int a, int b)
//{
// return a * b / gcd(a, b);
//} // 高斯消元法解方程组(Gauss-Jordan elimination).(-2表示有浮点数解,但无整数解,-1表示无解,0表示唯一解,大于0表示无穷解,并返回*变元的个数)
int Gauss(void)
{
int i, j, k;
int max_r; // 当前这列绝对值最大的行.
int col; // 当前处理的列.
double ta, tb;
int LCM;
double teS;
int free_x_num;
int free_index;
// 转换为阶梯阵.
col = ; // 当前处理的列.
for (k = ; k < equ && col < var; k++, col++)
{
// 枚举当前处理的行.
// 找到该col列元素绝对值最大的那行与第k行交换.(为了在除法时减小误差)
max_r = k;
for (i = k + ; i < equ; i++)
{
if (fabs(a[i][col]) > fabs(a[max_r][col])) max_r = i;
}
if (max_r != k)
{
// 与第k行交换.
for (j = k; j < var + ; j++) swap(a[k][j], a[max_r][j]);
}
if (a[k][col] == )
{
// 说明该col列第k行以下全是0了,则处理当前行的下一列.
k--;
continue;
}
for (i = k + ; i < equ; i++)
{
// 枚举要删去的行.
if (a[i][col] != )
{
// LCM = lcm(fabs(a[i][col]), fabs(a[k][col]));
// ta = LCM / fabs(a[i][col]), tb = LCM / fabs(a[k][col]);
ta=1.0;
tb=fabs(a[i][col])/fabs(a[k][col]);
if (a[i][col] * a[k][col] < ) tb = -tb; // 异号的情况是两个数相加.
for (j = col; j < var + ; j++)
{
a[i][j] = a[i][j] * ta - a[k][j] * tb;
}
}
}
}
//Debug();
// 1. 无解的情况: 化简的增广阵中存在(0, 0, ..., a)这样的行(a != 0).
for (i = k; i < equ; i++)
{
// 对于无穷解来说,如果要判断哪些是*变元,那么初等行变换中的交换就会影响,则要记录交换.
if (a[i][col] != ) return -;
}
// 2. 无穷解的情况: 在var * (var + 1)的增广阵中出现(0, 0, ..., 0)这样的行,即说明没有形成严格的上三角阵.
// 且出现的行数即为*变元的个数.
if (k < var)
{
// 首先,*变元有var - k个,即不确定的变元至少有var - k个.
for (i = k - ; i >= ; i--)
{
// 第i行一定不会是(0, 0, ..., 0)的情况,因为这样的行是在第k行到第equ行.
// 同样,第i行一定不会是(0, 0, ..., a), a != 0的情况,这样的无解的.
free_x_num = ; // 用于判断该行中的不确定的变元的个数,如果超过1个,则无法求解,它们仍然为不确定的变元.
for (j = ; j < var; j++)
{
if (a[i][j] != && free_x[j]) free_x_num++, free_index = j;
}
if (free_x_num > ) continue; // 无法求解出确定的变元.
// 说明就只有一个不确定的变元free_index,那么可以求解出该变元,且该变元是确定的.
teS = a[i][var];
for (j = ; j < var; j++)
{
if (a[i][j] != && j != free_index) teS -= a[i][j] * x[j];
}
x[free_index] = teS / a[i][free_index]; // 求出该变元.
free_x[free_index] = ; // 该变元是确定的.
}
return var - k; // *变元有var - k个.
}
// 3. 唯一解的情况: 在var * (var + 1)的增广阵中形成严格的上三角阵.
// 计算出Xn-1, Xn-2 ... X0.
for (i = var - ; i >= ; i--)
{
teS = a[i][var];
for (j = i + ; j < var; j++)
{
if (a[i][j] != ) teS -= a[i][j] * x[j];
}
//if (teS % a[i][i] != 0) return -2; // 说明有浮点数解,但无整数解.
x[i] = teS / a[i][i];
}
return ;
} int main(void)
{
int i, j,k;
int lvln=;//
int fin;
double P,Q;
k=;
for(i=; i<lvln-; i++)
for(j=; j<=i; j++)
{
lit[k]=j;
big[k]=i;
S[mp(j,i)]=k;
k++;
}
lit[k]=lvln-;
big[k]=lvln-;
S[mp(lit[k],big[k])]=k;
k++;
equ=k;
var=k;
while (scanf("%lf", &P) != EOF)
{
Q=1.0-P;
memset(a, , sizeof(a));
memset(x, , sizeof(x));
memset(free_x, , sizeof(free_x)); // 一开始全是不确定的变元.
// for (i = 0; i < equ; i++)
// for (j = 0; j < var + 1; j++)
// scanf("%d", &a[i][j]);
a[k-][k-]=;
a[k-][var]=;
for(i=; i<k-; i++)
{
//cout<<i<<'.'<<lit[i]<<','<<big[i]<<endl;
if(lit[i]!=big[i]) a[i][S[mp(lit[i]+,big[i])]]+=P;
else a[i][S[mp(big[i],lit[i]+)]]+=P;
a[i][S[mp(max(lit[i]-,),big[i])]]+=Q;
//cout<<max(lit[i]-2,0)<<','<<big[i]<<','<<S[mp(max(lit[i]-2,0),big[i])]<<endl;
a[i][var]-=1.0;
a[i][i]-=1.0;
}
//Debug();
free_num = Gauss();
//Debug();
// if (free_num == -1) printf("无解!\n");
// else if (free_num == -2) printf("有浮点数解,无整数解!\n");
// else if (free_num > 0)
// {
// printf("无穷多解! *变元个数为%d\n", free_num);
// for (i = 0; i < var; i++)
// {
// if (free_x[i]) printf("x%d 是不确定的\n", i + 1);
// else printf("x%d: %d\n", i + 1, x[i]);
// }
// }
// else
// {
// for (i = 0; i < var; i++)
// {
// printf("x%d %d,%d: %lf\n", i + 1,lit[i],big[i], x[i]);
// }
// }
// printf("\n");
printf("%.6lf\n",x[]);
}
return ;
}
上一篇:错误:curl: (56) LibreSSL SSL_read: SSL_ERROR_SYSCALL, errno 54


下一篇:CSS: hover选择器的使用