USACO竞赛编程语言选择指南!USACO竞赛支持的五种语言有什么特点?适用场景是什么?

USACO是面向美国中学生的一项计算机编程竞赛,旨在培养学生的算法设计和编程能力。USACO支持多种编程语言,包括C++、Java、Python、Pascal和C。其中,C++、Java和Python是最常用的三种语言。每种语言都有其特点和适用场景,选择合适的编程语言对于参赛选手至关重要。

一、C++

1.特点

高性能与运行效率:C++以其高效的编译和执行速度著称,适合处理复杂和计算密集型的任务。

底层可控性:C++允许程序员直接控制内存管理和其他底层操作,这在某些情况下可以显著提升性能。

成熟度与兼容性:C++是一种非常成熟的语言,拥有丰富的标准库和第三方库,广泛应用于工业界和学术界。

面向对象编程:C++引入了面向对象的理念,可以便捷的使用数据结构和算法库,使得代码编写更加方便。

2.适用场景

目标高分或高级别比赛:如果目标是通过铂金级别甚至更高水平的比赛,C++是一个非常好的选择。

同时参加其他竞赛:C++也是NOIP(全国青少年信息学奥林匹克联赛)等其他竞赛的常用语言,因此选择C++可以帮助你在多个竞赛中受益。

二、Java

1.特点

简单易用:Java语法相对简洁,学习曲线较为平缓,适合初学者入门。

面向对象:Java是一种纯面向对象的语言,有助于培养良好的编程习惯和思维方式。

跨平台性:Java具有“一次编写,到处运行”的特性,代码可以在不同的操作系统上运行,无需修改。

安全性:Java内置了许多安全机制,如垃圾回收和异常处理,减少了内存泄漏和程序崩溃的风险。

2.适用场景

AP计算机课程学习:Java是AP计算机科学A课程的主要编程语言,因此选择Java对AP课程的学习有一定帮助。

中级水平比赛:如果目标是通过银组或金组考试,Java是一个不错的选择,尽管它的运行速度较慢,但在这些级别下通常不会成为主要瓶颈。

三、Python

1.特点

便捷性:Python以其简洁的语法和强大的库支持著称,非常适合快速开发和原型设计。

易学易用:Python的学习曲线较低,适合编程新手入门。

广泛应用:Python在人工智能、数据科学等领域有广泛应用,许多知名库(如TensorFlow、PyTorch)都支持Python。

2.适用场景

低级别比赛:如果目标是通过银组考试,Python是一个足够好的选择,因为在这个级别下,运行效率并不是主要问题。

后续发展:如果你对人工智能领域感兴趣,学习Python可以帮助你继续参加更高级别的AI竞赛。

四、如何选择适合自己的编程语言?

根据目标选择

高目标(铂金及以上):如果你的目标是通过铂金级别比赛或更高水平的比赛,推荐选择C++,因为它在性能和效率方面具有明显优势。

中级目标(银组或金组):如果你的目标是通过银组或金组考试,Java是一个不错的选择,特别是在你还需要准备AP计算机科学课程的情况下。

低目标或兴趣驱动:如果你的目标仅仅是通过银组考试,或者你对编程的兴趣大于竞争需求,Python是一个理想的入门语言。

根据个人背景选择

已有编程基础:如果你已经有了一定的编程基础,特别是熟悉C++或其他类似语言,可以选择继续使用C++以发挥你的优势。

零基础或初学者:如果你是编程新手,Python是一个很好的起点,它可以帮助你快速入门并建立信心。

多任务需求:如果你需要兼顾竞赛和课程学习(如AP计算机科学),Java可能是一个折中的选择。

【扫码免费领取】USACO真题+备赛书单+一对一备考规划!

预约最新真题讲座、课程详情可添加下方顾问老师咨询

思维导图

全球中学生都适配的编程竞赛!USACO赛制解析与中美竞赛难度对照!

作为国际信息学奥林匹克竞赛(IOI)的重要选拔通道,美国计算机奥林匹克竞赛(USACO)已成为全球计算机专业学子展现编程实力的重要舞台。这项创立于1992年的赛事凭借其权威的考核体系与科学的晋级机制,每年吸引超过5万名来自100多个国家的选手参与。

一、USACO核心赛制解析

参赛资格与形式

