NC223710. SumandProduct
描述
输入描述
The input has a single integer n (2 ≤ n ≤ 2 · 105 ) on the first line. The second line has n positive integers giving thenumbers in the arithmetic practice book ordered by the pages. None of these integers are larger than 109 .
输出描述
Output a single integer, the number of ways for Marguerite to choose a range of at least two consecutive pages so that Sarah’s answer matches Patricia’s.
示例1
输入:
5 2 2 1 2 3
输出:
2
示例2
输入:
8 1 2 4 1 1 2 5 1
输出:
4
示例3
输入:
4 5 6 7 8
输出:
0
C++ 解法, 执行用时: 68ms, 内存消耗: 1952K, 提交时间: 2021-08-19 13:14:22
#include <cmath> #include <cstdio> #include <algorithm> const int maxn = 2E+5 + 5; int n, a[maxn], lst[maxn]; int main() { scanf("%d", &n); for(int i = 1, LST = 0; i <= n; ++i) { scanf("%d", &a[i]), lst[i] = LST; if(a[i] != 1) LST = i; } int ans = 0; for(int i = 1; i <= n; ++i) { int u = i; __int128 prod = 1, sum = 0; while(u) { prod *= a[u], sum += a[u]; if(prod >= 1E+14) break; int cnt = u - lst[u] - 1; if(sum <= prod && sum + cnt >= prod) ++ans; sum += cnt, u = lst[u]; } } printf("%d\n", ans - n); }