codeforces A. Three swimmers

A. Three swimmers

time limit per test1 second
memory limit per test512 megabytes
inputstandard input
outputstandard output
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 minutes to swim across the entire pool and come back, exactly b minutes for the second swimmer and c minutes for the third. Hence, the first swimmer will be on the left side of the pool after 0, a, 2a, 3a, … minutes after the start time, the second one will be at 0, b, 2b, 3b, … minutes, and the third one will be on the left side of the pool after 0, c, 2c, 3c, … minutes.

You came to the left side of the pool exactly 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 (1≤t≤1000) — the number of test cases. Next t lines contains test case descriptions, one per line.

Each line contains four integers p, a, b and c (1≤p,a,b,c≤1018), 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.

题目大意

有三个游泳的人分别以a,b,c的往返时间在游泳,问当你于p分钟到达泳池时,还需要多久可以看到有人返回起点与你相遇

输入案例

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

输出案例

1
4
0
8

解题思路

你到达的时间和每个游泳者往返一周的时间取模,如果为0说明能够刚好遇上,否则得到的数就是游泳者本次往返已经游泳的时间,用往返一周的时间减去已经游泳的时间就可以得到还需多久才能返回起点,遍历三个人即可得到最小值

AC代码

#include <iostream>
using namespace std;
int main() {
    
    
	int  n;
	cin>>n;
	while(n--) {
    
    
		long long x,m=2e18;
		cin>>x;
		int cx=3;
		while(cx--) {
    
    
			long long t;//不开longlong见祖宗 
			cin>>t;
			long long p=x%t;
			if(p==0) m=0;//判定一下如果x%t=0说明刚好"你"到游泳池就能遇上他们 
			else m=min(m,t-p);
		}
		cout<<m<<endl;
	}
}

猜你喜欢

转载自blog.csdn.net/qq_34832548/article/details/114012672