Codeforces 100548F (XI ' an field game f): the principle of tolerance and repulsion

Source: Internet
Author: User

Main topic:

Color the n rows of items in a row, available in M colors.

Requires different colors for adjacent items, and there is a total of K colors, asking all possible scenarios

Analysis:

Choose K species from M color, there are C (m,k) methods, then we only consider the k color of the coating method can be

Obviously the first item has K-type coating method, the back because can not be the same as the previous only k-1 seed coating method

So it's easy to think of a formula: k* (k-1) ^ (n-1)

But this formula is not more than the k color of the coating method, the question must be K-type, how to do?

Consider a simplified version of the issue first:

The number of programs that use and use up to 5 different colors to coat unrelated five items

Using factorial method can calculate ans=120, change a way of thinking about this problem, it is easy to think

Ans (Take Five colors) =5^5 (take no more than 5 colors)-C (5,4) *4^5 (take no more than 4 colors)

However, it turns out that ans is less than 0, how is this going? Easy to find in fact, less than four colors of the scheme is reduced repetition

So I thought of the need for a rebuke

Ans=c (5,5) *5^5-c (5,4) *4^5+c (5,3) *3^5-c (5,2) *2^5+c (5,1) *1^5 =120

This problem has been solved. The original problem is almost the same.

Code:

#include <iostream>#include<stdio.h>#include<string.h>#include<algorithm>#include<string>#include<ctype.h>using namespacestd;Const Long LongMod=1000000007;Const Long Longny=500000004;Long Longn,m,k;Long Longcm[1000010];Long Longcn[1000010];Long Longck[1000010];Long Longinv[1000010];Long LongMoLong Longx) {     while(x<0) x+=MoD; returnX%MoD;}Long LongEXGCD (Long LongALong LongBLong Long&x,Long Long&y) {    if(a==0&&b==0)return-1; if(b==0) {x=1; y=0;returnA;} Long LongD=EXGCD (b,a%b,y,x); Y-=a/b*x; returnD;}Long LongINV (Long LongALong LongN) {    Long Longx, y; Long LongD=EXGCD (a,n,x,y); if(d==1)return(x%n+n)%N; Else return-1;}Long LongQuickmod (Long LongALong LongBLong LongM//a^b%m{    Long Longres=1;  while(b) {if(b&1) Res=res*a%MoD; b>>=1; A=a*a%MoD; }    returnRes;}voidIni () {cn[0]=cm[0]=1; memset (CM,0,sizeof(cm)); cm[0]=1; intTmp=min (m/2, K);  for(intI=1; i<=tmp;i++) {Cm[i]= (cm[i-1]* (m+1-i)%mod*inv[i])%MoD; }    if(cm[k]==0) Cm[k]=cm[m-K]; ck[0]=ck[k]=1;  for(intI=1; i<=k/2; i++) {Ck[i]= (ck[i-1]* (k +1-i)%mod*inv[i])%MoD; Ck[k-i]=Ck[i]; }}intMain () {//freopen ("In.txt", "R", stdin); //freopen ("OUT.txt", "w", stdout);inv[1]=1;  for(intI=2; i<=1000000; i++) {Inv[i]=INV (I,MOD); }    intT; scanf ("%d",&t); intcas=0;  while(t--) {CAs++; scanf ("%i64d%i64d%i64d",&n,&m,&k);        INI (); Long Longans=0; Long Longp=1;  for(inti=k;i>=1; i--) {ans= (ans+p* (ck[k-i]) *i%mod*quickmod (i-1, N-1, MoD)%mod) +mod)%MoD; P=-p; } ans= (Ans*cm[k])%MoD; printf ("Case #%d:%c%i64d\n"Cas' ', ans); }    return 0;}

Codeforces 100548F (XI ' an field game f): the principle of tolerance and repulsion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.