<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) F2. Wrong Answer on test 233 (Hard Version) dp 數(shù)學(xué)

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

      題意

      現(xiàn)在有n道題,每道題有k個(gè)答案,但是你現(xiàn)在犯傻了,把第一題的答案交到了第二題,第二題交到了第3題,第k題交到了第(k%n)+1題的位置上去。

      現(xiàn)在想知道,有多少種填答案的方案,可以使得交換后的正確數(shù)量多于交換前的正確數(shù)量。

      題解

      數(shù)據(jù)范圍小的話,dp[i][j]表示現(xiàn)在考慮到了第i題,交換后比交換前多得j分。

      那么如果h[i]==h[i+1]的話,dp[i][j]=dp[i-1][j],因?yàn)闊o論如何填什么正確得個(gè)數(shù)都不會(huì)變。

      其他情況 dp[i][j] = dp[i-1][j+1]+dp[i-1][j-1]+(k-2)dp[i-1][j],有一種情況是之前對(duì)了,轉(zhuǎn)換后錯(cuò)了;之前錯(cuò)了,轉(zhuǎn)換后對(duì)了;其他k-2種答案都保持不變。


      hard version我們要反著做,假設(shè)我們知道最后轉(zhuǎn)換后和轉(zhuǎn)換前分?jǐn)?shù)一樣得方案數(shù)為ans的話,那么k^n-ans表示的是轉(zhuǎn)換后得分發(fā)生改變的方案數(shù)。

      又因?yàn)檗D(zhuǎn)換前分?jǐn)?shù)高和轉(zhuǎn)換后分?jǐn)?shù)高的方案數(shù)是一樣的,因?yàn)閷?duì)稱,所以最后答案一定是 (k^n-ans)/2

      那么這個(gè)ans怎么做呢,假設(shè)現(xiàn)在h[i]!=h[i+1]的個(gè)數(shù)為num個(gè),因?yàn)橄嗤脑挍]有意義,因?yàn)樘钍裁炊紵o所謂

      我們枚舉+1的位置有多少個(gè),C(num,i);同樣的-1也得i個(gè)C(num-i,i),其他num-2i個(gè)位置有k-2種選擇(k-2)(num-2i),剩下n-num個(gè)位置都有k個(gè)選擇k(n-num)。

      那么i個(gè)+1位置的方案數(shù)其實(shí)就是C(num,i)C(num-i,i)(k-2)(num-2i)k(n-num),最后用所有的方案數(shù)減去他再除以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;
      }
      
      posted @ 2019-11-24 23:26  qscqesze  閱讀(691)  評(píng)論(0)    收藏  舉報(bào)
      主站蜘蛛池模板: 精品熟女少妇免费久久| 日本精品一区二区不卡| 国产亚洲天堂另类综合| A毛片终身免费观看网站| 国产伦精品一区二区亚洲| 亚洲男人天堂av在线| 精品一区二区免费不卡| 亚洲欧洲一区二区综合精品| 日韩国产精品中文字幕| 丁香婷婷激情综合俺也去| 99久久精品国产一区二区| 最新国产精品好看的精品| 国产av成人精品播放| 亚洲一区二区三区18禁| 熟女精品色一区二区三区| 国产精品人妻在线观看 | 国产超碰无码最新上传| 视频一区视频二区中文字幕| 亚洲欧美日韩综合久久| 久久久精品94久久精品| 婷婷五月综合丁香在线| 国产亚洲视频在线播放香蕉| 亚洲国产精品久久无人区| 亚洲av综合久久成人网| 人妻夜夜爽天天爽三区丁香花| 日韩精品人妻中文字幕| 国产三级国产精品久久成人 | 蜜臀在线播放一区在线播放 | 国内精品久久久久影院薰衣草| 亚洲av永久无码精品天堂久久| 亚洲中文字幕日韩精品| 亚洲日本欧美日韩中文字幕| 四虎成人精品国产永久免费| 开心一区二区三区激情| 拜泉县| 亚洲精品自产拍在线观看动漫| 亚洲夂夂婷婷色拍WW47| 亚洲高清WWW色好看美女| 国产一区二区三区色噜噜| 国产一二三区在线| 欧美日韩亚洲国产|