列表

详情


NC25067. [USACO 2007 Mar L]Professor Snarf

描述

Bessie went down to town to see her former teacher, Professor Snarf.
Snarf was a clever math professor who often answered questions in "Where do you live?" asked Bessie.
"I live on University Drive, of course," said Snarf. "University Drive is an interesting street since all the houses are on the same side of the road looking at the beautiful University Park. The houses start at number 1 and are numbered sequentially through N. If I stick my head out the door and calculate the sum of all the house numbers to my right, I note that it is the same sum as the sum of the house numbers to my left. Isn't that interesting?"
Bessie knows that Snarf's address is larger than some integer A (3 <= A <= 1,940,500). Given A, calculate Snarf's smallest possible address and N, the number of houses on his street.
Bessie wants to test your program and will tell you that A is 3. Your program should figure out that Snarf lives at house 6 of 8 houses on the street (since 1+2+3+4+5 = 15 = 7+8).

输入描述

Line 1: A single integer: A

输出描述

Line 1: A single line with two space-separated integers: Snarf's address and N

示例1

输入:

3

输出:

6 8

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Python3 解法, 执行用时: 2001ms, 内存消耗: 0K, 提交时间: 2023-08-15 20:23:11

'''
n * n + 2 * n + 1 - 2*x = (x * x - * x) * 2
n*(n+1) = 2 * x * x
x * x - x  = 2*(n+x+1)*(n-x-1)
2*n*n = 3*x*x+3*x+2

x > A
'''

x = int(input()) + 1

while True:
    m = x*x*3 + x*3 + 2
    n = (m//2)**0.5
    if m % 2 == 0 and n == int(n) and n*(n+1) == 2*x*x:
        print(x, int(n))
        break
    else:
        x += 1

上一题