标签:html name solution ssi review 相同 show using ret
Input第1行:一个数N,表示序列的长度(1 <= N <= 100000)
第2 - N + 1行:序列中的元素(1 <= ai i
<= 100000)Output输出a的不同子序列的数量Mod 10^9 + 7。Sample Input
4
1
2
3
2
Sample Output
13
Solution:
设以第i位为结尾且一定选的方案数
一片题解:
https://www.cnblogs.com/acerkoo/p/11621037.html
#include <cstdio> using namespace std; const int maxn = 1e5+10; const int mod = 1e9+7; int last[maxn]; int dp[maxn], pre[maxn]; int n, a[maxn]; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", a+i); // 思路一: // pre[0] = 1; // for (int i = 1; i <= n; ++i) { // if(last[a[i]] == 0) dp[i] = pre[i-1]; // else dp[i] = (pre[i-1]-pre[last[a[i]]-1]+mod)%mod; // pre[i] = (pre[i-1] + dp[i]) % mod; // last[a[i]] = i; // } // printf("%d\n", (pre[n]-1+mod)%mod); // 思路二: dp[1] = 1, last[a[1]] = 1; for (int i = 2; i <= n; ++i) { if(last[a[i]] == 0) dp[i] = (dp[i-1]*2%mod+1)%mod; else dp[i] = (dp[i-1]*2%mod-dp[last[a[i]]-1]+mod)%mod; last[a[i]] = i; } printf("%d\n", dp[n]); return 0; }
标签:html name solution ssi review 相同 show using ret
原文地址:https://www.cnblogs.com/zhangbuang/p/11624259.html