牛客 Crash Test

链接:登录—专业IT笔试面试备考平台_牛客网
来源:牛客网
 

题目描述

After five years, the most high-profile event in motor racing, Formula 1, returns to China. The Chinese Grand Prix was recently held at the Shanghai International Circuit. Formula 1 cars can reach speeds of up to 350 km/h. To ensure the safety of the drivers, the cars must pass rigorous crash tests.

We consider the following simplified version of a crash test. Initially, a car is positioned with its front facing a wall, at a distance of DDD meters from the wall. This crash test provides nnn types of boosters, where the iii-th type of booster has a thrust performance of hih_ihi​, and there are ample quantities of each type of booster. Suppose the current distance between the car's front and the wall is ddd, and we use a booster with a thrust performance of hhh. When d≥hd \ge hd≥h, the car will move forward hhh meters and then stop. Otherwise, the car will move forward ddd meters, crash into the wall, and rebound h−dh-dh−d meters, after which it stops, still facing the wall.

Now, you want to know, through any number of operations (including no operation), what the minimum distance between the car's front and the wall can be?

输入描述:

The first line of input contains two positive integers nnn and DDD (1≤n≤100,1≤D≤10181 \le n \le 100, 1 \le D \le 10^{18}1≤n≤100,1≤D≤1018), denoting the number of boosters and the distance between the Formula 1 car and the wall, respectively.

The second line of inputcontains nnn positive integers h1,h2,…,hnh_1, h_2, \ldots, h_nh1​,h2​,…,hn​ (1≤hi≤10181 \le h_i \le 10^{18}1≤hi​≤1018), denoting the thrust performance of each booster.

输出描述:

Output an integer in a line, denoting the minimum possible distance between the car's front and the wall.
#include<bits/stdc++.h>
using namespace std;

#define int long long
#define endl '\n'
#define ios ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);

const int N=2e5+5;
const int C=2e3+5;
const int inf=0x3f3f3f3f3f3f3f;

signed main() {
	ios
	int n,d; cin >> n >> d;
	int p;
    for(int i=0;i<n;i++){
    	int x; cin >> x;
		p=__gcd(p,x);
		//求所有人的最大公约数  
	}
//	cout << p << endl; 
	//那么也就是说我能开的最大的距离,一定是p的倍数 
	//因为有两种可能,一种是没撞上,一种是撞上再返回 
    cout << min(d%p,p-d%p) << endl;
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值