hdu 1178 Heritage from father (推导)

题意:

  有一个金币堆的金字塔,最上层就有一个金币,以后的i层都是边长为i的实心三角形,给你层数,问:一共有多少个金币?(用科学计数法表示,并且保留两位小数)

解题思路:

  根据题意可知求出1*n+2*(n-1)+3*(n-2)+4*(n-3)+.......+(n-2)*3+(n-1)*2+n*1的和即可,但是要化简一下求出n*(n+1)*(n+2)/6;

证明:

  1 * n + 2 (n - 1) + 3 (n - 2) + …… + (n - 2) * 3 + (n - 1) * 2 + n * 1

  = 1 * (n + 1 - 1) + 2 (n + 1 - 2) + 3 (n + 1 - 3) + …… + (n - 2) * (n + 1 - (n - 2)) + (n - 1) * (n + 1 - n - 1) + n * (n + 1 - n)

  =(1 + 2 + 3 + ... + n)(n + 1) - (1^2 + 2^2 + 3^2 + ..... + n^2)

  =n (n + 1)^2 / 2 - n (n + 1) (2n + 1) / 6       (n (n + 1) (2n + 1) / 6后面会证明)

  =n * (n + 1) * (n + 2) / 6          (证毕)

  (n+1)^3-n^3=3n^2+3n+1,

  n^3-(n-1)^3=3(n-1)^2+3(n-1)+1

  

  3^3-2^3=3*(2^2)+3*2+1

  2^3-1^3=3*(1^2)+3*1+1.

  这n个等式两边分别相加得出:

  (n+1)^3-1=3(1^2+2^2+3^2+....+n^2)+3(1+2+3+...+n)+n,

  (n+1)^3-1=3(1^2+2^2+3^2+....+n^2)+3((n+1)n/2)+n,

  整理可得上述式子。

代码:

 1 #include <stdio.h>
 2 int main ()
 3 {
 4     int n, i, k;
 5     double s;
 6
 7     while (scanf ("%d", &n), n)
 8     {
 9         s = 1.0 * n * (n+1) * (n+2) / 6;
10         k = 0;
11         while (s >= 10)
12         {
13             s /= 10;
14             k++;
15         }
16         printf ("%.2fE%d\n", s, k);
17     }
18
19     return 0;
20 }

  
 
时间: 2024-11-09 23:45:55

hdu 1178 Heritage from father (推导)的相关文章

HDU Heritage from father

Heritage from father Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131070/65535K (Java/Other) Total Submission(s) : 71   Accepted Submission(s) : 18 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Famous Harry Po

hdu 5312 Sequence(数学推导——三角形数)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5312 Sequence Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 1336    Accepted Submission(s): 410 Problem Description Today, Soda has learned a

一些项目——Heritage from father

Problem Description Famous Harry Potter,who seemd to be a normal and poor boy,is actually a wizard.Everything changed when he had his birthday of ten years old.A huge man called 'Hagrid' found Harry and lead him to a new world full of magic power. If

hdu 5312 Sequence【数学推导】

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5312 解法: 这个题看上去是一个贪心, 但是这个贪心显然是错的. 事实上这道题目很简单, 先判断1个是否可以, 然后判断2个是否可以. 之后找到最小的整数k (k > 2), 使得(m - k) mod 6 = 0即可. 证明如下: 3n(n-1)+1 = 6(n*(n-1)/2)+1, 注意到n*(n-1)/2是三角形数, 任意一个自然数最多只需要3个三角形数即可表示. 枚举需要k个. 事实上,

HDOJ(HDU) 2524 矩形A + B(推导公式、)

Problem Description 给你一个高为n ,宽为m列的网格,计算出这个网格中有多少个矩形,下图为高为2,宽为4的网格. Input 第一行输入一个t, 表示有t组数据,然后每行输入n,m,分别表示网格的高和宽 ( n < 100 , m < 100). Output 每行输出网格中有多少个矩形. Sample Input 2 1 2 2 4 Sample Output 3 30 此方格其实就是求其中所有格子数,如果按宽度来算的话,1,2,3,-m,种情况,对每一种情况,有(1+2

hdu 5312 Sequence(数学推导+线性探查(两数相加版))

Problem Description Today, Soda has learned a sequence whose n-th (n≥1) item is 3n(n−1)+1. Now he wants to know if an integer m can be represented as the sum of some items of that sequence. If possible, what are the minimum items needed? For example,

hdu 5430 Reflect (数学推导题)

Problem Description We send a light from one point on a mirror material circle,it reflects N times and return the original point firstly.Your task is calcuate the number of schemes. ![](../../data/images/C628-1004-1.jpg) Input First line contains a s

hdu 5073 Galaxy (数学+推导)

Galaxy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 553    Accepted Submission(s): 126 Special Judge Problem Description Good news for us: to release the financial pressure, the government

HDOJ/HDU 1297 Children’s Queue(推导~大数)

Problem Description There are many students in PHT School. One day, the headmaster whose name is PigHeader wanted all students stand in a line. He prescribed that girl can not be in single. In other words, either no girl in the queue or more than one