F2. Wrong Answer on test 233 (Hard Version)

Your program fails again. This time it gets "Wrong answer on test 233"

.

This is the harder version of the problem. In this version, 1≤n≤2⋅105. You can hack this problem if you locked it. But you can hack the previous problem only if you locked both problems.

The problem is to finish n one-choice-questions. Each of the questions contains k options, and only one of them is correct. The answer to the i-th question is hi, and if your answer of the question i is hi, you earn 1 point, otherwise, you earn 0 points for this question. The values h1,h2,…,hn are known to you in this problem.

However, you have a mistake in your program. It moves the answer clockwise! Consider all the n answers are written in a circle. Due to the mistake in your program, they are shifted by one cyclically.

Formally, the mistake moves the answer for the question i to the question imodn+1. So it moves the answer for the question 1 to question 2, the answer for the question 2 to the question 3, ..., the answer for the question n to the question 1.

We call all the n answers together an answer suit. There are kn possible answer suits in total.

You're wondering, how many answer suits satisfy the following condition: after moving clockwise by 1, the total number of points of the new answer suit is strictly larger than the number of points of the old one. You need to find the answer modulo 998244353.

For example, if n=5, and your answer suit is a=[1,2,3,4,5], it will submitted as a′=[5,1,2,3,4] because of a mistake. If the correct answer suit is h=[5,2,2,3,4], the answer suit a earns 1 point and the answer suite a′ earns 4 points. Since 4>1, the answer suit a=[1,2,3,4,5] should be counted.

Input

The first line contains two integers n, k (1≤n≤2⋅105, 1≤k≤109) — the number of questions and the number of possible answers to each question.

The following line contains n integers h1,h2,…,hn, (1≤hi≤k) — answers to the questions.

Output

Output one integer: the number of answers suits satisfying the given condition, modulo 998244353.

Examples

input

3 3

1 3 1

output

9

input

5 5

1 1 4 2 2

output

1000

input

6 2

1 1 2 2 1 1

output

16

Note

For the first example, valid answer suits are [2,1,1],[2,1,2],[2,1,3],[3,1,1],[3,1,2],[3,1,3],[3,2,1],[3,2,2],[3,2,3].

题意

现在有n道题,每道题有k个答案,但是你现在犯傻了,把第一题的答案交到了第二题,第二题交到了第3题,第k题交到了第(k%n)+1题的位置上去。

现在想知道,有多少种填答案的方案,可以使得交换后的正确数量多于交换前的正确数量。

题解

数据范围小的话,dp[i][j]表示现在考虑到了第i题,交换后比交换前多得j分。

那么如果h[i]==h[i+1]的话,dp[i][j]=dp[i-1][j],因为无论如何填什么正确得个数都不会变。

其他情况 dp[i][j] = dp[i-1][j+1]+dp[i-1][j-1]+(k-2)dp[i-1][j],有一种情况是之前对了,转换后错了;之前错了,转换后对了;其他k-2种答案都保持不变。


hard version我们要反着做,假设我们知道最后转换后和转换前分数一样得方案数为ans的话,那么k^n-ans表示的是转换后得分发生改变的方案数。

又因为转换前分数高和转换后分数高的方案数是一样的,因为对称,所以最后答案一定是 (k^n-ans)/2

那么这个ans怎么做呢,假设现在h[i]!=h[i+1]的个数为num个,因为相同的话没有意义,因为填什么都无所谓

我们枚举+1的位置有多少个,C(num,i);同样的-1也得i个C(num-i,i),其他num-2i个位置有k-2种选择(k-2)(num-2i),剩下n-num个位置都有k个选择k(n-num)。

