Codeforces Round #704 (Div. 2)-A. Three swimmers-题解

本文解析了Codeforces竞赛中的问题A,涉及三个游泳者按不同时间返回泳池,你需要在特定时间到达并计算等待最少时间以看到某人抵达。解题关键在于找到每个人在特定时间点的位置,并确定最小等待时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Codeforces Round #704 (Div. 2)-A. Three swimmers

传送门
Time Limit: 1 second
Memory Limit: 512 megabytes

Problem Description

Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.

It takes the first swimmer exactly a a a minutes to swim across the entire pool and come back, exactly b b b minutes for the second swimmer and c c c minutes for the third. Hence, the first swimmer will be on the left side of the pool after 0 0 0, a a a, 2 a 2a 2a, 3 a 3a 3a, … minutes after the start time, the second one will be at 0 0 0, b b b, 2 b 2b 2b, 3 b 3b 3b, … minutes, and the third one will be on the left side of the pool after 0 0 0, c c c, 2 c 2c 2c, 3 c 3c 3c, … minutes.

You came to the left side of the pool exactly p p p minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.

Input

The first line of the input contains a single integer t t t ( 1 ≤ t ≤ 1000 1 \leq t \leq 1000 1t1000) — the number of test cases. Next t t t lines contains test case descriptions, one per line.

Each line contains four integers p p p, a a a, b b b and c c c ( 1 ≤ p , a , b , c ≤ 1 0 18 1 \leq p, a, b, c \leq 10^{18} 1p,a,b,c1018), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.

Output

For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.

Sample Input

4
9 5 4 8
2 6 10 9
10 2 5 10
10 9 9 9

Sample Onput

1
4
0
8

Note

In the first test case, the first swimmer is on the left side in 0 , 5 , 10 , 15 , … 0, 5, 10, 15, \ldots 0,5,10,15, minutes after the start time, the second swimmer is on the left side in 0 , 4 , 8 , 12 , … 0, 4, 8, 12, \ldots 0,4,8,12, minutes after the start time, and the third swimmer is on the left side in 0 , 8 , 16 , 24 , … 0, 8, 16, 24, \ldots 0,8,16,24, minutes after the start time. You arrived at the pool in 9 9 9 minutes after the start time and in a minute you will meet the first swimmer on the left side.

In the second test case, the first swimmer is on the left side in 0 , 6 , 12 , 18 , … 0, 6, 12, 18, \ldots 0,6,12,18, minutes after the start time, the second swimmer is on the left side in 0 , 10 , 20 , 30 , … 0, 10, 20, 30, \ldots 0,10,20,30, minutes after the start time, and the third swimmer is on the left side in 0 , 9 , 18 , 27 , … 0, 9, 18, 27, \ldots 0,9,18,27, minutes after the start time. You arrived at the pool 2 2 2 minutes after the start time and after 4 4 4 minutes meet the first swimmer on the left side.

In the third test case, you came to the pool 10 10 10 minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!

In the fourth test case, all swimmers are located on the left side in 0 , 9 , 18 , 27 , … 0, 9, 18, 27, \ldots 0,9,18,27, minutes after the start time. You arrived at the pool 10 10 10 minutes after the start time and after 8 8 8 minutes meet all three swimmers on the left side.


题目大意

有3个人往返游泳,每人一趟用时分别为a,b,c
他们在t=0时开始从泳池左侧开始来回游泳,你在t=p时去泳池左侧看他们。
问最少需要等多长时间能看到某个人正好在泳池左侧


解题思路

第一个人在t=0,a,2a,3a…时刻正好在泳池左侧
过来时如果他正好在泳池左侧,等待时间为0
否则t=p时过来,他游走了p%t分钟,需等待t-p%t分钟
共3个人,更新最小耗时即可


AC代码

#include<bits/stdc++.h>
using namespace std;
#define mem(a) memset(a,0,sizeof(a))
#define dbg(x) cout<<#x<<" = "<<x<<endl
#define fi(i,l,r) for(int i=l;i<r;i++) 
#define cd(a) scanf("%d",&a)
typedef long long ll;

int main()
{
	int N;
    cin>>N; //N次询问
    while(N--)
    {
        ll p,a,m=2e18;
        cin>>p;
        for(ll i=0;i<3;i++)
        {
            cin>>a;
            ll need = p%a;
            if(need==0)m=0;
            else m=min(m,a-need);
        }
        printf("%lld\n",m);
    }
	return 0;
}
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tisfy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值