USACO面向全球所有中学生开放,无国籍和年龄限制。参赛者只需在官网注册真实信息(含出生日期、毕业年份等),即可免费参与全年四场在线赛事。每场竞赛窗口持续3天,选手可自主选择参赛时段,每次需连续完成3道编程题(总分1000分)。

二、阶梯式晋级体系

竞赛设置铜、银、金、铂金四个级别,所有新选手须从铜级起步。晋级规则包含两种方式:

满分直通机制

选手在3小时内完成所有题目且测试点全部通过(1000分),可立即解锁更高级别赛事。理论上,顶尖选手可在单赛季四场比赛中完成从铜级到铂金级的跨越。

动态分数线晋级

未获满分的选手需等待官方公布的晋级线(通常700-800分),该分数线根据当次参赛者整体水平动态调整。近三年数据显示,铜升银平均分数线为725分,银升金约765分,金升铂金需达到790分以上。

三、中美竞赛难度对照

通过与国内主流赛事的横向对比,可更清晰定位各阶段难度:

USACO等级 对应国内赛事 核心能力要求
铜级 CSP-J初阶 基础语法、简单模拟、枚举算法
银级 CSP-J高阶/CSP-S初阶 贪心算法、DFS/BFS、基础图论
金级 CSP-S中阶 动态规划、高级图论、数据结构优化
铂金级 NOIP/CSP-S高阶 组合数学、高级DP优化、计算几何

四、科学备考策略

1.基础能力构建(铜级)

建议投入150-200小时系统学习,重点掌握:

C++/Java/Python基础语法

时间复杂度分析

线性数据结构(数组/链表/栈/队列)

简单搜索算法(二分/枚举)

2.中级提升路径(银级)

需额外投入300小时专项训练:

树形数据结构(二叉树/堆)

图论基础(邻接表/最短路径)

递归与回溯算法

文件输入输出处理

3.高阶突破要点(金级及以上)

建议500+小时强化训练:

动态规划(背包问题/状态压缩)

网络流与匹配算法

线段树/红黑树实现

数论与组合数学应用

五、常见问题解答

Q:是否需要学习特定编程语言?

A:官方支持C++、Java、Python、C四种语言,其中C++在算法实现效率上更具优势,约85%的晋级选手选择该语言。

Q:晋级后能否跨级参赛?

A:当季晋级后可立即参加后续场次的高级别赛事。例如12月铜级晋级,1月即可参与银级竞赛。

【扫码免费领取】USACO真题+备赛书单+一对一备考规划!

预约最新真题讲座、课程详情可添加下方顾问老师咨询

思维导图

USACO竞赛新规解读!面对USACO新规我们可以做些什么?

随着人工智能(AI)技术的迅速发展,其在算法和编程领域的应用对传统的竞赛模式带来了新的挑战。为了维护比赛的公平性和公信力,USACO(USA Computing Olympiad)官方制定了一系列新规。以下是详细的规则解读和应对策略。

一、成绩认证制度革新

1.新规内容

特定时间窗口:在金组和铂金组别中,所有参赛选手须在美国东部时间周六12:00 PM的特定时间窗口开始比赛,才能获得“认证成绩”。

认证成绩的重要性:只有获得“认证成绩”的选手才有资格晋级更高组别或获得相关荣誉。

2.影响与应对

这一规定确保了所有参赛者在同一时间段内进行比赛,减少了作弊的可能性。

3.建议

提前准备:确保在比赛当天没有其他冲突,并提前测试网络环境和设备,避免因技术问题错过比赛。

心理准备:集中比赛可能会增加心理压力,建议提前进行模拟训练,适应比赛节奏。

二、严格禁止使用 AI 和 VPN

1.新规内容

禁用生成式 AI:竞赛期间,严禁利用生成式AI(如ChatGPT)及其他自动化工具辅助解题。

禁止更改IP地址:美国地区参赛者禁止使用VPN隐匿真实位置,不得更改IP地址。

违规处罚:违规者将面临账号封禁,以此保障比赛公平公正。

2.影响与应对

杜绝作弊行为:这些规定旨在防止使用外部工具或更改地理位置以获取不公平优势的行为。

诚信教育:强调比赛的公平性和诚信原则,要求参赛者自觉遵守规则。

3.建议

自律性:参赛者应自觉遵守比赛规则,不依赖任何外部工具或手段。

