Floating-Point Hazard
時間限制: 1 Sec 內存限制: 128 MB
提交: 106 解決: 42
[提交] [狀態] [命題人:admin]
題目描述
Given the value of low, high you will have to find the value of the following expression:
If you try to find the value of the above expression in a straightforward way, the answer may be incorrect due to precision error.
輸入
The input file contains at most 2000 lines of inputs. Each line contains two integers which denote the value of low, high (1 ≤ low ≤ high ≤ 2000000000 and high-low ≤ 10000). Input is terminated by a line containing two zeroes. This line should not be processed.
輸出
For each line of input produce one line of output. This line should contain the value of the expression above in exponential format. The mantissa part should have one digit before the decimal point and be rounded to five digits after the decimal point. To be more specific the output should be of the form d.dddddE-ddd, here d means a decimal digit and E means power of 10. Look at the output for sample input for details. Your output should follow the same pattern as shown below.
樣例輸入
1 100
10000 20000
0 0
樣例輸出
3.83346E-015
5.60041E-015
題目大意:
輸入兩個整數lll和rrr,求∑i=lr((i+10?15)3?i3)\sum_{i=l}^r(\sqrt[3]{(i+10^{-15})}-\sqrt[3]{i})i=l∑r?(3(i+10?15)??3i?)的值,并用d.dddddE-ddd的格式輸出。
解題思路:
首先,我們可以先回顧一下求導的公式:f(x)′=f(x+Δx)?f(x)Δxf(x)\prime=\frac{f(x+\Delta x)-f(x)}{\Delta x}f(x)′=Δxf(x+Δx)?f(x)?,因此當f(x)=x3,Δx=10?15f(x)=\sqrt[3]{x},\Delta x=10^{-15}f(x)=3x?,Δx=10?15時,f(x)′=x+10?153?x310?15f(x)\prime=\frac{\sqrt[3]{x+10^{-15}}-\sqrt[3]x}{10^{-15}}f(x)′=10?153x+10?15??3x??,因此∑i=lr((i+10?15)3?i3)=∑i=1rf(x)′?10?15\sum_{i=l}^r(\sqrt[3]{(i+10^{-15})}-\sqrt[3]{i})=\sum_{i=1}^rf(x)\prime*10^{-15}i=l∑r?(3(i+10?15)??3i?)=i=1∑r?f(x)′?10?15
而此題主要就是因為根號里面的值太小,可能導致計算后為0,所以我們可以先算出∑i=1rf(x)′\sum_{i=1}^rf(x)\prime∑i=1r?f(x)′的值,10?1510^{-15}10?15可以直接先放在指數上,最后化簡為所需要的形式即可。
代碼:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
int main()
{#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);#endif//freopen("out.txt", "w", stdout);ios::sync_with_stdio(0),cin.tie(0);int l,r;while(scanf("%d %d",&l,&r)&&l+r) {double t=0;for(int i=l;i<=r;i++) {t+=(1.0/3.0)*(pow(double(i),-2.0/3.0));}double x=0;int y=-15;while(t>10.0) {t=t/10.0;y++;}x=t;while(t<1.0) {t=t*10.0;y--;}x=t;printf("%.5fE-%03d\n",x,(-y));}return 0;
}