Topic links
Describe
Little Ho got an array as a gift for his new year, and he liked this array very much!
After a few days of careful study, small Ho succeeded in splitting the array into segments, and each paragraph was not 0!
Now little Ho would like to know how many of these splitting methods are there?
The two split methods are treated differently when and only if the array is made up of all the locations where the arrays are broken.
Input
The first behavior of each set of inputs is a positive integer n, which indicates the length of the array
The second behaves n integers a1~an, describing the array that little Ho received
For 40% of data, meet 1<=n<=10
For 100% of data, meet 1<=n<=105, | ai|<=100
Output
For each set of inputs, the output is one line of ans, which represents the remainder of the split scheme divided by (109+7).
-----------------------------------------------------------------------------------------------------------
It is easy to think of a n^2 solution:
F[i] = Sum (F[0<=j<i]) + (pre[i]!=0), Pre[j]!=pre[i]. Where Pre[i] is prefixed and.
Because N is 10w, it must not be over.
We can first not judge whether Pre[j] is equal to pre[i], that is, to maintain an accumulative sum, and then subtract the sum of the prefix and equals pre[i] and: f[i] = The sum of the current summation and-pre[i].
Pre[i] accumulation and maintenance with map, is the complexity of LOGN.
So it's n^2 down to Nlogn.
#include <map>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;Const intMOD = 1e9+7;Const intN =100100;intSum[n];Long LongDp[n];intMain () {Long LongPre =0; Map<Long,Long>Records; intN cin>>n; for(intI=0; i<n;i++) {scanf ("%d", sum+i);if(i) sum[i]+=sum[i-1]; Dp[i]= Sum[i]? ((pre+1)%MOD):p re; Auto ITER=Records.find (Sum[i]); if(Iter==records.end ()) records[sum[i]]=Dp[i]; Else{Dp[i]= (dp[i]-iter->second+mod)%MOD; ITER->second = (Iter->second+dp[i])%MOD; } Pre= (Pre+dp[i])%MOD; } printf ("%lld\n", dp[n-1]); return 0;}
*hiho 1475-array split, DP, reduced from N^2 to Nlogn