CodeForces The Endless River

The Endless River

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

standard input/output
Statements

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 exactly n meters long. Each minute, David moves d 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 integer T, the number of the test cases. Each of the following lines represents a test case and contains three space-separated integers nd and r (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

48 2 38 3 25 1 4100 1 1

Output

3331

Hint

In the first case, we have n = 8, d = 2 and r = 3. They start at the first space which is denoted with ‘start‘. Red color denotes David‘s markers, green for Roger‘s markers, blue denotes that two markers (of different 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 fifth 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 his marker, two markers are placed at the seventh space and the answer is 3 minutes.

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

typedef long long LL;

int a[100005];
int b[100005];

int main()
{
    //freopen("input.txt","r",stdin);
    int t;
    cin>>t;
    while(t--)
    {
        int n,d,r;
        cin>>n>>d>>r;
        int f=1;
        int s=1;
        int m=0;
        a[1]=1;
        int flag=0;
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        while(1)
        {
            m++;
            f=m*d%n;
            if(b[f]==1)  {break;}
            a[f]=1;

            s=m*r%n;
            if(a[s]==1)   {break;}
            b[s]=1;
        }
        cout<<m<<endl;
    }
}

  

时间: 2024-11-07 06:07:04

CodeForces The Endless River的相关文章

H - The Endless River

1 /* 2 从顶部发射石头,按照碰壁反弹的运动路线问能不能击中底部的某个位置. 3 计算落点,飞行轨迹周期为2*(w-1),所以直接将高度视为h=h%(2*w-2),然后如果高度 4 不大于w 则落点即为高度,否则落点在2*w-h的位置. 5 */ 6 #include <bits/stdc++.h> 7 using namespace std; 8 int main() 9 { 10 int n; 11 scanf("%d",&n); 12 while(n--)

3月14日霍金逝世,我用6个时间“剪史”祭奠大师

标题没有错别字,霍金最让人熟知的是他的科普宇宙学著作<时间简史>. 今天,3月14日,著名物理学家史蒂芬·霍金(Stephen William Hawking)今晨在位于剑桥的家中去世,享年76岁.我,张书乐,一个科技自媒体,想用6个时间"剪史"祭奠大师. 时间"剪史"一:3月14日,偶遇 2018年3月14日,霍金去世. 1879年3月14日,阿尔伯特·爱因斯坦(Albert.Einstein),出生于德国符腾堡王国乌尔姆市,而他逝世于1955年4月1

CodeForces 1107F. Vasya and Endless Credits

题目简述:给定 $n \leq 500$ 个贷款方式,其中第$i$个贷款额为$a_i$元,需要$k_i$个月偿还,每月还贷$b_i$元.在每个月月初可申请其中一个贷款,而在每个月月底时需要还贷.求:(在某一时刻)可获得的最多贷款. 观察1:获得最多贷款的时刻一定在$n$月以内. 观察2:倒数第$j$个月申请的第$i$个贷款,在获得最多贷款的时刻之前,需要还贷$b_i*\min\{k_i,j-1\}$元. 解1:(二分图最佳匹配,最小费用最大流)code 设$X$部有$n$个点,记为$X_i$,表

Codeforces 538C. Tourist&#39;s Notes

A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer hi. The tourist pick smooth enough route for his hike, meaning that t

Codeforces Round #200 (Div. 2) (ABCDE题解)

比赛链接:http://codeforces.com/contest/344 A. Magnets time limit per test:1 second memory limit per test:256 megabytes Mad scientist Mike entertains himself by arranging rows of dominoes. He doesn't need dominoes, though: he uses rectangular magnets inst

Codeforces Round #300——C贪心——Tourist&#39;s Notes

Description A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer hi. The tourist pick smooth enough route for his hike, me

Codeforces Round #300-Tourist&#39;s Notes(贪心)

Tourist's Notes Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Description A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level

Codeforces Round #277.5 (Div. 2) JAVA版题解

Codeforces Round #277.5 (Div. 2) A. SwapSort time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output In this problem your goal is to sort an array consisting of n integers in at most n swaps. For t

Codeforces gym 100685 F. Flood bfs

F. FloodTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/F Description We all know that King Triton doesn't like us and therefore shipwrecks, hurricanes and tsunami do happen. But being bored with the same routine