感觉这题要想到dp挺难的。。
这个状态选得就比较巧了。。
先考虑序列上的情况。。
设d[i][j]为将前j个数切成i段的和
所以答案只要枚举段数i再乘以总长度就能构成环了orz
然后显然d[i][j]=sum(d[i-1][j-k]*k) k=1..j-i
直接转移肯定tle。。所以需要优化。。给出2种方法。。
第一种自己想的。。。
其实
sum(d[i-1][j-k]*k)
=sum(d[i-1][k]*(j-k))
=sum(j*d[i-1][k]-d[i-1][k]*k)
=j*sum(d[i-1][k])-sum(d[i-1][k]*k)
所以只要维护d[i-1][k]和d[i-1][k]*k的前缀和即可。。
第二种是别人的。。。
sum(d[i-1][j-k]*k)
=sum(d[i-1][j-1-k]*k)[k=1..j-2]+sum(d[i-1][j-k])[k=1..j-1]
=d[i][j-1]+sum(d[i-1][k])[k=1..j-1]
然后只需要维护d[i-1][k]的前缀和就好了。。orz
不管哪种方法都挺巧的。。值得学习。。毕竟这个方程还挺常见的。。。
然后最后记数的时候。。由于把序列旋转成环。。对每个切好的i个片段来说,可以由i个状态转到当前状态。。所以在计数的时候记得除以i。。当然不是直接除。。要用逆元。。。
Circular Coloring
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)
Total Submission(s): 44 Accepted Submission(s): 15
Problem Description
(n+m) balls arranged in a circle.
The balls are numbered with
0,1,…,(n+m−1) where the ball
i and the ball
(i+1)mod(n+m) are adjacent.
Bobo would like to color
n of his balls black and
m of his balls white.
Bobo groups adjacent balls with same colors, and he determines the weight of the coloring as the product of the lengths of groups.
He would like to know the sum of the weight of the possible colorings, modulo
(109+7).
Input
The input consists of several test cases and is terminated by end-of-file.
Each test case contains two integers
n and
m.
Output
For each test case, print an integer which denotes the result.
## Constraint
*
1≤n,m≤5000
* The number of test cases does not exceed
5000.
Sample Input
1 2 2 3 5000 5000
Sample Output
Hint
Source
CCPC2018-湖南全国邀请赛-重现赛(感谢湘潭大学)
Recommend
liuyiding | We have carefully selected several similar problems for you: 6296 6295 6294 6293 6292
Statistic |
Submit |
Discuss |
Note