1. 程式人生 > >[ABC 099] B-Stone Monument

[ABC 099] B-Stone Monument

core select all auth class force main spa ORC

B - Stone Monument


Time limit : 2sec / Memory limit : 256MB

Score : 200 points

Problem Statement

In some village, there are 999 towers that are 1,(1+2),(1+2+3),…,(1+2+3+…+999) meters high from west to east, at intervals of 1 meter.

It had been snowing for a while before it finally stopped. For some two adjacent towers located 1

meter apart, we measured the lengths of the parts of those towers that are not covered with snow, and the results are a meters for the west tower, and b meters for the east tower.

Assuming that the depth of snow cover and the altitude are the same everywhere in the village, find the amount of the snow cover.

Assume also that the depth of the snow cover is always at least 1 meter.

Constraints

  • 1≤a<b<499500(=1+2+3+…+999)
  • All values in input are integers.
  • There is no input that contradicts the assumption.

[題目解釋]

  這裏有999座塔,第1座高度為1,第二座高度為(1+2),…,第999座高度為(1+2+3+…+999),現在有雪覆蓋在塔的底部即使塔的高度降低雪的厚度,給你兩座相鄰且高度分別為a,b(a<b)的塔,求雪的厚度為多少?

[題目解析]

  我們發現第1座塔高度為1,第二座塔高度為(1+2),得到第n座塔高度為公差為1從1到n的等差數列之和即(1+2+…+n)=(1+n)/2,因為a,b兩座塔是相鄰的,也就是說b-a可以得到b的等差數列最後一項,這樣我們就可以等差數列求和公式算出b塔最初的高度,b塔的最初高度減去現在b塔的高度及為答案(當然你也可以將b-a-1算出a等差數列的最後一項,從而算出a的原始高度,再算出雪的厚度)

[代碼]

/*
    Name: Stone Monument 
    Author: FZSZ-LinHua
    Date: 2018 06 10
    Exec time: 1ms
    Memory usage: 256KB
    Score: 200
    Algorithm: Brute-force 
*/
# include "iostream"
# include "cstdio"

using namespace std;

int a,b,now; 

int main(){
    scanf("%d%d",&a,&b);
    now=b-a;    //算出b等差數列的最後一項 
    printf("%d",(now+1)*now/2-b);     //(now+1)*now/2為b塔的原始高度(等差數列求和公式) 
    return 0; 
} 

[ABC 099] B-Stone Monument