Description
You are given a sequence A consisting of N integers (not to be confused with the sequence from the
previous task). We will call the i
th
sequence element good if it equals the sum of some three elements
in positions strictly smaller than i (an element can be used more than once in the sum).
How many good elements does the sequence contain?
Input
The first line of input contains the positive integer N (1 ≤ N ≤ 5000), the length of the sequence A.
The second line of input contains N space-separated integers representing the sequence A (-100 000 ≤
A
i
≤ 100 000).
Output
The first and only line of output must contain the number of good elements in the sequence.
Sample Input21 361 2 3 5 7 103-1 2 0Sample Output141
簡單動態規劃思想
#include<stdio.h>#include<string.h>#include<iostream>using namespace std;int a[6000];bool p[410000]; //進行的是其餘兩個的儲存int main(){ int n; while(scanf("%d",&n)!=EOF){ for(int i=0;i<n;i++) scanf("%d",&a[i]); memset(p,0,sizeof(p)); int ans=0; for(int i=0;i<n;i++){ for(int j=0;j<i;j++)if(p[ a[i]-a[j]+200000] == 1) {ans++;break;} //a[i]作為目標值 p[t]代表的是 t(有兩個數相加)是否存在 for(int j=0;j<=i;j++)p[ a[i]+a[j]+200000 ]=1; //a[i]作為其中的一個元素 } cout<<ans<<endl; } return 0; }