[ABC279G] At Most 2 Colors
Problem Statement
There is a grid with $1 \times N$ squares, numbered $1,2,\dots,N$ from left to right.
Takahashi prepared paints of $C$ colors and painted each square in one of the $C$ colors.
Then, there were at most two colors in any consecutive $K$ squares.
Formally, for every integer $i$ such that $1 \le i \le N-K+1$, there were at most two colors in squares $i,i+1,\dots,i+K-1$.
In how many ways could Takahashi paint the squares?
Since this number can be enormous, find it modulo $998244353$.
Constraints
- All values in the input are integers.
- $2 \le K \le N \le 10^6$
- $1 \le C \le 10^9$
Input
The input is given from Standard Input in the following format:
$N$ $K$ $C$
Output
Print the answer as an integer.
Sample Input 1
3 3 3
Sample Output 1
21
In this input, we have a $1 \times 3$ grid.
Among the $27$ ways to paint the squares, there are $6$ ways to paint all squares in different colors, and the remaining $21$ ways are such that there are at most two colors in any consecutive three squares.
Sample Input 2
10 5 2
Sample Output 2
1024
Since $C=2$, no matter how the squares are painted, there are at most two colors in any consecutive $K$ squares.
Sample Input 3
998 244 353
Sample Output 3
952364159
Print the number modulo $998244353$.
定义 \(dp_{i,j}\) 为前 \(i\) 个位置,满足 \([j,i]\) 颜色全部相等,但是 \(j-1\) 的颜色不等的方案数。
为什么会想到这个定义呢?因为我们会发现,当且仅当倒数 \(k-1\) 个数是相等的,那么我们在下一个位置才可以随便选数字。否则的话,下一个位置如果还要和上一个位置不一样,只有一种选择。
那么对于 \(i>j\),所有的 \(dp_{i,j}\) 都是一样的,因为往后只能填和前一位用的同一个颜色。\(dp_i\) 表示的是前 \(i\) 位有多少种填法。
然后如果结尾的相同的颜色个数大于等于 \(k-1\),那么有 \(c-1\) 种填法。否否则,就只有一种填法。这里可以拿前缀和优化。
还有一个特殊情况,就是把 \([2,i-1]\) 全部填成一种颜色,此时有 \(c*(c-1)\) 种情况
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5,P=998244353;
int n,k,c,s[N];
int main()
{
scanf("%d%d%d",&n,&k,&c);
for(int i=2,dp;i<=n;i++)
{
dp=(s[i-1]+(c-2LL)*s[max(i-k+1,0)]%P+P+c*(c-1LL))%P;;
s[i]=(s[i-1]+dp)%P;
// printf("%d ",dp);
}
printf("%d",(s[n]+c)%P);
}
[ABC279G] At Most 2 Colors的更多相关文章
- Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- [LeetCode] Sort Colors 颜色排序
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- Leetcode 75. Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- CF444C. DZY Loves Colors[线段树 区间]
C. DZY Loves Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces444C DZY Loves Colors(线段树)
题目 Source http://codeforces.com/problemset/problem/444/C Description DZY loves colors, and he enjoys ...
- Sort Colors [LeetCode]
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- 【LeetCode】Sort Colors
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- PAT (Advanced Level) Practise:1027. Colors in Mars
[题目链接] People in Mars represent the colors in their computers in a similar way as the Earth people. ...
- LintCode Sort Colors
For this problem we need to sort the array into three parts namely with three numbers standing for t ...
- LeetCode-Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
随机推荐
- 产品代码都给你看了,可别再说不会DDD(三):战略设计
这是一个讲解DDD落地的文章系列,作者是<实现领域驱动设计>的译者滕云.本文章系列以一个真实的并已成功上线的软件项目--码如云(https://www.mryqr.com)为例,系统性地讲 ...
- java与es8实战之二:实战前的准备工作
欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇概览 本篇是<java与es8实战>系 ...
- Linux 内核设备驱动程序的IO寄存器访问 (上)
Linux 内核提供了一套可缓存的设备 IO 寄存器访问机制,即 regmap.regmap 机制支持以统一的接口,访问多种不同类型的设备 IO 寄存器,如内存映射的设备 IO 寄存器,和需要通过 I ...
- mac安装mysql8.0
1.进入下载页 历史版本:https://downloads.mysql.com/archives/community/ 最新版本:https://dev.mysql.com/downloads/my ...
- 分享一个 SpringBoot + Redis 实现「查找附近的人」的小技巧
前言 SpringDataRedis提供了十分简单的地理位置定位的功能,今天我就用一小段代码告诉大家如何实现. 正文 1.引入依赖 <dependency> <groupId> ...
- Goobye, cnblogs
转 typecho 了,个人网站的客制化程度当然不是 cnblogs 能比得上的. <cirnovsky.cf>
- 「ceoi 2009」harbingers
link. 朴素 dp 大约就是 \(f_x=f_y+v_x\times(d_x-d_y)+s_x\),\(y\) 是 \(x\) 的祖先.这个式子可以斜率优化,在以 \(d_y\) 为横坐标,\(f ...
- 文心一言 VS 讯飞星火 VS chatgpt (103)-- 算法导论10.1 1题
一.用go语言,仿照图 10-1,画图表示依次执行操作 PUSH(S,4).PUSH(S,1).PUSH(S,3).POP(S).PUSH(S,8)和 POP(S)每一步的结果,栈 S初始为空,存储于 ...
- Informix 4gl错误代码信息和更正
(一)Informix信息和更正 出版日期:6 1996 年 年 1 11 月 0 成功. 操作成功.当 SQL 语句成功地执行时,数据库服务器把这个 SQLCODE 值返回给应用程序. 100 没有 ...
- 10.7 多校联测 Day? 总结
打了一场从头到尾稀里糊涂的比赛. 脑子完全不转. 开考看 T1,一开始连逆序对都没看出来.想着把不合法的点两两连边黑白染色(也是够离谱)然后也不知道干了啥(似乎很困),反正一个小时过去了. 就只看出来 ...