那么i个+1位置的方案数其实就是C(num,i)C(num-i,i)(k-2)(num-2i)k(n-num),最后用所有的方案数减去他再除以2就完事。

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2005;
const int mod = 998244353;
int h[maxn];
long long dp[maxn][maxn*2],base=2003,k,n;
int main(){
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
scanf("%d",&h[i]);
if(k==1){
cout<<"0"<<endl;
return 0;
}
dp[0][base]=1;
for(int i=1;i<=n;i++){
for(int j=base-2000;j<=base+2000;j++){
if(h[i]==h[i%n+1]){
dp[i][j]=dp[i-1][j]*k%mod;
}else{
dp[i][j]=(dp[i-1][j+1]+dp[i-1][j-1]+dp[i-1][j]*(k-2))%mod;
}
}
}
long long ans = 0;
for(int i=1;i<=n;i++){
ans=(ans+dp[n][base+i])%mod;
}
cout<<ans<<endl;
} #include<bits/stdc++.h>
using namespace std; const long long mod = 998244353;
const int maxn = 2e5+7;
int n,k,h[maxn];
long long powmod(long long a,long long b){
if(b==0)return 1;
return b%2==0?powmod(a*a%mod,b/2):powmod(a*a%mod,b/2)*a%mod;
}
long long fac[maxn],inv[maxn];
long long C(int a,int b){
if(b<0||b>n)return 0;
return (fac[a]*inv[b]%mod)*inv[a-b]%mod;
}
int main(){
fac[0]=inv[0]=1;
for(int i=1;i<maxn;i++){
fac[i]=i*fac[i-1]%mod;
inv[i]=powmod(i,mod-2)*inv[i-1]%mod;
}
cin>>n>>k;
if(k==1){
cout<<"0"<<endl;
return 0;
}
for(int i=0;i<n;i++)
cin>>h[i];
int num = 0;
h[n]=h[0];
for(int i=0;i<n;i++){
if(h[i]!=h[i+1])num++;
}
long long ans = 0;
for(int i=0;i*2<=num;i++){
long long tmp = C(num,i)*C(num-i,i)%mod*powmod(k-2,num-2*i)%mod*powmod(k,n-num);
ans=(ans+tmp)%mod;
}
cout<<((powmod(k,n)-ans+mod)*inv[2])%mod<<endl;
}

Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) F2. Wrong Answer on test 233 (Hard Version) dp 数学的更多相关文章

  1. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3

    A,有多个线段,求一条最短的线段长度,能过覆盖到所又线段,例如(2,4)和(5,6) 那么我们需要4 5连起来,长度为1,例如(2,10)(3,11),用(3,10) 思路:我们想一下如果题目说的是最 ...

  2. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) E. Arson In Berland Forest 二分 前缀和

    E. Arson In Berland Forest The Berland Forest can be represented as an infinite cell plane. Every ce ...

  3. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) D2. Optimal Subsequences (Hard Version) 数据结构 贪心

    D2. Optimal Subsequences (Hard Version) This is the harder version of the problem. In this version, ...

  4. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) C. Messy 构造

    C. Messy You are fed up with your messy room, so you decided to clean it up. Your room is a bracket ...

  5. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) B. Box 贪心

    B. Box Permutation p is a sequence of integers p=[p1,p2,-,pn], consisting of n distinct (unique) pos ...

  6. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) A. Math Problem 水题

    A. Math Problem Your math teacher gave you the following problem: There are n segments on the x-axis ...

  7. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) C Messy

    //因为可以反转n次 所以可以得到任何可以构成的序列 #include<iostream> #include<string> #include<vector> us ...

  8. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) B Box

    #include<bits/stdc++.h> using namespace std; ]; ]; int main() { int total; cin>>total; w ...

  9. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) A Math Problem

    //只要从所有区间右端点的最小值覆盖到所有区间左端点的最大值即可 #include<iostream> using namespace std ; int x,y; int n; int ...

随机推荐

  1. CentOS 安装Asp.net Core & FTP服务

    网络设置 确认是否成功连网: ping baidu.com 如果无法上网请检查以下设置 ip link show vim /etc/sysconfig/network-scripts/ipcfg-(看 ...

  2. kotlin之变量与常量

    版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/217 kotlin之变量与常量 最近开始做新产品,于是乎用 ...

  3. SVN 创建发行版/分支版的步骤

    最近看了很多 Git 与 SVN 的比较,很多都说 SVN 做分支很慢,不知道是从何说起.有可能大家都不清楚,SVN 做分支的正确步骤,特此介绍一下. SVN 服务器后台使用 Berkeley DB ...

  4. Appium(一):java环境、AndroidSDK环境

    1. java环境 java的下载和安装可以看我以前写的Java基础:<java下载和安装>. 2. AndroidSDK环境 2.1 AndroidSDK下载 我们进入:https:// ...

  5. C#的语法----程序结构(2)

    接下来我们继续学习程序流程控制的语法! switch-case 用来处理多条件的定值的判断. 语法: switch(变量或者表达式的值) { case value1:要执行的代码1: break; c ...

  6. JS去重的几种常见方法

    JS数组去重的几种常见方法 一.简单的去重方法 // 最简单数组去重法 /* * 新建一新数组,遍历传入数组,值不在新数组就push进该新数组中 * IE8以下不支持数组的indexOf方法 * */ ...

  7. 【使用篇二】Lombok的介绍与使用(16)

    Lombok通过简单注解来实现精简代码来达到消除冗长代码的目的.它能够提高编码效率.使代码更简洁.消除冗长代码.避免修改字段名时忘记修改方法名. 一.Lombok注解 Lombok主要常用的注解有: ...

  8. 字典树(Trie)详解

    详解字典树(Trie) 本篇随笔简单讲解一下信息学奥林匹克竞赛中的较为常用的数据结构--字典树.字典树也叫Trie树.前缀树.顾名思义,它是一种针对字符串进行维护的数据结构.并且,它的用途超级广泛.建 ...

  9. 分布式文件服务器FastDFS的使用

    分布式项目中涉及到的文件上传与下载,此时使用之前的上传方式,将上传的文件与当前项目所在服务器放在同一个位置,显然不符合分布式项目的理念,此时我们借助FastDFS将上传的文件数据存储到单纯的一个服务器 ...

  10. Python 爬虫从入门到进阶之路(一)

    通用爬虫和聚焦爬虫 根据使用场景,网络爬虫可分为 通用爬虫 和 聚焦爬虫 两种. 通用爬虫 通用网络爬虫 是 捜索引擎抓取系统(Baidu.Google.Yahoo等)的重要组成部分.主要目的是将互联 ...