网络安全:确保比赛期间使用的网络环境安全稳定,避免因网络问题导致误判为违规行为。

三、晋级难度提升

1.新规内容

满分晋级:比赛中斩获满分(1000分),可即刻晋级到更高组别。

常规晋级:未达满分者,需等待晋级分数线公布。一般700 - 800分是安全晋级线。

认证成绩要求:金级升铂金级,需获得“认证成绩”。

2.影响与应对

高分竞争加剧:满分晋级的机会使得高分竞争更加激烈,参赛者需要更加注重细节和准确性。

分数策略:未达满分的选手需要根据公布的晋级分数线来评估自己的表现,合理安排答题策略。

3.建议

提高准确率:在比赛中不仅要追求速度,还要确保答案的准确性,减少不必要的错误。

优化答题顺序:优先解决自己有把握的题目,确保拿到基础分数后再尝试难题。

四、防作弊措施加强

1.新规内容

技术与人工手段结合:USACO加强了防作弊措施,包括技术手段(如监测代码相似度、异常行为等)和人工手段(如审查可疑行为、举报机制等)。

严重后果:一经发现作弊行为,将会终身禁赛,并且会通知学生所在学校。

2.影响与应对

威慑作用:严格的防作弊措施起到了强大的威慑作用,减少了作弊行为的发生。

诚信意识:参赛者需要增强诚信意识,认识到作弊行为不仅影响个人前途,还会对学校声誉造成负面影响。

3.建议

自我约束:参赛者应自觉遵守比赛规则,树立良好的诚信意识。

团队合作:如果遇到疑似作弊行为,可以通过正规渠道进行举报,共同维护比赛的公平性。

【扫码免费领取】USACO真题+备赛书单+一对一备考规划!

预约最新真题讲座、课程详情可添加下方顾问老师咨询

思维导图

计算机专业的高配竞赛!USACO竞赛不同竞赛基础如何备考?

在申请美国顶尖大学时,特别是STEM领域,USACO成绩常常成为招生官审阅申请材料时的重要考量指标。优秀的竞赛成绩不仅能够证明学生的技术能力,还展示了其对计算机科学的热爱与追求。

USACO竞赛不同竞赛基础备考攻略

1.对于没有编程基础的学生

选择合适的入门语言:

Python:

优点: 语法简单易学,代码简洁明了,适合初学者快速入门。

资源丰富: 拥有大量的学习资源和社区支持,可以帮助学习者快速解决问题。

应用广泛: Python在数据分析、人工智能、Web开发等领域应用广泛,为未来发展提供更多可能性。

Java:

优点: 语法严谨,具有很强的通用性和跨平台性。

深厚底蕴: Java在企业级应用和Android开发中占据重要地位,学习Java可以为未来发展打下坚实基础。

面向对象: Java是面向对象的编程语言,学习Java可以帮助学习者理解面向对象编程的思想。

学习建议:

循序渐进: 从基础语法开始学习,逐步掌握变量、数据类型、控制结构、函数等基本概念。

多练习题: 通过大量的编程练习,巩固所学知识,提高编程能力。

项目实践: 尝试完成一些简单的项目,例如计算器、猜数字游戏等,将所学知识应用于实际问题。

2.对于有部分编程基础的学生

选择合适的编程语言:

C++或C:

优点: 这两门语言在编程竞赛中应用广泛,尤其是在USACO竞赛中,C++是主流语言。

性能优越: C++和C具有较高的执行效率,适合处理复杂的算法和数据结构问题。

学习资源: 拥有丰富的学习资源和竞赛经验,可以帮助学习者快速提升编程能力。

学习建议:

深入学习语法: 深入学习C++或C的语法,包括指针、内存管理、面向对象编程等高级概念。

掌握数据结构和算法:

基础数据结构: 掌握数组、链表、栈、队列、哈希表等基本数据结构。

基础算法: 掌握排序、搜索、递归、分治等基本算法。

参加编程竞赛:

模拟比赛: 参加模拟比赛,熟悉比赛环境,提高实战能力。

分析总结: 赛后分析总结,找出不足之处,并进行针对性改进。

3.对于有编程基础及编程经验的学生

目标设定:

冲击金级及以上奖项: 目标应定为在USACO竞赛中取得优异成绩,冲击金级及以上奖项。

学习重点:

深入学习算法:

