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∑rf(x)′∗10−15
而此题主要就是因为根号里面的值太小,可能导致计算后为0,所以我们可以先算出∑i=1rf(x)′\sum_{i=1}^rf(x)\prime∑i=1rf(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;
}