#include <cstdio> #include <vector> using namespace std; vector<int> smallnums; vector<int> bignums; vector<int> opennums; int countof[20000003]; inline int number_idx(int number) { return number + 10000000; } int main() { int n, a; scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d", &a); smallnums.push_back(a); } for(int i = 0; i < n; ++i) { for (int j = i; j < n; ++j) { int sum = 0; for (int k = i; k <= j; ++k) { sum += smallnums[k]; } bignums.push_back(sum); } } /* Too smart for 12:00 pm for (int i = 0; i < n; ++i) { scanf("%d", &a); smallnums.push_back(a); int opennums_sofar = opennums.size(); for (int j = 0; j < opennums_sofar; ++j) { opennums[j] += a; bignums.push_back(bignums[j] + a); } opennums.push_back(a); printf("AFTER %d: %d seqs\n", i, bignums.size()); } */ int bnums_count = bignums.size(); // printf("%d\n", bnums_count); // for (int i = 0; i < bnums_count; ++i) { // printf("%d ", bignums[i]); // } // printf("\n"); for (int i = 0; i < bnums_count; ++i) { countof[number_idx(bignums[i])]++; } int totalres = 0; for (int i = 0; i < bnums_count; ++i) { for (int j = i+1; j < bnums_count; ++j) { int res = bignums[i] + bignums[j]; // printf("%d + %d = %d\n", bignums[i], bignums[j], res); // printf("Need %d: found %d\n", -res, countof[number_idx(-res)]); if (bignums[i] == -res) { totalres--; } if (bignums[j] == -res) { totalres--; } totalres += countof[number_idx(-res)]; } } totalres /= 3; // each triple was found 3 times printf("%d\n", totalres); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | #include <cstdio> #include <vector> using namespace std; vector<int> smallnums; vector<int> bignums; vector<int> opennums; int countof[20000003]; inline int number_idx(int number) { return number + 10000000; } int main() { int n, a; scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d", &a); smallnums.push_back(a); } for(int i = 0; i < n; ++i) { for (int j = i; j < n; ++j) { int sum = 0; for (int k = i; k <= j; ++k) { sum += smallnums[k]; } bignums.push_back(sum); } } /* Too smart for 12:00 pm for (int i = 0; i < n; ++i) { scanf("%d", &a); smallnums.push_back(a); int opennums_sofar = opennums.size(); for (int j = 0; j < opennums_sofar; ++j) { opennums[j] += a; bignums.push_back(bignums[j] + a); } opennums.push_back(a); printf("AFTER %d: %d seqs\n", i, bignums.size()); } */ int bnums_count = bignums.size(); // printf("%d\n", bnums_count); // for (int i = 0; i < bnums_count; ++i) { // printf("%d ", bignums[i]); // } // printf("\n"); for (int i = 0; i < bnums_count; ++i) { countof[number_idx(bignums[i])]++; } int totalres = 0; for (int i = 0; i < bnums_count; ++i) { for (int j = i+1; j < bnums_count; ++j) { int res = bignums[i] + bignums[j]; // printf("%d + %d = %d\n", bignums[i], bignums[j], res); // printf("Need %d: found %d\n", -res, countof[number_idx(-res)]); if (bignums[i] == -res) { totalres--; } if (bignums[j] == -res) { totalres--; } totalres += countof[number_idx(-res)]; } } totalres /= 3; // each triple was found 3 times printf("%d\n", totalres); return 0; } |