Joana loves playing with odd numbers. In the other day, she started writing, in each line, an odd number of odd numbers. It looked as follows:
1
3 5 7
9 11 13 15 17
19 21 23 25 27 29 31
...
On a certain line Joana wrote 55 odd numbers. Can you discover the sum of the last three numbers written in that line? Can you do this more generally for a given quantity of odd numbers?
Given the number N of odd numbers in a certain line, your task is to determine the sum of the last three numbers of that line.
Input
The input is a sequence of lines, one odd number N (1 < N <1000000000) per line
Output
For each input line write the sum of the last three odd numbers written by Joana in that line with N numbers. This sum is guaranteed to be less than 2^63.
Sample Input
3
5
7
Sample Output
15
45
87
问题链接:UVA913 Joana and the Odd Numbers
问题简述:
奇数1,3,5,...,按其个数顺序写成三角形。求有n个数字那行的最后三个数字之和。
问题分析:
这个问题的关键是找出计算规律。
给定的n则存在k,满足n=2*k-1,k为行数。那么k=(n+1)/2。 按照奇数和公式,第k行结束的数字为第k*k个奇数,即2k*k-1=(n+1)/2。那么,最后三个数字分别为2*k*k-1、2*k*k-3和2*k*k-5,其和为6*k*k-9。
程序说明:(略)题记:(略)
参考链接:(略)
AC的C++语言程序如下:
/* UVA913 Joana and the Odd Numbers */ #include <iostream> using namespace std; int main() { long long n; while(cin >> n) cout << ((n + 1) / 2 * (n + 1) / 2) * 6 - 9 << endl; return 0; }