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
Output
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
相关文章推荐
- jquery 获取 outerHtml 包含当前节点本身的代码
- 大端法与小端法
- Mac机测试应用程序内存溢出的工具-Xcode Instrument
- Jquery实现遮罩层的简单实例(就是弹出DIV周围都灰色不能操作)
- 语音提示
- 一路走来---大二这一年
- labview实现字符串转16进制
- scp命令的用法详解
- linux安装jdk
- 不知道这些,加薪也是白加!
- Android 解析XML 文件的四种方法总结
- EXT4 form 表单内控件输入无效时不出现提示的原因
- 解决react-native 创建工程、init过程太慢的问题
- @RequestBody和@ResponseBody
- DataTable跳转到指定页
- 帝国cms会员查看权限设置方法
- 设计模式--原型模式
- MySQL存储过程中的3种循环
- 如何由jdk的安装版本改成非安装版本
- CSS3盒子模型