您的位置:首页 > 其它

sgu 146. The Runner 取模技巧 难度:1

2014-11-02 15:38 447 查看

146. The Runner

[align=center]time limit per test: 0.25 sec.
memory limit per test: 4096 KB[/align]
[align=center]input: standard input
output: standard output[/align]

[align=left]The runner moves along the ring road with length L. His way consists of N intervals. First he ran T1 minutes with speed V1, then T2 minutes with speed V2 and so on till the N-th interval, where he ran TN minutes with speed VN. Your task is to find the distance from start to finish along the ring road. The distance along the ring road is the length of the shortest way all points of which belongs to the ring road.[/align]
[align=left]
Input[/align]
[align=left]Real number L (1<=L<=1000, with 4 signs after decimal point) and natural number N (N<=20000) are written in the first line. Each of the following N lines contains two integer numbers Ti and Vi (1<=Ti<=10^7, 1<=Vi<=10^6).[/align]
[align=left]
Output[/align]
[align=left]Write the only one real number with 4 digits after decimal points: the distance from start to finish.[/align]
[align=left]
Sample test(s)[/align]
[align=left]
Input[/align]
[align=left] [/align]
[align=left] [/align]
[align=left]2 1
1 3[/align]
[align=left] [/align]
[align=left] [/align]
[align=left]
Output[/align]
[align=left] [/align]
[align=left] [/align]
[align=left]1.0000[/align]
感想:使用了double/double的取模..结果过不了,可能还是尾数长度不够的原因,所以还是longlong整除法,似乎longlong*1en再整除更精确

思路:因为题目是四位数精度,所以sigma(vi*ti)乘上1e4后取余就可以得到当前位置,注意所说的是起点与终点的距离,取劣弧

#include <cstdio>
#include <cstring>
using namespace std;
const double eps=1e-8;

int n;
long long t[25001],v[25001];
int main(){
double tl;
long long l;
scanf("%lf%d",&tl,&n);
l=tl*10000+0.5;
for(int i=0;i<n;i++){
scanf("%I64d%I64d",t+i,v+i);
}
long long ans=0;
for(int i=0;i<n;i++){
ans+=t[i]*v[i]*10000;
ans%=l;
}
if(l-ans<ans)ans=l-ans;
printf("%.4f\n",(double)ans/10000.0);
return 0;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: