题解[CodeForces171C]A Piece of Cake

Description

You \ (n-\) number, obtains \ (\ sum_ {i = 1 } ^ {n} a_ {i} \ times i \ qquad \)

Input

Co \ (n + 1 \) number, respectively, \ (n-\) and \ (n-\) number \ (A_ {I} \) ( \ (. 1 \) \ (\ I Leq \ n-Le \) ).

Output

\(\sum_{i=1}^{n} a_{i}\times i\qquad\)

Examples

Input

4 1 2 3 4

Output

30

Solution

According to the title translation shows: This is a simulation questions.

First, enter an integer \ (n-\) , the next input \ (n-\) integers are \ (A_. 1} {\) , \ (A_ {2} \) , \ (\ cdots \) , \ (n-A_ {} \) , you want to output \ (ANS \) \ (= \) \ (\ sum_. 1} = {I} n-A_ ^ {} {I \ I Times \ qquad \)

We can use one loop to enumerate \ (I \) , \ (I \) ranges from \ (. 1 \) ~ \ (n-\) , each entry \ (a_ {i} \) , can then be the answer \ (ANS \) increase \ (A_ {I} \) \ (\ Times \) \ (I \) , the final output answer \ (ANS \) can.

The time complexity of the algorithm: \ (\ Theta (n-) \) , the spatial complexity: \ (\ Theta (. 1) \)

Code

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>//头文件准备

using namespace std;//使用标准名字空间

inline int gi()//快速读入,不解释
{
    int f = 1, x = 0;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-') f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
    {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return f * x;
}

int n, sum;//n为数字的个数,sum为答案

int main()//进入主函数
{
    n = gi();//输入数字个数
    for (int i = 1; i <= n; i++)
    {
        int x = gi();//依次输入每个数
        sum = sum + i * x;//更新答案
    }
    printf("%d\n", sum);//输出最终答案
    return 0;//完美结束
}

Guess you like

Origin www.cnblogs.com/xsl19/p/11104942.html