HDU - 5858 Hard Problem (simpson積分)
阿新 • • 發佈:2018-08-04
asr \n int show turn push first 陰影部分 bubuko
原題鏈接
題意:
給定一個邊長為n的正方形,求陰影部分面積
思路:
現將圖形順時針旋轉 45° 然後建立坐標系,寫出陰影部分方程,用Simpson積分算一下就行了,註意精度,1e-10 WA 了 ,1e-20 A了
1 #include<bits/stdc++.h> 2 3 using namespace std; 4 5 #define X first 6 #define Y second 7 #define eps 1e-20 8 #define gcd __gcd 9 #define pb push_back 10#define PI acos(-1.0) 11 #define lowbit(x) (x)&(-x) 12 #define bug printf("!!!!!\n"); 13 #define mem(x,y) memset(x,y,sizeof(x)) 14 15 typedef long long LL; 16 typedef long double LD; 17 typedef pair<int,int> pii; 18 typedef unsigned long long uLL; 19 20 const int maxn = 1e5+2;21 const int INF = 1<<30; 22 const int mod = 1e9+7; 23 24 double f(double x){ 25 return sqrt(1-x*x) - sqrt(4 - x*x) + sqrt(2); 26 } 27 double simpson(double L ,double R){ 28 return (R-L)*(f(L)+4.0*f((L+R)/2.0)+f(R))/6.0; 29 } 30 double asr(double L,double R){ 31 double mid = (L+R)/2.0; 32 double res = simpson(L,R); 33 double left = simpson(L,mid),right = simpson(mid,R); 34 if(fabs(left+right-res)<eps)return left + right; 35 else return asr(L,mid)+asr(mid,R); 36 } 37 double C; 38 void solve(){ 39 int n; 40 scanf("%d",&n); 41 printf("%.2f\n",n*n*1.0*C); 42 return; 43 } 44 45 int main() 46 { 47 // freopen("in.txt","r",stdin); 48 // freopen("out.txt","w",stdout); 49 // ios::sync_with_stdio(false); 50 int t = 1; 51 scanf("%d",&t); 52 C = asr(0,sqrt(7.0/8.0)); // 離線計算單位系數 53 while(t--){ 54 // printf("Case %d: ",cas++); 55 solve(); 56 } 57 return 0; 58 }
HDU - 5858 Hard Problem (simpson積分)