排序算法: 掌握快速排序、归并排序、堆排序等高级排序算法。

搜索算法: 深入学习广度优先搜索(BFS)、深度优先搜索(DFS)、A*算法等。

图论算法: 掌握图的存储、遍历、最短路算法( Dijkstra、 Bellman-Ford、 Floyd-Warshall)、最小生成树算法(Prim、Kruskal)等。

掌握高级数据结构:

树结构: 深入学习二叉树、平衡树(例如AVL树、红黑树)、Trie树等。

图结构: 掌握图的存储方式,例如邻接矩阵、邻接表等。

提升算法应用能力:

多练习题: 通过大量练习官方金、白金级别的真题,熟悉各种算法应用场景。

总结解题技巧: 总结不同类型题目的解题方法,形成一套有效的解题流程。

【扫码免费领取】USACO真题+备赛书单+一对一备考规划!

预约最新真题讲座、课程详情可添加下方顾问老师咨询

思维导图

2025 年 2 月比赛——最终结果

2025年2月的比赛以算法编程问题为特色,涵盖了广泛的技术和难度水平。

在为期4天的比赛中,共有9339名不同的用户登录。来自100多个不同国家的7157名参与者提交了至少一个解决方案。3324名参与者来自美国,其中中国、加拿大、韩国、罗马尼亚、马来西亚、印度和新加坡的参与程度也很高。

总计有18455份已评级的提交材料,按语言细分如下:

11253 C++17
2750 Python-3.6.9
2194 C++11
2108 Java
128 C
22 Python-2.7.17

以下是白金、金牌、银牌和铜牌比赛的详细结果。您还可以找到每个问题的解决方案和测试数据。

USACO 2025年 2 月竞赛,白金奖

白金组共有 314 名参与者,其中 233 名是大学预科生。得分最高的成员的成绩在这里。祝贺所有最优秀的参赛者取得优异的成绩!

1 Min Max Subarrays
查看问题 | 测试数据 | 解决方案
2 Transforming Pairs
查看问题 | 测试数据 | 解决方案
3 True or False Test
查看问题 | 测试数据 | 解决方案

USACO 2025 年 2 月比赛,金奖

黄金组共有 1196 名参与者,其中 879 名是大学预科生。所有在本次比赛中获得 700 分或更高认证分数的参赛者将自动晋升到白金组。获得晋升的美国大学预科生名单在这里

1 Bessie's Function
查看问题 | 测试数据 | 解决方案
2 The Best Subsequence
查看问题 | 测试数据 | 解决方案
3 Friendship Editing
查看问题 | 测试数据 | 解决方案

USACO 2025 年 2 月比赛,银奖

白银组共有 3185 名参与者,其中 2451 名是大学预科生。所有在本次比赛中获得 700 分或更高分的参赛者将自动晋级金牌组。

1 The Best Lineup
查看问题 | 测试数据 | 解决方案
2 Vocabulary Quiz
查看问题 | 测试数据 | 解决方案
3 Transforming Pairs
查看问题 | 测试数据 | 解决方案

USACO 2025 年 2 月比赛,铜奖

白银组共有 4875 名参与者,其中 3712 名是大学预科生。所有在本次比赛中获得 700 分或更高分的参赛者将自动晋级银奖组。

1 Reflection
查看问题 | 测试数据 | 解决方案
2 Making Mexes
查看问题 | 测试数据 | 解决方案
3 Printing Sequences
查看问题 | 测试数据 | 解决方案

结束语

2024-25赛季似乎转瞬即逝,又一场成功的比赛已经过去。我很高兴看到强劲的表现和大量晋升,以及比赛本身的顺利运作。

对于那些尚未晋升的人,请记住,你练习得越多,你的算法编码技能就会越好——请坚持下去!USACO竞赛旨在挑战即使是最优秀的学生,要想在他们身上脱颖而出,可能需要付出大量的努力。

大量的人为USACO竞赛的质量和成功做出了贡献。本次竞赛的协助人员包括Chongtian Ma, Alex Liang, Haokai Ma, Andrew Li, Avnith Vijayram, Michelle Wei, Nick Wu, Nathan Wang, Brandon Wang, Spencer Compton, Tina Wang, Jichao Qian, Suhas Nagar, Ho Tin Fan, Daniel Zhang, William Lin, Larry Xing, Weiming Zhou, Benjamin Chen, Andi Qu, Thomas Liu, and Benjamin Qi。也感谢我们的翻译帮助我们扩大比赛的范围。最后,我们非常感谢USACO赞助商的慷慨支持:Citadel、Ansatz、X-Camp、TwoSigma、VPlanet Coding、EasyFunCoding、Orijtech和Jump Trading。

