[USACO18JAN] Stamp Painting G
「USACO18JAN」 Stamp Painting G
动态规划,dp优化
题面
题目描述
Bessie has found herself in possession of an -unit long strip of canvas () and she intends to paint it. However, she has been unable to acquire paint brushes. In their place she has rubber stamps of different colors (), each stamp units wide (). Astounded by the possibilities that lie before her, she wishes to know exactly how many different paintings she could conceivably create, by stamping her stamps in some order on the canvas.
To use a stamp, it must first be aligned with exactly neighboring units on the canvas. The stamp cannot extend beyond the ends of the canvas, nor can it cover fractions of units. Once placed, the stamp paints the covered units with its color. Any given stamp may be used multiple times, once, or even never at all. But by the time Bessie is finished, every unit of canvas must have been painted at least once.
Help Bessie find the number of different paintings that she could paint, modulo . Two paintings that look identical but were painted by different sequences of stamping operations are counted as the same.
For at least 75% of the input cases, .
输入格式
The first and only line of input has three integers, , , and . It is guaranteed that .
输出格式
A single integer: the number of possible paintings, modulo .
样例输入
1 | 3 2 2 |
样例输出
1 | 6 |
题意简述
用 种颜色长度都为 的图章涂一个长度为 的画布,即每次选择一个长度为 的区间都染成一种颜色(覆盖先前的颜色),随便填涂但画布要填满,问画布最终有多少种状态。
题解
先不考虑图章大小的(假设 ),显然 ,但这其中有些是不合法的,首先有个显然的结论:因为图章可以覆盖,除了最后以下连续 个相同颜色,其他格子都可做到任意颜色。 于是那些最大连续颜色长度 的都不合法,其他都合法。
考虑动态规划,设 表示前 个格子当前连续 个相同颜色的方案数:
初始化
颜色与上次相同
颜色与上次不同
统计答案
以上做法复杂度 可拿75 pts
考虑优化dp,其实稍微观察以下转移就会发现:
直接继承 ,
可以设 ,每次更新 ,其中 可用一个队列来维护。
复杂度
CODE
1 |
|