Atcoder Beginner Content 143 B - TAKOYAKI FESTIVAL 2019

Calendar Clock iconCalendar Clock icon

atcoder

Table of contents

# Problem

https://atcoder.jp/contests/abc143/tasks/abc143_b

# Time complexity

O(N2)O(N^2)

# Explanation

You can use a double loop as N is enough small.
Just sum up all the products of possible combinations.

# Solution

// C++ 14
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <math.h>

#define ll long long
#define Int int
#define loop(x, start, end) for(Int x = start; x < end; x++)
#define loopdown(x, start, end) for(int x = start; x > end; x--)
#define rep(n) for(int x = 0; x < n; x++)
#define span(a,x,y) a.begin()+x,a.begin()+y
#define span_all(a) a.begin(),a.end()
#define len(x) (x.size())
#define last(x) (*(x.end()-1))

using namespace std;
#define MAX_N 51

Int N;
vector<Int> D(MAX_N, 0);

Int solve() {
  Int sum = 0;
  loop(i,0,N-1) {
    loop(j,i+1,N) {
      sum += D[i] * D[j];
    }
  }
  return sum;
}

int main() {
  cin >> N;
  loop(i,0,N) cin >> D[i];
  cout << solve() << endl;
}

Remote freelancer. A web and mobile application enginner.
Traveling around the world based on East Asia.
I'm looking forward to your job offers from all over the world!

Offer jobs or contact me!

Comments