我们期待着再次见到大家参加公开赛,我们的全国冠军!

编码愉快!

2025 年 2 月USACO竞赛铜奖组问题三— Printing Sequences

Bessie is learning to code using a simple programming language. She first defines a valid program, then executes it to produce some output sequence.

Defining:

A program is a nonempty sequence of statements.
A statement is either of the form "PRINT c" where c is an integer, or "REP o", followed by a program, followed by "END," where o is an integer that is at least 1.

Executing:

Executing a program executes its statements in sequence.
Executing the statement "PRINT c" appends c to the output sequence.
Executing a statement starting with "REP o" executes the inner program a total of o times in sequence.

An example of a program that Bessie knows how to write is as follows.

The program outputs the sequence [1,2,2,1,2,2,1,2,2].

Bessie wants to output a sequence of N (1≤N≤100) positive integers. Elsie challenges her to use no more than K (1≤K≤3) "PRINT" statements. Note that Bessie can use as many "REP" statements as she wants. Also note that each positive integer in the sequence is no greater than K.

For each of T (1≤T≤100) independent test cases, determine whether Bessie can write a program that outputs some given sequence using at most K "PRINT" statements.

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains T.

The first line of each test case contains two space-separated integers, N and K.

The second line of each test case contains a sequence of N space-separated  positive integers, each at most K, which is the sequence that Bessie wants to produce.

OUTPUT FORMAT (print output to the terminal / stdout):

For each test case, output "YES" or "NO" (case sensitive) on a separate line.

SAMPLE INPUT:

2
1 1
1
4 1
1 1 1 1

SAMPLE OUTPUT:

YES
YES

For the second test case, the following code outputs the sequence [1,1,1,1] with 1 "PRINT" statement.

SAMPLE INPUT:

11
4 2
1 2 2 2
4 2
1 1 2 1
4 2
1 1 2 2
6 2
1 1 2 2 1 1
10 2
1 1 1 2 2 1 1 1 2 2
8 3
3 3 1 2 2 1 2 2
9 3
1 1 2 2 2 3 3 3 3
16 3
2 2 3 2 2 3 1 1 2 2 3 2 2 3 1 1
24 3
1 1 2 2 3 3 3 2 2 3 3 3 1 1 2 2 3 3 3 2 2 3 3 3
9 3
1 2 2 1 3 3 1 2 2
6 3
1 2 1 2 2 3

SAMPLE OUTPUT:

YES
NO
YES
NO
YES
YES
YES
YES
YES
NO
NO

For the first test case, the following code outputs the sequence [1,2,2,2] with 2
"PRINT" statements.

For the second test case, the answer is "NO" because it is impossible to output the sequence [1,1,2,1] using at most 2 "PRINT" statements.

For the sixth test case, the following code outputs the sequence [3,3,1,2,2,1,2,2]
with 3 "PRINT" statements.

SCORING:

Input 3: K=1
Inputs 4-7: K≤2
Inputs 8-13: No additional constraints.

Problem credits: Alex Liang

扫码领取USACO试题答案+详细解析

咨询一对一备赛规划

2025 年 2 月USACO竞赛铜奖组问题二— Making Mexes

You are given an array a of N non-negative integers a1,a2,…,aN (1≤N≤2⋅105,0≤aiN). In one operation, you can change any element of a
to any non-negative integer.

The mex of an array is the minimum non-negative integer that it does not contain. For each i in the range 0 to N inclusive, compute the minimum number of operations you need in order to make the mex of a equal i.

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains N.
The next line contains a1,a2,…,aN.

OUTPUT FORMAT (print output to the terminal / stdout):

For each i in the range 0 to N, output the minimum number of operations for i on a new line. Note that it is always possible to make the mex of a equal to any i in the range 0 to N.

SAMPLE INPUT:

4
2 2 2 0

SAMPLE OUTPUT:

