[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 ...
随机推荐
- 深入了解Elasticsearch搜索引擎篇:倒排索引、架构设计与优化策略
什么是倒排索引?有什么好处? 倒排索引是一种用于快速检索的数据结构,常用于搜索引擎和数据库中.与传统的正排索引不同,倒排索引是根据关键词来建立索引,而不是根据文档ID. 倒排索引的建立过程如下:首先, ...
- 【升职加薪秘籍】我在服务监控方面的实践(7)-业务维度的redis监控
大家好,我是蓝胖子,关于性能分析的视频和文章我也大大小小出了有一二十篇了,算是已经有了一个系列,之前的代码已经上传到github.com/HobbyBear/performance-analyze,接 ...
- Flutter系列文章-Flutter应用优化
当涉及到优化 Flutter 应用时,考虑性能.UI 渲染和内存管理是至关重要的.在本篇文章中,我们将通过实例深入讨论这些主题,展示如何通过优化技巧改进你的 Flutter 应用. 代码性能优化 1. ...
- defined('BASEPATH') OR exit('No direct script access allowed'); 的作用
起到保护.php文件的作用, 如果直接访问此php文件会得到"不允许直接访问脚本"的错误提示 如果你是用ci框架或者其他的什么, 就建议加上, 如果你怕别人恶意攻击你的话
- spark修改控制台输出日志级别
spark修改控制台输出日志级别 修改conf/log4j.properties cd $SPARK_HOME/conf cp log4j.properties.template ./log4j.pr ...
- 《流畅的Python》 读书笔记 230926
写在最前面的话 缘由 关于Python的资料市面上非常多,好的其实并不太多. 个人认为,基础的,下面的都还算可以 B站小甲鱼 黑马的视频 刘江的博客 廖雪峰的Python课程 进阶的更少,<流畅 ...
- 【FAQ】关于获取运动健康数据的常见问题及解答
目录 一.Health Kit健康数据采样, 原子采样数据问题 二.Health Kit查询历史数据查询数据和返回数据不一致 三.Health Kit关于获取历史数据问题 四.调用Health Kit ...
- oracle优化-分页查询的错误认识
对于分页查询,上一篇文章总结了实现分页查询的办法.同时给出等价写法,另外在执行计划角度验证SQL的等价性https://www.cnblogs.com/handhead/p/13856505.html ...
- Meteors 题解
Meteors 蒟蒻初学整体二分,写一篇题解记录一下思考与看法. 题目大意 在一个环形的轨道上分别着若干国家的空间站,在接下来的一段时间内会出现若干次陨石,每次出现在环形的某一段轨道,每个国家都想收集 ...
- Facade 外观模式简介与 C# 示例【结构型5】【设计模式来了_10】
〇.简介 1.什么是外观模式? 一句话解释: 将一系列需要一起进行的操作,封装到一个类中,通过对某一个方法的调用,自动完成一系列操作. 外观模式是一种简单而又实用的设计模式,它的目的是提供一个统一 ...