POJ 3086 Triangular Sums

Triangular Sums

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6371   Accepted: 4529

Description

The nth 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…nk * T(k + 1)]

Input

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.

Output

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.

Sample Input

4
3
4
5
10

Sample Output

1 3 45
2 4 105
3 5 210
4 10 2145

Source

Greater New York 2006

找规律的一道题,其实题目已经说的很明白了。

先打表再输出,开始怕时限,所以一直改不对,干脆分开算。

代码:

#include <iostream>
#include <algorithm>
#include <stdio.h>
using namespace std;
int f[305],T[305],sum[305];
int main()
{
    int i,j,n,m;
    T[1]=1;f[1]=1;
    for(i=2;i<305;i++)
    T[i]=i+T[i-1];
    for(i=1;i<304;i++)
    f[i]=i*T[i+1];
    sum[1]=f[1];
    for(i=2;i<305;i++)
    sum[i]=sum[i-1]+f[i];
    while(scanf("%d",&n)!=EOF&&n)
    {
        for(i=1;i<=n;i++)
        {
            scanf("%d",&m);
            printf("%d %d %d\n",i,m,sum[m]);
        }
    }
    return 0;
}

POJ 3086 Triangular Sums,布布扣,bubuko.com

时间: 2024-10-03 19:56:57

POJ 3086 Triangular Sums的相关文章

南阳122(Triangular Sums)

Triangular Sums 时间限制:3000 ms  |  内存限制:65535 KB 难度:2 描述 The nth 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): XX XX X XX X X X Write a

POJ 1948 Triangular Pastures(DP)

Description Like everyone, cows enjoy variety. Their current fancy is new shapes for pastures. The old rectangular shapes are out of favor; new geometries are the favorite. I. M. Hei, the lead cow pasture architect, is in charge of creating a triangu

POJ 2140 Herd Sums

http://poj.org/problem?id=2140 Description The cows in farmer John's herd are numbered and branded with consecutive integers from 1 to N (1 <= N <= 10,000,000).  When the cows come to the barn for milking, they always come in sequential order from 1

Triangular Sums

描述 The nth 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): XX XX X XX X X X Write a program to compute the weighted sum of triangular n

POJ 1948 Triangular Pastures(二维背包)

Triangular Pastures Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6774   Accepted: 2199 Description Like everyone, cows enjoy variety. Their current fancy is new shapes for pastures. The old rectangular shapes are out of favor; new geo

poj 2140 Herd Sums(等差数列)

题目链接:http://poj.org/problem?id=2140 题意:给出n,求一共有多少个连续的数满足加和恰好得到n, 思路:这题想了好久一开始以为是打表找规律,后来才发现与等差数列有关...(这题也可以用DP来做) 根据等差数列求和公式S=(a1+an)*n/2和末项公式an=a1+(n-1)*d(d位公差)得a1=(2*s+n-n*n)/2/n;得出求a1的公式然后对所有的n(n为项数)进行枚举,得出结果 2*s=(2*a1+n-1)*n,因为2*S必为偶数所以n为偶数或者(2*a

POJ 2140 Herd Sums 公式推导

题意:给出n<=1e7 求有多少个连续数之和等于k x+x+1+....x+k=n (k+1)k/2+(k+1)x=n (k+1)k+(k+1)2x=2*n (k+1)*(2x+k)=2*n    2*n为偶 k+1,2x+k都为2*n因子 &&一奇一偶 得到:2*n有多少个奇因子(或者偶因子)就有多少对解 (k,n固定可以得到首项x 得出一解) #include <iostream> #include <cstdio> #include <cmath&

HOJ 题目分类

转自:http://blog.sina.com.cn/s/blog_65f3869301011a1o.html ******************************************************************************* 简单题(包括枚举,二分查找,(复杂)模拟,基础数据结构(栈.队列),杂题等 ****************************************************************************

Poj OpenJudge 百练 2602 Superlong sums

1.Link: http://poj.org/problem?id=2602 http://bailian.openjudge.cn/practice/2602/ 2.Content: Superlong sums Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 22337   Accepted: 6577 Description The creators of a new programming language D++