jiejiejiang2004

题解:2024牛客多校第三场 B

B Crash Test header

时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 1048576K,其他语言2097152K
64bit IO Format: %lld

题目描述

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 \(D\) meters from the wall. This crash test provides \(n\) types of boosters, where the \(i\)-th type of booster has a thrust performance of \(h_i\) , and there are ample quantities of each type of booster. Suppose the current distance between the car's front and the wall is \(d\), and we use a booster with a thrust performance of \(h\). When \(d≥h\), the car will move forward \(h\) meters and then stop. Otherwise, the car will move forward \(d\) meters, crash into the wall, and rebound \(h-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?

时隔五年,赛车界最受瞩目的赛事一级方程式赛车重返中国。中国大奖赛最近在上海国际赛车场举行。一级方程式赛车的时速可达 350 公里。为了确保车手的安全,赛车必须通过严格的碰撞测试。

我们考虑以下简化版的碰撞测试。起初,汽车的前部朝向墙壁,与墙壁的距离为 \(D\) 米。该碰撞测试提供了 \(n\) 种助推器,其中第 i$ 种助推器的推力性能为 \(h_i\),每种助推器的数量都很充足。假设当前车头与墙壁之间的距离为 \(d\),我们使用的助推器的推力性能为 \(h\)。当 \(d≥h\) 时,汽车将向前行驶 \(h\) 米,然后停止。否则,汽车将向前行驶 \(d\) 米,撞到墙上,反弹 \(h-d\) 米,然后停下来,仍然面向墙壁。

现在,你想知道,通过任意多次运算(包括不运算),汽车车头与墙壁之间的最小距离是多少?

输入描述:

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

The second line of inputcontains n positive integers \(h_1 ,h _2 ,\dots ,h_n(1≤h_i≤10^{18} )\), denoting the thrust performance of each booster.

第一行输入包含两个正整数 \(n\)\(D (1≤n≤100,1≤D≤10^{18} )\),分别表示助推器的数量和一级方程式赛车与墙壁之间的距离。

第二行输入包含 n 个正整数 \(h_1 ,h _2 ,\dots ,h_n(1≤h_i≤10^{18} )\) ,表示每个助推器的推力性能。

输出描述:

Output an integer in a line, denoting the minimum possible distance between the car's front and the wall.

在一行中输出一个整数,表示车头与墙壁之间可能的最小距离。

示例1

输入

1 10
3

输出

1

说明

An optimal strategy is \(10 \xrightarrow[]{i=1} 7 \xrightarrow[]{i=1} 4 \xrightarrow[]{i=1} 1\)

最佳策略是 \(10 \xrightarrow[]{i=1} 7 \xrightarrow[]{i=1} 4 \xrightarrow[]{i=1} 1\)

示例2

输入

2 10
3 4

输出

0

说明

An optimal strategy is \(10 \xrightarrow[]{i=1} 7 \xrightarrow[]{i=2} 3 \xrightarrow[]{i=1} 0\)

最佳策略是 \(10 \xrightarrow[]{i=1} 7 \xrightarrow[]{i=2} 3 \xrightarrow[]{i=1} 0\)

示例3

输入

2 1
3 7

输出

0

说明

An optimal strategy is \(2 \xrightarrow[]{i=2} 6 \xrightarrow[]{i=1} 3 \xrightarrow[]{i=1} 0\)

最佳策略是 \(2 \xrightarrow[]{i=2} 6 \xrightarrow[]{i=1} 3 \xrightarrow[]{i=1} 0\)

示例4

输入

10 4306315981482911
4306 3519 8148 2911 9970 7222 5462 1844 7072 1702

输出

0

示例5

输入

10 123456789987654321
10 10 10 10 10 10 10 10 10 10

输出

1

题解

by W.Sherlock.Henry
这题可以运用到数论中的裴蜀定理
不懂的可以看这里
简而言之,就是:

a的任意倍数与b的任意倍数的和 都是 a和b的最大公约数的倍数

可以很明显地看到,假设我们只选一种操作,那么每种操作得到的最靠近的墙边的距离是

std::min(D % hi , hi - D % hi);

结合全部来看
那么总的答案就应该是

std::min(D % h_gcd , h_gcd - D % h_gcd);

我的代码

#include <iostream>
#include <algorithm>
#define int long long

int n,d;
const int N = 1e7+10;
int a[N];

signed main() {
    std::cin >> n >> d;
    for(int i = 0 ; i < n ; i ++) {
         std::cin >> a[i];
    }

    int m = a[0];
    for(int i =- 0 ; i < n ; i ++) {
        m = std::__gcd(m,a[i]);
    }

    std::cout << std::min(d%m,m - d%m);
    return 0;
}

posted on 2024-07-24 15:47  Jiejiejiang  阅读(16)  评论(0编辑  收藏  举报

导航