SOLUTION INFO
C++ · main.cpp
- 작성자
- tony9402
- 공동 작성자
- 없음
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int N; cin >> N;
ll sum = 0, square_sum = 0;
for(int i=0;i<N;i++) {
ll x; cin >> x;
sum += x;
square_sum += x * x;
}
cout << (sum * sum - square_sum) / 2;
return 0;
}
SOLUTION DESCRIPTION
풀이 설명
등록된 풀이 설명이 없습니다.
SOLUTION INFO
Java · Main.java
- 작성자
- rhljh201
- 공동 작성자
- tony9402
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main {
public static void main(String[] args) {
FastReader rd = new FastReader();
int N = rd.nextInt();
int[] O = new int[N + 1];
int[] A = new int[N + 1];
for(int i=1; i<=N; i++){
O[i] = rd.nextInt();
A[i] = A[i-1] + O[i];
}
long sum = 0;
for(int i=1; i<=N; i++){
sum += A[i-1]*O[i];
}
System.out.println(sum);
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while(st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble() { return Double.parseDouble(next()); }
String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
SOLUTION DESCRIPTION
풀이 설명
N = 3이라면, (1)*2 + (1 + 2) * 3
N = 4라면, (1)*2 + (1 + 2)*3 + (1 + 2 + 3)*4
이런식으로 식을 정리할 수 있다. 따라서 구간합을 구한 다음 구간합 배열[i-1] * 값[i]를 해주면 계산할 수 있다.