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的更多相关文章

  1. Sort Colors

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  2. [LeetCode] Sort Colors 颜色排序

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  3. Leetcode 75. Sort Colors

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  4. CF444C. DZY Loves Colors[线段树 区间]

    C. DZY Loves Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. Codeforces444C DZY Loves Colors(线段树)

    题目 Source http://codeforces.com/problemset/problem/444/C Description DZY loves colors, and he enjoys ...

  6. Sort Colors [LeetCode]

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  7. 【LeetCode】Sort Colors

    Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...

  8. 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. ...

  9. LintCode Sort Colors

    For this problem we need to sort the array into three parts namely with three numbers standing for t ...

  10. LeetCode-Sort Colors

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

随机推荐

  1. k8s实战案例之运行WordPress

    1.WordPress架构 LNMP案例之基于Nginx+PHP实现WordPress博客站点,要求Nginx+PHP运⾏在同⼀个Pod的不同容器;nginx主要作用是接入站点请求,如果请求静态资源n ...

  2. Vue【原创】日历组件Calendar

    最近项目中封装了一个日历组件,用于节假日管理,支持输入默认选中的日期,选择管理日期. 效果图: calendar组件: 1 <template> 2 <div class=" ...

  3. ORM分组查询复杂查询

    记录一下自己在工作中遇到的一个问题,就是根据一个字段分类,然后还要统计这个字段不同状态的数量,这里我举个例子 假如有好多学生,这些学生来自不同的班级,现在我们要统计每一个班级中男生和女生的数量 def ...

  4. 从软件工程师角度聊聊 Kubernetes

    作为软件工程师,我们应该熟悉 K8s,尽管它有点像 DevOps,但它能让我们更好地了解幕后发生的事情,让我们与部署工作更密切相关,更有责任感.本文将从软件工程师的角度探讨 Kubernetes (K ...

  5. excel的烦恼

    Smiling & Weeping ---- 他未对我好半分,偏巧这感情疯长似野草 题目链接:https://www.matiji.net 思路:与新三进制2思路相似,转化为纯26进制,然后往 ...

  6. .NET使用quartz+topshelf实现定时执行任务调度服务

    一.项目开发 1.新建控制台应用(.NET Framework) 2.配置新项目,自行修改项目名称.位置和框架(建议使用.NET Framework4.5以上版本) 创建好的项目目录如下: 3.右键引 ...

  7. k8s添加节点报[WARNING SystemVerification]: missing optional cgroups: blkio

    环境信息: ubuntu-master01  192.1681.195.128 ubuntu-work01    192.168.195.129 k8s版本 1.25.2 背景描述:初始环境是一个ma ...

  8. C++ RAII在HotSpot VM中的重要应用

    RAII(Resource Acquisition Is Initialization),也称为"资源获取就是初始化",是C++语言的一种管理资源.避免泄漏的惯用法.C++标准保证 ...

  9. Codechef - Maximize Colours(IQ)

    题目大意   有红绿蓝三种颜色,三种颜色当中任意两个颜色混合都可以产生出一个新的颜色(然而混合产生的颜色不能与任何其它的颜色进行混合).输入三个整数,分别代表红色,绿色,蓝色的颜色个数(每次混合各消耗 ...

  10. 03-11gR2单机通过RMAN恢复到RAC(未验证)

    1.在单机上做一个完全备份,并将备份集拷贝到RAC的第一个节点上. 2.强行启动到nomount 3.恢复spfile 4.创建pfile,修改pfile,重建spfile #####修改contro ...