Luo Gu solution to a problem P5534 XR-3 [[]] arithmetic sequence

I double Cheese Li was spicy cabbage!

topic

This question is the recent exams,Now a solution to a problem runoff

Pull back to the topic

The questions were very clear to tell you, this is an arithmetic sequence,

Then also tell you the first term and the second term, the number of items. You want to drop yesterday

Tell you the first and second terms, not subtraction is tolerance?

Now, you know the first term, tolerance and the number of items you regularly spaced series of, you ask it to individual items and.Set formula on the line ah

First put the formula:

A formula: Sn = (a1 + an) × n / 2;

Formula 2: Sn = n × a1 + n × (n - 1) × d / 2;

We see which formula should be used?

Because we know n, d, a1;

So we use the second formula would be better.

Then the followingProve it

Enumerate the entire series:

a1, a2, a3, a4, … a(n - 1), an;

Fn replace them all with a1 and d:

a1, (a1 + d), (a1 + 2d), … (a1 + d × (n - 2)), (a1 + d × (n - 1));

Then reversed gn?

(a1 + d × (n - 1)), (a1 + d × (n - 2)), … (a1 + 2d),(a1 + d), a1;

We take a look, f1 + g1 == f2 + g2 == f3 + g3;

So, we try to sum them, that is, 2 * s:

a1 × n × 2 + (n - 1) × d × n;

Then divided by 2 s not that it?

s = a1 × n + n × (n - 1) × d / 2;

Hair spicy Code:

#include<bits/stdc++.h>//万能头万岁!
using namespace std;

#define int long long//不开long long见祖先
int head, nxt, n, d;
// head是首项, nxt 是第二项, n是项数

signed main()
{
scanf("%lld%lld%lld", &head, &nxt, &n);//输入
d = nxt - head;//求公差。
printf("%lld", n * head + n * (n - 1) * d / 2);//套公式
return 0;
}

Ps: Please read and then copy qwq

Guess you like

Origin www.cnblogs.com/Flash-plus/p/12028337.html