
正文
nyoj 122-Triangular Sums (数学之读懂求和公式的迭代)
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
122-Triangular Sums
内存限制:64MB
时间限制:3000ms
特判: No
通过数:5
提交数:7
难度:2
题目描述:
The
n
th
Triangular
number,
T
(
n
) = 1 + … +
n
, is the sum of the first
n
integers. It is the number of points in a triangular array with
n
points on side. For example
T
(4)
:
X
X X
X X X
X X X X
Write a program to compute the weighted sum of triangular numbers:
W
(
n
) =
SUM[
k
= 1…
n
;
k
*
T
(
k
+ 1)]
The n th Triangular number, T ( n ) = 1 + … + n , is the sum of the first n integers. It is the number of points in a triangular array with n points on side. For example T (4) :
X
X X
X X X
X X X X
X X
X X X
X X X X
Write a program to compute the weighted sum of triangular numbers:
W
(
n
) =
SUM[
k
= 1…
n
;
k
*
T
(
k
+ 1)]
输入描述:
The first line of input contains a single integer N, (1 ≤ N ≤ 1000) which is the number of datasets that follow.
Each dataset consists of a single line of input containing a single integer n, (1 ≤ n ≤300), which is the number of points on a side of the triangle.
The first line of input contains a single integer N, (1 ≤ N ≤ 1000) which is the number of datasets that follow. Each dataset consists of a single line of input containing a single integer n, (1 ≤ n ≤300), which is the number of points on a side of the triangle.
输出描述:
For each dataset, output on a single line the dataset number (1 through N), a blank, the value of n for the dataset, a blank, and the weighted sum ,W(n), of triangular numbers for n.
For each dataset, output on a single line the dataset number (1 through N), a blank, the value of n for the dataset, a blank, and the weighted sum ,W(n), of triangular numbers for n.
样例输入:
复制
4
3
4
5
10
4
3
4
5
10
样例输出:
1 3 45
2 4 105
3 5 210
4 10 2145
1 3 45
2 4 105
3 5 210
4 10 2145
C/C++ AC:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <stack>
#include <set>
#include <map>
#include <queue>
#include <climits> using namespace std;
const int MY_MAX = ;
int T[MY_MAX] = {}, W[MY_MAX] = {}, n; void cal_W()
{
T[] = , T[] = ;
for (int i = ; i < MY_MAX - ; ++ i)
{
W[i] = W[i - ] + i * T[i + ];
T[i + ] = T[i + ] + i + ;
}
} int main()
{
cal_W();
cin >>n;
for (int i = ; i <= n; ++ i)
{
int temp;
scanf("%d", &temp);
printf("%d %d %d\n", i, temp, W[temp]);
}
}







