ABC177 C - Sum of product of pairs から学んだ
4566 ワード
無心になったが、とりあえず
サンプルも読む。
なるほど。
例えば** 1 2 3 4 が与えられたとすると
**1x2+(1+2)x3+(1+2+3)x4とならないか?
後は表現方法が問題だ。
だがしかし、tle & wa
ans.py
N = int(input())
A = list(map(int,input().split()))
score = A[0]*A[1]
for i in range(2,N): #計算量 O(N)
score += sum(A[:i])*A[i] #計算量 O(N)...合算してほぼ O(N^2)
score %= 7+10**9
print(score)
配列の sum の計算量は配列長さに依存する。
なので最終的には、それだけで O(N) となりうる。
っということで書き換えたら、通った。
ans_r1.py
N = int(input())
A = list(map(int,input().split()))
X = sum(A)
score = 0
for i in range(N-1,0,-1):
score += (X-A[i])*A[i]
score %= 7+10**9
X -= A[i]
print(score)
Author And Source
この問題について(ABC177 C - Sum of product of pairs から学んだ), 我々は、より多くの情報をここで見つけました https://qiita.com/AKpirion/items/4162119826d348c8aedf著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .