6.Sum square difference

问题描述

The sum of the squares of the first ten natural numbers is,

The square of the sum of the first ten natural numbers is,

Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is $3025−385=2640$.

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

解法

emmm……

第一次遇到这么简单的题目?

我们可以直接遍历到$100$即可求出两个值$sum1$和$sum2$,然后根据题意输出即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <bits/stdc++.h>
#define ll long long
#define IOS {ios::sync_with_stdio(false);cin.tie(0);}
using namespace std;
ll suma, sumb;
int main(void)
{
IOS
for(ll i = 1; i <= 100; i++)
{
suma += i * i;
}
//cout << suma << endl;
for(ll i = 1; i <= 100; i++)
{
sumb += i;
}
//cout << sumb << endl;
cout << sumb * sumb - suma << endl;
return 0;
}

答案给出了一种推导$F(n)$的解法,可以在${O(1)}$时间里求得…

可以参考(虽然我觉得直接$100$暴力就可以…..)

注意点

注意精度问题就行了 最好采用long long 来定义变量..避免发生上一种题目的情况。。。

正确答案

官方解答