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

146. The Runner

time limit per test: 0.25 sec.
memory limit per test: 4096 KB
input: standard input
output: standard output




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.

Input
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).

Output
Write the only one real number with 4 digits after decimal points: the distance from start to finish.

Sample test(s)

Input
 
 
2 1 
1 3
 
 

Output
 
 
1.0000

感想:使用了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
",(double)ans/10000.0);
    return 0;
}

  

原文地址:https://www.cnblogs.com/xuesu/p/4069358.html