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 hi , 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 dh, the car will move forward h meters and then stop. Otherwise, the car will move forward d meters, crash into the wall, and rebound hd 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$ 种助推器的推力性能为 hi,每种助推器的数量都很充足。假设当前车头与墙壁之间的距离为 d,我们使用的助推器的推力性能为 h。当 dh 时,汽车将向前行驶 h 米,然后停止。否则,汽车将向前行驶 d 米,撞到墙上,反弹 hd 米,然后停下来,仍然面向墙壁。

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

输入描述:

The first line of input contains two positive integers n and D(1n100,1D1018), 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 h1,h2,,hn(1hi1018), denoting the thrust performance of each booster.

第一行输入包含两个正整数 nD(1n100,1D1018),分别表示助推器的数量和一级方程式赛车与墙壁之间的距离。

第二行输入包含 n 个正整数 h1,h2,,hn(1hi1018) ,表示每个助推器的推力性能。

输出描述:

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 10i=17i=14i=11

最佳策略是 10i=17i=14i=11

示例2

输入

2 10
3 4

输出

0

说明

An optimal strategy is 10i=17i=23i=10

最佳策略是 10i=17i=23i=10

示例3

输入

2 1
3 7

输出

0

说明

An optimal strategy is 2i=26i=13i=10

最佳策略是 2i=26i=13i=10

示例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   Jiejiejiang  阅读(33)  评论(0编辑  收藏  举报

编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」

导航

统计信息

点击右上角即可分享
微信分享提示