首先我们对每天的股票价格做差分,得到差分序列$a[1], a[2] ... a[k - 1]$
则对已一个给定的序列,满足这个差分序列的答案共有$n - \sum_{i = 1} ^{k - 1} a[i]$
故总的答案$ans = \sum (n - \sum_{i = 1} ^{k - 1} a[i])$
把n提出来,有$ans = n * m ^ {k - 1} - \sum (\sum_{i = 1} ^{k - 1} a[i])$
我们来看这个东西$A = \sum (\sum_{i = 1} ^{k - 1} a[i])$会发现里面的每个$a[i]$是独立的
故对于每个$a[i]$,它对于$T$的贡献是$m ^ {k - 2} * \sum_{a[i] = 1}^{m} = m ^ {k - 2} * \frac{m (m + 1)} {2}$
所以$ans = n * m ^ {k - 1} - (k - 1) * \frac{m^{k - 1} * (m + 1)} {2}$
1 /************************************************************** 2 Problem: 3142 3 User: rausen 4 Language: Python 5 Result: Accepted 6 Time:80 ms 7 Memory:6360 kb 8 ****************************************************************/ 9 10 def pow(x, y) : 11 res = 1 12 while (y > 0) : 13 if (y & 1) : res = res * x % p 14 x = x * x % p 15 y = y >> 1 16 return res 17 18 n, k, m, p = [int(i) for i in raw_input().split()] 19 print pow(m, k - 2) * (m * n - (k - 1) * m * (m + 1) / 2) % p
By Xs酱~ 转载请说明
博客地址:http://www.cnblogs.com/rausen