Created
October 20, 2014 13:12
-
-
Save kuuso/333ff9f52b722ee04c23 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
class TEST{ | |
static void Main(){ | |
Sol mySol =new Sol(); | |
mySol.Solve(); | |
} | |
} | |
class Sol{ | |
public void Solve(){ | |
target=17128.111274826415512; | |
Func<int,double> f=null; | |
String rule=""; | |
for(int i=0;i<4;i++){ | |
switch(i){ | |
case 0: | |
f=Trapezoid;rule="Trapezoid";break; | |
case 1: | |
f=Simpson;rule="Simpson";break; | |
case 2: | |
f=Simpson38;rule="Simpson38";break; | |
case 3: | |
f=Boole;rule="Boole";break; | |
} | |
int l=1; | |
int r=100000000;//素直に全区間二分探索する場合。各方式で15秒ずつくらいかかります。 | |
//int r=1000000;//ideoneで動かすときはこちらで | |
int c=l; | |
while(r-l>1){ | |
c=(l+r)/2; | |
double d=f(c);//Console.WriteLine("{0} {1}",c,d); | |
if(Math.Abs(target-d)<0.1)r=c; | |
else l=c; | |
} | |
while(Math.Abs(target-f(c))<0.1)c--; | |
while(Math.Abs(target-f(c))>0.1)c++; | |
Console.WriteLine("Case {0}:{1}",rule,c); | |
} | |
} | |
double Trapezoid(int n){ | |
double ret=0; | |
double d=(6.0-1.0)/(double)n; | |
for(int i=0;i<n;i++){ | |
double a=1+i*d; | |
double b=1+(i+1)*d; | |
ret+=d/2.0*(Math.Pow(a,a)+Math.Pow(b,b)); | |
} | |
return ret; | |
} | |
double Simpson(int n){ | |
double ret=0; | |
double d=(6.0-1.0)/(double)n; | |
for(int i=0;i<n;i++){ | |
double a=1+i*d; | |
double b=1+(i+1)*d; | |
ret+=d/6.0*(Math.Pow(a,a)+4*Math.Pow(a+(b-a)/2.0,a+(b-a)/2.0)+Math.Pow(b,b)); | |
} | |
return ret; | |
} | |
double Simpson38(int n){ | |
double ret=0; | |
double d=(6.0-1.0)/(double)n; | |
for(int i=0;i<n;i++){ | |
double a=1+i*d; | |
double b=1+(i+1)*d; | |
ret+=d/8.0*(Math.Pow(a,a)+3*Math.Pow(a+(b-a)/3.0,a+(b-a)/3.0)+3*Math.Pow(a+2*(b-a)/3.0,a+2*(b-a)/3.0)+Math.Pow(b,b)); | |
} | |
return ret; | |
} | |
double Boole(int n){ | |
double ret=0; | |
double d=(6.0-1.0)/(double)n; | |
for(int i=0;i<n;i++){ | |
double a=1+i*d; | |
double b=1+(i+1)*d; | |
ret+=d/90.0*(7*Math.Pow(a,a)+32*Math.Pow(a+(b-a)/4.0,a+(b-a)/4.0)+12*Math.Pow(a+2*(b-a)/4.0,a+2*(b-a)/4.0)+32*Math.Pow(a+3*(b-a)/4.0,a+3*(b-a)/4.0)+7*Math.Pow(b,b)); | |
} | |
return ret; | |
} | |
double target; | |
public Sol(){ | |
} | |
} | |
//Case Trapezoid:1648 | |
//Case Simpson:40 | |
//Case Simpson38:32 | |
//Case Boole:10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment