#include <stdio.h>
#include <math.h>
double u(double c1,double l1, double l2){
double x1,x2,x3,ux;
int pp;
x1=l1;
x2=l2;
x3=c1;
pp=0;
if (x1>0.99) pp=100;
if (x1<0) pp=100;
if (x2>0.99) pp=100;
if (x1<0) pp=100;
if (x3<0.01) pp=100;
if (pp>5)x1=0.5;
if (pp>5)x2=0.5;
if (pp>5)x3=0.5;
ux=log(1-x1)+log(1-x2)+log(x3);
if (pp>5)ux=-999;
return ux;
}
double bud(double tk,double tl,double tr){
int s1,s2,j;
double bx,w1,w2,c1,l1,l2;
double th1[11],th2[11];
for (s1=1;s1<=10;s1++){
th1[s1]=0.2*s1;
}
for (s2=1;s2<=10;s2++){
th2[s2]=0.1*s2;
}
bx=0;
for (s1=1;s1<=10;s1++){
for (s2=1;s2<=10;s2++){
w1=(1-tk)*th1[s1];
w2=(1-tl)*th2[s2];
c1=(w1+w2+tr)/3;
l1=1-c1/w1;
l2=1-c1/w2;
j=0;
if (l1<0) j=j+1;
if (l2<0) j=j+1;
if (j==2) l1=0;
if (j==2) l2=0;
if (j==2) c1=tr;
j=0;
if (l1<0) j=j+1;
if (l2>0) j=j+1;
if (j==2) l1=0;
if (j==2) l2=(w2-tr)/(2*w2);
if (j==2) c1=w2*l2+tr;
j=0;
if (l1>0) j=j+1;
if (l2<0) j=j+1;
if (j==2) l2=0;
if (j==2) l1=(w1-tr)/(2*w1);
if (j==2) c1=w1*l1+tr;
bx=bx+th1[s1]*l1+th2[s2]*l2-c1;
}
}
return bx;
}
double trs(double tk,double tl){
double trtr,tr1,tr2,tr3,b1,b2;
int t;
tr1=0.05;
tr2=0.1;
b1=bud(tk,tl,tr1);
t=0;
while (t<100){
b2=bud(tk,tl,tr2);
tr3=tr2-b2*(tr2-tr1)/(b2-b1);
b1=b2;
tr1=tr2;
tr2=tr3;
if (b2*b2<0.0001)t=1000;
t=t+1;
}
return tr2;
}
double wel(double tk,double tl,double tr){
int s1,s2,j;
double we,w1,w2,c1,l1,l2;
double th1[11],th2[11];
for (s1=1;s1<=10;s1++){
th1[s1]=0.2*s1;
}
for (s2=1;s2<=10;s2++){
th2[s2]=0.1*s2;
}
we=0;
for (s1=1;s1<=10;s1++){
for (s2=1;s2<=10;s2++){
w1=(1-tk)*th1[s1];
w2=(1-tl)*th2[s2];
c1=(w1+w2+tr)/3;
l1=1-c1/w1;
l2=1-c1/w2;
j=0;
if (l1<0) j=j+1;
if (l2<0) j=j+1;
if (j==2) l1=0;
if (j==2) l2=0;
if (j==2) c1=tr;
j=0;
if (l1<0) j=j+1;
if (l2>0) j=j+1;
if (j==2) l1=0;
if (j==2) l2=(w2-tr)/(2*w2);
if (j==2) c1=w2*l2+tr;
j=0;
if (l1>0) j=j+1;
if (l2<0) j=j+1;
if (j==2) l2=0;
if (j==2) l1=(w1-tr)/(2*w1);
if (j==2) c1=w1*l1+tr;
we=we+log(c1)+log(1-l1)+log(1-l2);
}
}
return we;
}
int main(void){
double th1[11],th2[11];
int s1,s2,j,t,m1,m2,m3,n1,n2,n3;
double tk,tl,tr,we,h;
double maxw,maxk,maxl,maxtr;
double c[11][11],y1[11][11],y2[11][11];
double c1,l1,l2,w1,w2,u1,u2;
double ux[11][3][3][3],w[10][3][3][3],v[10][3][3][3][11];
double end2[3][3][3],end1[3][3][3],endv[3][3][3];
double endq[3][3][3],endc[3][3][3];
double gotoc[10][3][3][3][11],goto1[10][3][3][3][11],goto2[10][3][3][3][11];
double gotoq[10][3][3][3][11];
double z[11];
int q,qx,mx1,mx2,mx3,nx1,nx2,nx3,e;
double v1,maxv,pp,ww,maxu;
double op1[11],op2[11],opc[11],opq[11];
for (s1=1;s1<=10;s1++){
th1[s1]=0.2*s1;
}
for (s2=1;s2<=10;s2++){
th2[s2]=0.1*s2;
}
maxw=-999;
for (m1=10;m1<=40;m1++){
for (m2=10;m2<=40;m2++){
tk=0.01*m1;
tl=0.01*m2;
tr=trs(tk,tl);
we=wel(tk,tl,tr);
if (we>maxw) maxtr=tr;
if (we>maxw) maxk=tk;
if (we>maxw) maxl=tl;
if (we>maxw) maxw=we;
}
}
tk=maxk;
tl=maxl;
tr=maxtr;
for (s1=1;s1<=10;s1++){
for (s2=1;s2<=10;s2++){
w1=(1-tk)*th1[s1];
w2=(1-tl)*th2[s2];
c1=(w1+w2+tr)/3;
l1=1-c1/w1;
l2=1-c1/w2;
j=0;
if (l1<0) j=j+1;
if (l2<0) j=j+1;
if (j==2) l1=0;
if (j==2) l2=0;
if (j==2) c1=tr;
j=0;
if (l1<0) j=j+1;
if (l2>0) j=j+1;
if (j==2) l1=0;
if (j==2) l2=(w2-tr)/(2*w2);
if (j==2) c1=w2*l2+tr;
j=0;
if (l1>0) j=j+1;
if (l2<0) j=j+1;
if (j==2) l2=0;
if (j==2) l1=(w1-tr)/(2*w1);
if (j==2) c1=w1*l1+tr;
c[s1][s2]=c1;
y1[s1][s2]=th1[s1]*l1;
y2[s1][s2]=th2[s2]*l2;
}
}
for (s2=1;s2<=10;s2++){
h=0.001;
t=0;
while (t<100){
for (s1=1;s1<=10;s1++){
maxu=-999;
for (m1=1;m1<=1;m1++){
for (m2=1;m2<=1;m2++){
l1=(y1[m1][m2])/th1[s1];
l2=(y2[m1][m2])/th2[s2];
c1=c[m1][m2];
u1=u(c1,l1,l2);
if (s2==m2)u1=-999;
if (u1>maxu)maxu=u1;
}
}
z[s1]=maxu;
}
for (s1=1;s1<=10;s1++){
for (n1=-1;n1<=1;n1++){
for (n2=-1;n2<=1;n2++){
for (n3=-1;n3<=1;n3++){
l1=(y1[s1][s2]+n1*h)/th1[s1];
l2=(y2[s1][s2]+n2*h)/th2[s2];
c1=c[s1][s2]+n3*h;
u1=u(c1,l1,l2);
if (z[s1]>u1)u1=-999;
ux[s1][n1+1][n2+1][n3+1]=u1;
}
}
}
}
for (s1=1;s1<=9;s1++){
for (n1=-1;n1<=1;n1++){
for (n2=-1;n2<=1;n2++){
for (n3=-1;n3<=1;n3++){
l1=(y1[s1][s2]+n1*h)/th1[s1+1];
l2=(y2[s1][s2]+n2*h)/th2[s2];
c1=c[s1][s2]+n3*h;
w[s1][n1+1][n2+1][n3+1]=u(c1,l1,l2);
}
}
}
}
for (n1=-1;n1<=1;n1++){
for (n2=-1;n2<=1;n2++){
for (n3=-1;n3<=1;n3++){
for (q=-5;q<=5;q++){
v[1][n1+1][n2+1][n3+1][q+5]=-999;
}
}
}
}
for (n1=-1;n1<=1;n1++){
for (n2=-1;n2<=1;n2++){
for (n3=-1;n3<=1;n3++){
q=n1+n2-n3;
v[1][n1+1][n2+1][n3+1][q+5]=ux[s1][n1+1][n2+1][n3+1];
}
}
}
for (s1=2;s1<=9;s1++){
for (n1=-1;n1<=1;n1++){
for (n2=-1;n2<=1;n2++){
for (n3=-1;n3<=1;n3++){
for (q=-5;q<=5;q++){
u1=ux[s1][n1+1][n2+1][n3+1];
qx=q-n1-n2+n3;
pp=0;
if (qx>5)pp=100;
if (qx<-5)pp=100;
if (pp>10)qx=0;
maxv=-999;
for (m1=-1;m1<=1;m1++){
for (m2=-1;m2<=1;m2++){
for (m3=-1;m3<=1;m3++){
v1=u1+v[s1-1][m1+1][m2+1][m3+1][qx+5];
ww=w[s1-1][m1+1][m2+1][m3+1];
if (ww>u1)v1=-999;
if (v1>maxv)mx1=m1;
if (v1>maxv)mx2=m2;
if (v1>maxv)mx3=m3;
if (v1>maxv)maxv=v1;
}
}
}
if (pp>10)maxv=-999;
v[s1][n1+1][n2+1][n3+1][q+5]=maxv;
gotoc[s1][n1+1][n2+1][n3+1][q+5]=mx3;
goto1[s1][n1+1][n2+1][n3+1][q+5]=mx1;
goto2[s1][n1+1][n2+1][n3+1][q+5]=mx2;
gotoq[s1][n1+1][n2+1][n3+1][q+5]=qx;
}
}
}
}
}
for (n1=-1;n1<=1;n1++){
for (n2=-1;n2<=1;n2++){
for (n3=-1;n3<=1;n3++){
u1=ux[10][n1+1][n2+1][n3+1];
qx=n3-n1-n2;
maxv=-999;
for (m1=-1;m1<=1;m1++){
for (m2=-1;m2<=1;m2++){
for (m3=-1;m3<=1;m3++){
v1=u1+v[9][m1+1][m2+1][m3+1][qx+5];
ww=w[9][m1+1][m2+1][m3+1];
if (ww>u1)v1=-999;
if (v1>maxv)mx1=m1;
if (v1>maxv)mx2=m2;
if (v1>maxv)mx3=m3;
if (v1>maxv)maxv=v1;
}
}
}
endv[n1+1][n2+1][n3+1]=maxv;
endc[n1+1][n2+1][n3+1]=mx3;
end1[n1+1][n2+1][n3+1]=mx1;
end2[n1+1][n2+1][n3+1]=mx2;
endq[n1+1][n2+1][n3+1]=qx;
}
}
}
maxv=-999;
for (n1=-1;n1<=1;n1++){
for (n2=-1;n2<=1;n2++){
for (n3=-1;n3<=1;n3++){
if (endv[n1+1][n2+1][n3+1]>maxv)nx1=n1;
if (endv[n1+1][n2+1][n3+1]>maxv)nx2=n2;
if (endv[n1+1][n2+1][n3+1]>maxv)nx3=n3;
if (endv[n1+1][n2+1][n3+1]>maxv)maxv=endv[n1+1][n2+1][n3+1];
}
}
}
opc[10]=nx3;
op1[10]=nx1;
op2[10]=nx2;
opc[9]=endc[nx1+1][nx2+1][nx3+1];
op1[9]=end1[nx1+1][nx2+1][nx3+1];
op2[9]=end2[nx1+1][nx2+1][nx3+1];
opq[9]=endq[nx1+1][nx2+1][nx3+1];
for (j=1;j<=8;j++){
s1=9-j;
n1=op1[s1+1];
n2=op2[s1+1];
n3=opc[s1+1];
q=opq[s1+1];
opc[s1]=gotoc[s1+1][n1+1][n2+1][n3+1][q+5];
op1[s1]=goto1[s1+1][n1+1][n2+1][n3+1][q+5];
op2[s1]=goto2[s1+1][n1+1][n2+1][n3+1][q+5];
opq[s1]=gotoq[s1+1][n1+1][n2+1][n3+1][q+5];
}
e=0;
for (s1=1;s1<=10;s1++){
e=e+opc[s1]*opc[s1]+op1[s1]*op1[s1]+op2[s1]*op2[s1];
}
for (s1=1;s1<=10;s1++){
c[s1][s2]=c[s1][s2]+opc[s1]*h;
y1[s1][s2]=y1[s1][s2]+op1[s1]*h;
y2[s1][s2]=y2[s1][s2]+op2[s1]*h;
}
if (e<2)h=h/2;
if (h<0.0001)t=1000;
t=t+1;
}
printf("%f",maxv);
printf("\n");
}
return 0;
}
最終更新:2009年12月01日 02:37