1
0
3
1
2

  • To make the mex of a equal to 0, we can change a4 to 3 (or any positive integer). In the resulting array, [2,2,2,3], 0 is the smallest non-negative integer that the array does not contain, so 0 is the mex of the array.
  • To make the mex of a equal to 1, we don't need to make any changes since 1 is already the smallest non-negative integer that a=[2,2,2,0] does not contain.
  • To make the mex of a equal to 2, we need to change the first three elements of a. For example, we can change a to be [3,1,1,0].

SCORING:

Inputs 2-6: N≤103

Inputs 7-11: No additional constraints.

Problem credits: Benjamin Qi

扫码领取USACO试题答案+详细解析

咨询一对一备赛规划

2025 年 2 月USACO竞赛铜奖组问题一— Reflection

Farmer John has a square canvas represented by an N by N grid of cells (2≤N≤2000, N is even). He paints the canvas according to the following steps:

1.First, he divides the canvas into four equal quadrants, separated by horizontal and vertical lines through the center of the canvas.
2.Next, he paints a lovely painting in the top-right quadrant of the canvas. Each cell of the top-right quadrant will either be painted (represented by '#') or unpainted (represented by '.').
3.Finally, since he is so proud of his painting, he reflects it across the previously mentioned horizontal and vertical lines into the other quadrants of the canvas.

For example, suppose N=8 and FJ painted the following painting in the top-right quadrant in step 2:

.#..
.#..
.##.
....

Then after reflecting across the horizontal and vertical lines into the other quadrants in step 3, the canvas would look as follows:

..#..#..
..#..#..
.##..##.
........
........
.##..##.
..#..#..
..#..#..

However, while FJ was sleeping, Bessie broke into his barn and stole his precious canvas. She totally vandalized the canvas—removing some painted cells and adding more painted cells! Before FJ woke up, she returned the canvas to FJ.

FJ would like to modify his canvas so that it once again satisfies the reflective condition: that is, it is the result of reflecting the top-right quadrant into each of the other quadrants. Since he only has a limited number of resources, he would like to achieve this in as few operations as possible, where a single operation consists of either painting a cell or removing paint from a cell.

You are given the canvas after Bessie's vandalism, as well as a sequence of U
(0≤U≤105) updates to the canvas, each toggling a single cell to '.' if it is '#', or vice versa. Before any updates, and after each update, output the minimum number of operations x FJ needs to perform so that the reflective condition is satisfied.

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains integers N and U.

The next N lines each contain N characters representing the canvas after Bessie's vandalism. Every character is either '#' or '.'.

The following U lines each contain integers r and c, where 1≤r,cN, representing an update to the cell in the rth row from the top and cth column from the left of the canvas.

OUTPUT FORMAT (print output to the terminal / stdout):

Output U+1 lines representing x before any updates and after each update.

SAMPLE INPUT:

4 5
..#.
##.#
####
..##
1 3
2 3
4 3
4 4
4 4

SAMPLE OUTPUT:

4
3
2
1
0
1

The following canvas satisfies the reflective condition and differs from the original canvas by 4 operations:

....
####
####
....

It is impossible to make the original canvas satisfy the reflective condition using fewer than 4 operations.

After updating (1,3), the canvas looks like this:

....
##.#
####
..##
It now takes 3 operations to make the canvas satisfy the reflective condition.

After updating (2,3), the canvas looks like this:

....
####
####
..##

It now takes 2 operations to make the canvas satisfy the reflective condition.

SCORING:

Inputs 2-3: N≤4
Inputs 4-6: U≤10
Inputs 7-16: No additional constraints.

Problem credits: Chongtian Ma

扫码领取USACO试题答案+详细解析

咨询一对一备赛规划

2025 年 2 月USACO竞赛银奖组问题三—Transforming Pairs

Bessie the brilliant bovine has discovered a new fascination—mathematical magic! One day, while trotting through the fields of Farmer John’s ranch, she stumbles upon two enchanted piles of hay. The first pile contains a bales, and the second pile contains b bales (1≤a,b≤1018).

Next to the hay, half-buried in the dirt, she discovers an ancient scroll. As she unfurls it, glowing letters reveal a prophecy:

To fulfill the decree of the Great Grasslands, the chosen one must transform these two humble hay piles into exactly c and d bales—no more, no less.

Bessie realizes she can only perform the following two spells:

She can magically conjure new bales to increase the first pile’s size by the amount currently in the second pile.
She can magically conjure new bales to increase the second pile’s size by the amount currently in the first pile.

