[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 ...
随机推荐
- 面霸的自我修养:synchronized专题
王有志,一个分享硬核Java技术的互金摸鱼侠 加入Java人的提桶跑路群:共同富裕的Java人 今天是<面霸的自我修养>的第3弹,内容是Java并发编程中至关重要的关键字synchroni ...
- Python怎么通过url下载网络文件到本地
以下代码演示Python怎么从网络下载一个文件至本地并保存在当前文件夹download import os import requests from urllib.parse import urlpa ...
- 美团面试拷打:ConcurrentHashMap 为何不能插入 null?HashMap 为何可以?
周末的时候,有一位小伙伴提了一些关于 ConcurrentHashMap 的问题,都是他最近面试遇到的.原提问如下: 整个提问看着非常复杂,其实归纳来说就是两个问题: ConcurrentHashMa ...
- Java单元测试及常用语句
1 前言 编写Java单元测试用例,即把一段复杂的代码拆解成一系列简单的单元测试用例,并且无需启动服务,在短时间内测试代码中的处理逻辑.写好Java单元测试用例,其实就是把"复杂问题简单化, ...
- WPF动画入门教程
Windows Presentation Foundation (WPF)是一种用于创建Windows客户端应用程序的UI框架.它让我们能够创建丰富的图形界面,包括各种各样的动画效果.接下来,我们将介 ...
- vue2实现饼图Pie组件封装
实现如下效果: 效果展示:https://code.juejin.cn/pen/7226656439941955644 如果不会请移步到官网的栗子,请点击查看 直接给大家上代码: 整体代码片段 1 & ...
- Shell 文件或目录操作符(-e、-d、-f、-r、-w、-x)
操作符 操作符 含义-e 判断对象是否存在(Exist),若存在则结果为真-d 判断对象是否为目录(Directory),是则为真-f 判断对象是否为一般文件(File),是则为真-r 判断对象是否有 ...
- 如何用ppt打印9张一面,并且去除边距?
如何用ppt打印9张一面,并且去除边距? 方法其实很简单,答主不要在ppt软件的打印选项里设置[每页打印9张幻灯片],而是使用默认的[每页打印1张幻灯片]. 然后去[打印机属性]里设置,我是 ...
- 调节LED的亮度
虽然Devices Plus已经介绍过Arduino的多种应用和作品示例,但是了解相关基础知识对于任何项目的构建仍然是非常重要的!这次,我们将为您介绍Arduino电子制作的基础知识.此次的分享嘉宾是 ...
- Visible Lattice Points 题解
Visible Lattice Points 题目大意 给定一个 \(N×N×N\) 的由若干点组成的立方体,点的坐标从 \((0,0,0)\) 到 \((N,N,N)\),求从点 \((0,0,0) ...