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. 《流畅的python》— 列表推导与生成器表达式

    列表推导是构建列表(list)的快捷方式,而生成器表达式则可以用来创建其他任何类型的序列.如果你的代码里并不经常使用它们,那么很可能你错过了许多写出可读性更好且更高效的代码的机会. 很多Python ...

  2. SqlServer表添加字段

    IF NOT EXISTS (SELECT * FROM syscolumns WHERE id=object_id('表名') AND name='字段名') ALTER TABLE 表名 ADD ...

  3. mybatis数据库字段自动填充

    背景描述 目前,大多数项目的数据库设计,都会添加一些公共字段,比如version(版本号).deleted(逻辑删除标识).create_time.update_time.create_by.upda ...

  4. ArcGIS地图投影与坐标系转换的方法

      本文介绍在ArcMap软件中,对矢量图层或栅格图层进行投影(即将地理坐标系转为投影坐标系)的原理与操作方法.   首先,地理坐标系与投影坐标系最简单的区别就是,地理坐标系用经度.纬度作为空间衡量指 ...

  5. 圆柱坐标系(Cylindrical Coordinate System)

    参考:维基百科 圆柱坐标系(英语:cylindrical coordinate system)是一种三维坐标系统.它是二维极坐标系往 z-轴的延伸.添加的第三个坐标 \(z\) 专门用来表示 P 点离 ...

  6. 其它——Redis与Mysql双写一致性方案解析

    文章目录 一 前言 二 一致性方案 三 先更新数据库,再更新缓存 四 先删缓存,再更新数据库 五 先更新数据库,再删缓存 一 前言 首先,缓存由于其高并发和高性能的特性,已经在项目中被广泛使用.在读取 ...

  7. 前端三件套系例之JQuery——JQuery动画效果、JQuery插件、

    文章目录 1 JQuery动画效果 1. 基本效果 2. 滑动效果 3 淡入淡出效果 4 自定义动画 5 动画控制 6 设置 7 事件 7-1 常用事件 7-2 事件绑定 7-3 移除事件 7-4 阻 ...

  8. Linux常用命令大全 Linux Commands Line - v1.0

    The most complete and updated list of commands on linux by LinuxGuide.it - over 350 commands!       ...

  9. 【译】A unit of profiling makes the allocations go away

    在 Visual Studio 17.8 Preview 2 中,我们更新了单元测试分析,允许你在性能分析器中使用任何可用的工具--而不仅仅是仪表工具.有了这个更改,可以很容易地快速分析孤立的小工作单 ...

  10. 系统RAM几乎爆满与解决方法

    先说一遍,遇事不决就重启! 在电脑长时间运转下某些无良应用程序会产生大量的临时文件(目前我怀疑是有道云笔记) 最终导致系统爆炸 附图 在这种情况下,我下载了RAM实时监测我内存占用情况 结果发现pag ...