您的位置:首页 > 其它

Gym 101028H-The Endless River

2016-07-14 10:05 344 查看
题目描述:

You've possibly heard about 'The Endless River'. However, if not, we are introducing it to you. The Endless River is a river in Cambridge on which David and Roger used to sail. This river has the shape of a circular ring, so if you kept sailing forward you
wouldn't find an end, and that's why it was called 'endless'. The river is exactlyn meters long. Each minute, David movesd meters forward while Roger moves
r meters forward; they start sailing from the same position at the same time, moving in the same direction. At the beginning of each minute, each of them leaves a marker at his current place (they don't put any markers
at the beginning of the race). Determine the number of minutes before two markers (of different people) are placed at the same position.

Input

The input consists of several test cases. The first line of the input contains a single integerT, the number of the test cases. Each of the following lines represents a test case and contains three space-separated integersn,d
andr (1 ≤ n, d, r ≤ 100000) denoting the length 'in meters' of the river, David's speed and Roger's speed (as explained in the statement above) respectively.

Output

For each test case print a single line containing one integer: the number of minutes before two markers (of different people) are placed at the same position.

Sample Input

Input
4
8 2 3
8 3 2
5 1 4
100 1 1


Output
3
3
3
1In the first case,They start at the rst space which is denoted with'start'. Red color denotes David's markers, green for Roger's markers, blue denotes that two markers (ofdifferent people) are placed.At the beginning (0 minutes passed), no markers are placed.After 1 minute passes, David is at the third space and Roger is at the fourth.After 2 minutes pass, David is at the fth space and Roger is at the seventh.After 3 minutes pass, David is at the seventh space and Roger is at the minute, so when David places hismarker, two markers are placed at the seventh space and the answer is 3 minutes.



代码实现:
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
int main()
{
__int64 t,n,d,r,x,y,i,j,f,a[100010],b[100010],minn,s;
cin>>t;
while(t--)
{
cin>>n>>d>>r;
x=max(d,r);
y=min(d,r);
//cout<<y<<endl;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
//cout<<x<<" "<<y<<endl;
//sum=0;
for(i=1;;i++)
{
if(a[(i*x)%n]!=0)
{
break;
}
else
{
a[(i*x)%n]=i;
}
}
//        for(i=1;i<=n;i++)
//        {
//            cout<<a[i]<<" ";
//        }
//        cout<<endl;
for(i=1;;i++)
{
if(b[(i*y)%n]!=0)
{
break;
}
else
{
b[(i*y)%n]=i;
}
}
//        for(i=1;i<=n;i++)
//        {
//            cout<<b[i]<<" ";
//        }
//        cout<<endl;
for(i=1;i<=n;i++)
{
if(a[i]!=0&&b[i]!=0)
{
minn=max(a[i],b[i]);
break;
}
}
for(i=1;i<=n;i++)
{
if(a[i]!=0&&b[i]!=0)
{
s=max(a[i],b[i]);
minn=min(s,minn);
}
}
cout<<minn<<endl;
}
return 0;
}
1.看到样例解释,很容易发现数组应该怎么标记,也能发现样例中是慢得追快的
2.此题的坑点在于两个标记数组可能会超int范围,用__int64数据类型,因为a,b存的是时间,虽然路程的范围是100000
3.以上代码的方法是最保险的,如果用慢得追快的,不标记慢的的时间数组,是错误的,WA
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: