luogu4187 [USACO18JAN]Stamp Painting (dp)

可以发现,只要存在连续k个相同的,这个情况就一定是合法情况

然而这个不太好算,我们算不存在k个相同的,然后用$m^n$把它减掉

设f[i]为前i个,没有连续k个的

显然$f[i]=m^i ,i<K$

然后我们现在想把f[i]转移过来,只要取f[i-k+1]..f[i-1]的所有情况,然后在每个的后面都涂上与这种情况的最后一个颜色不相同的颜色就可以了。容(bu)易(hui)证明这样做是不重不漏的

所以$f[i]=(M-1)\sum_{j=i-K+1}^{i-1}f[j]$

 1 #include<bits/stdc++.h>
 2 #define ll long long
 3 #define pa pair<int,int>
 4 using namespace std;
 5 const int maxn=1000010,mod=1e9+7;
 6
 7 inline ll rd(){
 8     ll x=0;char c=getchar();
 9     while(c<‘0‘||c>‘9‘) c=getchar();
10     while(c>=‘0‘&&c<=‘9‘) x=x*10+c-‘0‘,c=getchar();
11     return x;
12 }
13
14 ll N,M,K;
15 ll f[maxn];
16 ll ans=1,sum;
17
18 int main(){
19     int i,j,k;
20     N=rd(),M=rd(),K=rd();
21     for(i=1;i<=N;i++){
22         ans=(ans*M)%mod;
23         if(i<K) f[i]=ans,sum=(sum+ans)%mod;
24     }
25     for(i=K;i<=N;i++){
26         f[i]=(sum*(M-1))%mod;
27         sum=(sum+f[i]-f[i-K+1])%mod;
28     }
29     printf("%d\n",((ans-f[N])%mod+mod)%mod);
30 }

原文地址:https://www.cnblogs.com/Ressed/p/9661855.html

时间: 2025-01-17 05:55:33

luogu4187 [USACO18JAN]Stamp Painting (dp)的相关文章

[USACO18JAN]Stamp Painting

题目 考虑正难则反,拿总方案数\(m^n\)减掉不合法的方案 由于我们最后一次涂了一段长度为\(k\)的连续颜色段,所以合法的方案有一段长度至少为\(k\)的颜色相同段 不合法的方案一定没有,于是我们求一下有多个颜色序列没有长度大于等于\(k\)的颜色相同段就好了 显然我们强制往后加一段长度小于\(k\)的段就行了 于是有\(f_i=(m-1)\sum_{j=i-k+1}^{i-1}f_j\),转移的时候维护一下前缀和就可以了 代码 #include<bits/stdc++.h> #defin

[ACM] ZOJ 3725 Painting Storages (DP计数+组合)

Painting Storages Time Limit: 2 Seconds      Memory Limit: 65536 KB There is a straight highway with N storages alongside it labeled by 1,2,3,...,N. Bob asks you to paint all storages with two colors: red and blue. Each storage will be painted with e

(dp)CodeForces - 300D Painting Square

Vasily the bear has got a large square white table of n rows and n columns. The table has got a black border around this table.  The example of the initial table at n = 5. Vasily the bear wants to paint his square table in exactly k moves. Each move

Codeforces Round #233 (Div. 2)D. Painting The Wall 概率DP

                                                                               D. Painting The Wall User ainta decided to paint a wall. The wall consists of n2 tiles, that are arranged in an n × n table. Some tiles are painted, and the others are not

cf 398B. Painting The Wall【期望dp】

传送门:http://codeforces.com/problemset/problem/398/B Description: User ainta decided to paint a wall. The wall consists of n2 tiles, that are arranged in an n?×?n table. Some tiles are painted, and the others are not. As he wants to paint it beautifull

!codeforces 399D Painting The Wall-概率dp

题意:有一个n*n的墙,现在小明来刷墙,如果每一行每一列都至少有一个格子刷过了就停止工作,否则每次随机选一个格子,如果刷过了就不刷如果没刷过就刷,然后休息一分钟,求停止工作时时间的数学期望(开始之前已经有m个格子刷过了) 分析:概率dp 状态:dp[i][j]表示还有i行j列没刷,则它能转移到的状态是dp[i][j],dp[i-1][j-1],dp[i][j-1],dp[i-1][j-1] 转移:dp[i][j]=dp[i][j]*(n-i)(n-j)/n^2+dp[i-1][j]*(i*(n-

codeforces 448C C. Painting Fence(分治+dp)

题目链接: codeforces 448C 题目大意: 给出n个杆子,每个杆子有一个长度,每次可以刷一行或一列,问最少刷多少次可以将整个墙刷成黄色. 题目分析: 首先我们能够想到,如果横着刷,为了得到最优解,当前刷的位置的下面也必须横着刷,然后对于每种情况都可以通过n次竖着刷得到整个黄色的墙. 所以我们采取分治的策略进行动态规划,也就是对于每个状态划分为两种情况讨论,如果要刷横向的话,最矮要刷到最矮的柱子的高度才可能得到比竖着刷优的解,然后就变成了多个具有相同性质的规模更小的墙,然后我们可以采取

poj - 1691 - Painting A Board(状态压缩dp)

题意:N(1 <= N <= 15)个矩形,每个矩形要涂上指定的颜色C(1 <= C <= 20),如果给一个矩形涂色,那么与它相邻的上方矩形必须已经涂色,问最少要取几次画笔. 题目链接:http://poj.org/problem?id=1691 -->>状态:dp[S][color] 表示达到状态 S 且最后一次涂色为 color 时的最小取画笔数 状态转移方程:dp[S][color] = min(dp[S][color], dp[sub][i]); 或者 dp[

CodeForce - 1187 E. Tree Painting (换根dp)

You are given a tree (an undirected connected acyclic graph) consisting of nn vertices. You are playing a game on this tree. Initially all vertices are white. On the first turn of the game you choose one vertex and paint it black. Then on each turn y