She must perform these operations sequentially, but she can perform them any number of times and in any order. She must reach exactly c bales in the first pile and d bales in the second pile (1≤c,d≤1018).

For each of T(1≤T≤104) independent test cases, output the minimum number of operations needed to fulfill the prophecy, or if it is impossible to do so, output -1.

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains T.

The next T lines each contain four integers a,b,c,d.

OUTPUT FORMAT (print output to the terminal / stdout):

Output T lines, the answer to each test case.

SAMPLE INPUT:

4
5 3 5 2
5 3 8 19
5 3 19 8
5 3 5 3

SAMPLE OUTPUT:

-1
3
-1
0

In the first test case, it is impossible since b>d initially, but operations can only increase b.

In the second test case, initially the two piles have (5,3) bales. Bessie can first increase the first pile by the amount in the second pile, resulting in (8,3) bales. Bessie can then increase the second pile by the new amount in the first pile, and do this operation twice, resulting in (8,11) and finally (8,19) bales. This matches c and d and is the minimum number of operations to get there.

Note that the third test case has a different answer than the second because c and d are swapped (the order of the piles matters).

In the fourth test case, no operations are necessary.

SAMPLE INPUT:

1
1 1 1 1000000000000000000

SAMPLE OUTPUT:

999999999999999999

SCORING:

Inputs 3-4: max(c,d)≤20⋅min(a,b)
Inputs 5-7: T≤10 and a,b,c,d≤106
Inputs 8-12: No additional constraints

Problem credits: Benjamin Qi

扫码领取USACO试题答案+详细解析

咨询一对一备赛规划

2025 年 2 月USACO竞赛银奖组问题二—Vocabulary Quiz

Bessie is helping Elsie with her upcoming vocabulary quiz. The words to be tested will be drawn from a bank of M distinct words, where no word in the bank is a prefix of another word in the bank.

While the bank is nonempty, Bessie will select a word from the bank, remove it from the bank, and read it to Elsie one character at a time from left to right. Elsie's task is to tell Bessie once she can uniquely identify it, after which Bessie will stop reading.

Bessie has already decided to read the words from the word bank in the order w1,w2,…,wM. If Elsie answers as quickly as possible, how many characters of each word will Bessie read?

The words are given in a compressed format. We will first define N+1 (1≤N≤ 106 ) distinct words, and then the word bank will consist of all those words that are not a prefix of another word. The words are defined as follows:

Initially, the 0th word will be the empty string.

Then for each 1≤i≤N, the ith word will be equal to the pith word plus an additional character at the end (0≤pi<i). The characters are chosen such that all N+1 words are distinct.

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains N, where N+1 is the number of words given in the  compressed format.

The next line contains p1,p2,…,pN where pi represents that the i-th word is formed by taking the pi-th word and adding a single character to the end.

Let M be the number of words that are not a prefix of some other word. The next M lines will contain w1,w2,…,wM, meaning that the with word will be the ith to be read. It is guaranteed that the words to be read form a permutation of the words in the word bank.

OUTPUT FORMAT (print output to the terminal / stdout):

Output M lines, where the ith line contains the number of characters of the ith word that Bessie reads.

SAMPLE INPUT:

5
0 1 2 3 4
5

SAMPLE OUTPUT:

0

There are 6words labeled 0…5. Word 5 is the only one that is not a prefix of another word, so it is the only one in the bank. In general, once only one word is left in the bank, Elsie won't need any characters to identify it.

SAMPLE INPUT:

4
0 0 1 1
4
3
2

SAMPLE OUTPUT:

2
1
0

The bank consists of words 2, 3, and 4.

Elsie needs two characters to identify word 4 since words 3 and 4 share their first character in common.

Once Bessie reads the first character of word 3 , Elsie has enough characters to uniquely identify it, since word 4 was already read.

SAMPLE INPUT:

4
0 0 1 1
2
3
4

SAMPLE OUTPUT:

1
2
0

SCORING:

Inputs 4-5: No word has length greater than 20.
Inputs 6-10: The sum of the lengths of all words in the word bank does not exceed 107.
Inputs 11-18: No additional constraints.

Problem credits: Benjamin Qi

扫码领取USACO试题答案+详细解析

咨询一对一备赛规划

在线咨询
微信咨询