题解: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
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 公里。为了确保车手的安全,赛车必须通过严格的碰撞测试。
我们考虑以下简化版的碰撞测试。起初,汽车的前部朝向墙壁,与墙壁的距离为
现在,你想知道,通过任意多次运算(包括不运算),汽车车头与墙壁之间的最小距离是多少?
输入描述:
The first line of input contains two positive integers
The second line of inputcontains n positive integers
第一行输入包含两个正整数
第二行输入包含 n 个正整数
输出描述:
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
最佳策略是
示例2
输入
2 10 3 4
输出
0
说明
An optimal strategy is
最佳策略是
示例3
输入
2 1 3 7
输出
0
说明
An optimal strategy is
最佳策略是
示例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 阅读(33) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」