21532: 数学密码
Brute force, http://cs101.openjudge.cn/practice/21532
小明和同学一起去玩密室逃脱,这个密室的老板特别喜欢数学,所以他设置了一个密码门。在搜集了房间里的有价值信息后,小明总结出的信息为:三个互不相同的正整数和为231,那么这三个互不相同正整数的最大公因数就是密码。小明很快的算出了密码。
回到学校后,小明希望写一个程序,对于给定的三个互不相同的正整数的和,快速求出这三个正整数的最大公因数。
描述部分对应英文:
Xiao Ming went to play escape room with his classmates. The owner of this room is especially fond of mathematics, so he set up a password door. After collecting valuable information from the room, Xiao Ming concluded the information as follows: three mutually dissimilar positive integers and 231, then the greatest common factor of these three mutually dissimilar positive integers is the password. Xiao Ming quickly worked out the password.
After returning to school, Xiao Ming wishes to write a program to quickly find the greatest common factor of the three mutually dissimilar positive integers for a given sum of these three positive integers.
输入
输入为三个互不相同的正整数的和 (6 ≤ N ≤ 10^9).
输出
输出三个数的最大公因数.
样例输入
sample1 in:
231
sample1 out:
33样例输出
sample2 in:
1234
sample2 out:
2
sample3 in:
88
sample3 out:
11来源: cs101 2020 Final Exam
出这个题目的思路,来自学而思小学高年级数学的一次板书。

图 一个小学高年级数学题目
对于大于等于6的整数,可以表示为三个互不相同的正整数的和。可以从1和 2开始。由于S-3必定大于2,所以我们可以确保这三个数互不相同。
n=int(input())
i = 1 + 2 + 3
while n%i != 0:
i += 1
else:
print(n // i)这种方法体现了深刻的数学洞察力,可能不是短时间内就能想到的。在解决这类问题时,用计算机思维的枚举策略同样能够获得正确的答案(AC)。如果超时了,可以考虑改变遍历方向。OJ评测系统中题目的测试数据,通常比较弱,考试时候给出的测试数据一般不超过20组。
t = int(input())
s = 1 + 2 +3
while True:
while t%s !=0:
s += 1
'''
if s > t**0.5:
t = s
break
'''
found = False
for x in range(1, s):
for y in range(x+1, s):
if y==x:
continue
for z in range(y+1, s):
if s - x - y != z:
continue
if z==y or z==x:
continue
#print(f'{x},{y},{z}')
found = True
break
if found:
break
if found:
break
if found:
break
print(t//s)