public class tax502{
public static void main(String[] args) {
double th1[]=new double[11];
double th2[]=new double[11];
double y1[][]=new double[11][11];
double y2[][]=new double[11][11];
double c[][]=new double[11][11];
double us[][][][][]=new double[11][11][3][3][3];
double ws[][][][][]=new double[11][11][3][3][3];
double vs[][][][][][]=new double[11][11][3][3][3][11];
double sumu[][]=new double[11][1001];
double sumv[][][]=new double[11][1001][11];
int gotomx[][][]=new int[11][1001][11];
int gotonx[][][]=new int[11][1001][11];
int sumb[][]=new int[11][1001];
int goto1[][][][][][][]=new int[11][11][3][3][3][11][28];
int goto2[][][][][][][]=new int[11][11][3][3][3][11][28];
int goto3[][][][][][][]=new int[11][11][3][3][3][11][28];
int gotom[][][][][][][]=new int[11][11][3][3][3][11][28];
int op1[][][]=new int[11][11][1001];
int op2[][][]=new int[11][11][1001];
int op3[][][]=new int[11][11][1001];
int opm[][][]=new int[11][11][1001];
int opnx[]=new int[11];
int opmx[]=new int[11];
double tk,tl,tr;
double c1,w1,w2,l1,l2;
double we;
double maxwel,maxtk,maxtl,maxtr;
int m,n,n1,n2,n3,s1,s2,z;
int nx,nx1,nx2,nx3,mx;
int ns,nxs,mxs;
double h;
double u1,v1,maxv;
int num[][][][][][]=new int[11][11][3][3][3][11];
int p,px,j,t1;
for (s1=1;s1<11;s1++){
th1[s1]=0.2*s1;
}
for (s2=1;s2<11;s2++){
th2[s2]=0.1*s2;
}
maxwel=-999;
maxtk=0;
maxtl=0;
maxtr=0;
for (n1=5;n1<40;n1++){
for (n2=5;n2<40;n2++){
tk=0.01*n1;
tl=0.01*n2;
tr=trs(tk,tl,th1,th2);
we=wel(tk,tl,tr,th1,th2);;
if (we>maxwel)maxtk=tk;
if (we>maxwel)maxtl=tl;
if (we>maxwel)maxtr=tr;
if (we>maxwel)maxwel=we;
}
}
tk=maxtk;
tl=maxtl;
tr=maxtr;
for (s1=1;s1<11;s1++){
for (s2=1;s2<11;s2++){
l1=mlx(s1,s2,tk,tl,tr,th1,th2);
l2=flx(s1,s2,tk,tl,tr,th1,th2);
w1=(1-tk)*th1[s1];
w2=(1-tl)*th2[s2];
y1[s1][s2]=th1[s1]*l1;
y2[s1][s2]=th2[s2]*l2;
c[s1][s2]=w1*l1+w2*l2+tr;
}
}
System.out.println(tk);
System.out.println(tl);
h=0.001;
t1=0;
while(t1<100){
for (s1=1;s1<11;s1++){
for (s2=1;s2<11;s2++){
for (n1=-1;n1<2;n1++){
for (n2=-1;n2<2;n2++){
for (n3=-1;n3<2;n3++){
c1=c[s1][s2]+n3*h;
l1=(y1[s1][s2]+n1*h)/th1[s1];
l2=(y2[s1][s2]+n2*h)/th2[s2];
us[s1][s2][n1+1][n2+1][n3+1]=ux(c1,l1,l2);
}
}
}
}
}
for (s1=1;s1<11;s1++){
for (s2=1;s2<10;s2++){
for (n1=-1;n1<2;n1++){
for (n2=-1;n2<2;n2++){
for (n3=-1;n3<2;n3++){
c1=c[s1][s2]+n3*h;
l1=(y1[s1][s2]+n1*h)/th1[s1];
l2=(y2[s1][s2]+n2*h)/th2[s2+1];
ws[s1][s2][n1+1][n2+1][n3+1]=ux(c1,l1,l2);
}
}
}
}
}
for (s1=1;s1<11;s1++){
for (n1=-1;n1<2;n1++){
for (n2=-1;n2<2;n2++){
for (n3=-1;n3<2;n3++){
for (m=-5;m<6;m++){
vs[s1][1][n1+1][n2+1][n3+1][m+5]=-999;
}
}
}
}
}
for (s1=1;s1<11;s1++){
for (n1=-1;n1<2;n1++){
for (n2=-1;n2<2;n2++){
for (n3=-1;n3<2;n3++){
m=n1+n2-n3;
vs[s1][1][n1+1][n2+1][n3+1][m+5]=us[s1][1][n1+1][n2+1][n3+1];
}
}
}
}
for (s1=1;s1<11;s1++){
for (s2=2;s2<11;s2++){
for (n1=-1;n1<2;n1++){
for (n2=-1;n2<2;n2++){
for (n3=-1;n3<2;n3++){
for (m=-5;m<6;m++){
u1=us[s1][s2][n1+1][n2+1][n3+1];
mx=m-n1-n2+n3;
maxv=-999;
z=0;
if (mx>5)z=100;
if (mx<-5)z=100;
if (z>50)mx=0;
p=0;
for (nx1=-1;nx1<2;nx1++){
for (nx2=-1;nx2<2;nx2++){
for (nx3=-1;nx3<2;nx3++){
v1=u1+vs[s1][s2-1][nx1+1][nx2+1][nx3+1][mx+5];
if (ws[s1][s2-1][nx1+1][nx2+1][nx3+1]>u1)v1=-999;
if (v1>maxv)maxv=v1;
px=0;
if (vs[s1][s2-1][nx1+1][nx2+1][nx3+1][mx+5]>-900)px=100;
if (ws[s1][s2-1][nx1+1][nx2+1][nx3+1]>u1)px=0;
if (z>50)px=0;
if (px>50)p=p+1;
if (px>50)goto1[s1][s2][n1+1][n2+1][n3+1][m+5][p]=nx1;
if (px>50)goto2[s1][s2][n1+1][n2+1][n3+1][m+5][p]=nx2;
if (px>50)goto3[s1][s2][n1+1][n2+1][n3+1][m+5][p]=nx3;
if (px>50)gotom[s1][s2][n1+1][n2+1][n3+1][m+5][p]=mx;
}
}
}
if (z>50)maxv=-999;
if (z>50)p=0;
num[s1][s2][n1+1][n2+1][n3+1][m+5]=p;
vs[s1][s2][n1+1][n2+1][n3+1][m+5]=maxv;
}
}
}
}
}
}
for (s1=1;s1<11;s1++){
for (n=1;n<1001;n++){
m=chance(3)-2;
sumb[s1][n]=m;
n1=chance(3)-2;
op1[s1][10][n]=n1;
n2=chance(3)-2;
op2[s1][10][n]=n2;
n3=chance(3)-2;
op3[s1][10][n]=n3;
opm[s1][10][n]=m;;
mx=m-n1-n2+n3;
for (j=1;j<10;j++){
s2=10-j;
p=num[s1][s2+1][n1+1][n2+1][n3+1][mx+5];
p=chance(p);
op1[s1][s2][n]=goto1[s1][s2+1][n1+1][n2+1][n3+1][mx+5][p];
op2[s1][s2][n]=goto2[s1][s2+1][n1+1][n2+1][n3+1][mx+5][p];
op3[s1][s2][n]=goto3[s1][s2+1][n1+1][n2+1][n3+1][mx+5][p];
opm[s1][s2][n]=gotom[s1][s2+1][n1+1][n2+1][n3+1][mx+5][p];
n1=op1[s1][s2][n];
n2=op2[s1][s2][n];
n3=op3[s1][s2][n];
mx=opm[s1][s2][n];
}
}
}
for (s1=1;s1<11;s1++){
for (n=1;n<1001;n++){
v1=0;
for (s2=1;s2<11;s2++){
n1=op1[s1][s2][n];
n2=op2[s1][s2][n];
n3=op3[s1][s2][n];
v1=v1+us[s1][s2][n1+1][n2+1][n3+1];
}
sumu[s1][n]=v1;
}
}
for (n=1;n<1001;n++){
for (m=-5;m<6;m++){
sumv[1][n][m+5]=-999;
}
}
for (n=1;n<1001;n++){
m=sumb[1][n];
sumv[1][n][m+5]=sumu[1][n];
}
for (s1=2;s1<10;s1++){
for (n=1;n<1001;n++){
for (m=-5;m<6;m++){
u1=sumu[s1][n];
mx=m-sumb[s1][n];
z=0;
if (mx>5)z=100;
if (mx<-5)z=100;
if (z>50)mx=0;
maxv=-999;
nxs=1;
for (nx=1;nx<1001;nx++){
v1=u1+sumv[s1-1][nx][mx+5];
if (v1>maxv)nxs=nx;
if (v1>maxv)maxv=v1;
}
if (z>50)maxv=-999;
gotonx[s1][n][m+5]=nxs;
gotomx[s1][n][m+5]=mx;
sumv[s1][n][m+5]=maxv;
}
}
}
maxv=-999;
ns=1;
mxs=0;
nxs=1;
for (n=1;n<1001;n++){
u1=sumu[10][n];
mx=-sumb[10][n];
for (nx=1;nx<1001;nx++){
v1=u1+sumv[9][nx][mx+5];
if (v1>maxv)mxs=mx;
if (v1>maxv)nxs=nx;
if (v1>maxv)ns=n;
if (v1>maxv)maxv=v1;
}
}
System.out.println(maxv);
opnx[10]=ns;
opnx[9]=nxs;
opmx[9]=mxs;
for(j=1;j<9;j++){
s1=9-j;
n=opnx[s1+1];
m=opmx[s1+1];
opnx[s1]=gotonx[s1+1][n][m+5];
opmx[s1]=gotomx[s1+1][n][m+5];
}
for (s1=1;s1<11;s1++){
n=opnx[s1];
for (s2=1;s2<11;s2++){
c[s1][s2]=c[s1][s2]+op3[s1][s2][n]*h;
y1[s1][s2]=y1[s1][s2]+op1[s1][s2][n]*h;
y2[s1][s2]=y2[s1][s2]+op2[s1][s2][n]*h;
}
}
t1=t1+1;
}
}
static int chance(int p){
int ch,j,x1,s;
int x[]=new int[101];
j=1;
for (s=1;s<101;s++){
x[s]=j;
j=j+1;
if (j>p)j=1;
}
ch=(int)(Math.random()*100)+1;
x1=x[ch];
return x1;
}
static double bud(double tk,double tl,double tr,double th1[],double th2[]){
double b1,w1,w2,l1,l2,c1;
int s1,s2,i;
b1=0;
for (s1=1;s1<11;s1++){
for (s2=1;s2<11;s2++){
w1=(1-tk)*th1[s1];
w2=(1-tl)*th2[s2];
l1=1-(w1+w2+tr)/(3*w1);
l2=1-(w1+w2+tr)/(3*w2);
i=0;
if (l1<0)i=i+1;
if (l2>0)i=i+1;
if (i==2)l1=0;
if (i==2)l2=(w2-tr)/(2*w2);
i=0;
if (l1>0)i=i+1;
if (l2<0)i=i+1;
if (i==2)l1=(w1-tr)/(2*w1);
if (i==2)l2=0;
c1=w1*l1+w2*l2+tr;
b1=b1+th1[s1]*l1+th2[s2]*l2-c1;
}
}
return b1;
}
static double wel(double tk,double tl,double tr,double th1[],double th2[]){
double we,w1,w2,l1,l2,c1;
int s1,s2,i;
we=0;
for (s1=1;s1<11;s1++){
for (s2=1;s2<11;s2++){
w1=(1-tk)*th1[s1];
w2=(1-tl)*th2[s2];
l1=1-(w1+w2+tr)/(3*w1);
l2=1-(w1+w2+tr)/(3*w2);
i=0;
if (l1<0)i=i+1;
if (l2>0)i=i+1;
if (i==2)l1=0;
if (i==2)l2=(w2-tr)/(2*w2);
i=0;
if (l1>0)i=i+1;
if (l2<0)i=i+1;
if (i==2)l1=(w1-tr)/(2*w1);
if (i==2)l2=0;
c1=w1*l1+w2*l2+tr;
we=we+Math.log(c1)+Math.log(1-l1)+Math.log(1-l2);
}
}
return we;
}
static double trs(double tk,double tl,double[] th1,double[] th2){
double tr1,tr2,tr3,b1,b2;
int t1;
tr1=0.1;
tr2=0.05;
b1=bud(tk,tl,tr1,th1,th2);
t1=0;
while(t1<100){
b2=bud(tk,tl,tr2,th1,th2);
tr3=tr2-b2*(tr2-tr1)/(b2-b1);
b1=b2;
tr1=tr2;
tr2=tr3;
if (b2*b2<0.001)t1=1000;
t1=t1+1;
}
return tr2;
}
static double ux(double c1,double l1,double l2){
int pp;
double u1,cp1,lp1,lp2;
pp=0;
cp1=c1;
lp1=l1;
lp2=l2;
if (lp1<0)pp=100;
if (lp1>0.99)pp=100;
if (lp2<0)pp=100;
if (lp2>0.99)pp=100;
if (pp>50)lp1=0.5;
if (pp>50)lp2=0.5;
u1=Math.log(cp1)+Math.log(1-lp1)+Math.log(1-lp2);
if (pp>50)u1=-999;
return u1;
}
static double mlx(int s1,int s2,double tk,double tl,double tr,double[] th1,double th2[]){
int i;
double w1,w2,l1,l2;
w1=(1-tk)*th1[s1];
w2=(1-tl)*th2[s2];
l1=1-(w1+w2+tr)/(3*w1);
l2=1-(w1+w2+tr)/(3*w2);
i=0;
if (l1<0)i=i+1;
if (l2>0)i=i+1;
if (i==2)l1=0;
if (i==2)l2=(w2-tr)/(2*w2);
i=0;
if (l1>0)i=i+1;
if (l2<0)i=i+1;
if (i==2)l1=(w1-tr)/(2*w1);
if (i==2)l2=0;
return l1;
}
static double flx(int s1,int s2,double tk,double tl,double tr,double[] th1,double th2[]){
int i;
double w1,w2,l1,l2;
w1=(1-tk)*th1[s1];
w2=(1-tl)*th2[s2];
l1=1-(w1+w2+tr)/(3*w1);
l2=1-(w1+w2+tr)/(3*w2);
i=0;
if (l1<0)i=i+1;
if (l2>0)i=i+1;
if (i==2)l1=0;
if (i==2)l2=(w2-tr)/(2*w2);
i=0;
if (l1>0)i=i+1;
if (l2<0)i=i+1;
if (i==2)l1=(w1-tr)/(2*w1);
if (i==2)l2=0;
return l2;
}
}
最終更新:2010年06月07日 10:16