kmjp's blog

競技プログラミング参加記です

yukicoder : No.2383 Naphthol

コーナーケースにやられた。
https://yukicoder.me/problems/no/2383

問題

正六角形をN個並べた図形がある。
このうち次数2の頂点は(2N+4)個あるが、このうちK個を赤く塗る。
反転・回転で同じ状態になる塗り方を同一視すると、何通りか。

解法

ポリアの数え上げ定理を考える。
まず回転は考えなくてよい。180回転は上下反転+左右反転で再現できる。

よって、

  • 反転しない場合
  • 左右反転して一致するケース
  • 上下反転して一致するケース
  • 上下反転+左右反転して一致するケース

をそれぞれ数えればよい。

ちなみにコーナーケースがN=1。

int N,K;
const ll mo=998244353;

ll comb(ll N_, ll C_) {
	const int NUM_=1400001;
	static ll fact[NUM_+1],factr[NUM_+1],inv[NUM_+1];
	if (fact[0]==0) {
		inv[1]=fact[0]=factr[0]=1;
		for (int i=2;i<=NUM_;++i) inv[i] = inv[mo % i] * (mo - mo / i) % mo;
		for (int i=1;i<=NUM_;++i) fact[i]=fact[i-1]*i%mo, factr[i]=factr[i-1]*inv[i]%mo;
	}
	if(C_<0 || C_>N_) return 0;
	return factr[C_]*fact[N_]%mo*factr[N_-C_]%mo;
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>K;
	
	if(N==1) {
		if(K==1||K==5||K==6) {
			cout<<1<<endl;
		}
		else if(K==2||K==4) {
			cout<<3<<endl;
		}
		else {
			cout<<3<<endl;
		}
		
		return;
	}
	
	//何もしなくて同じ
	ll ret=comb(2*N+4,K);
	//左右反転
	if(N%2==0) {
		if(K%2==0) ret+=comb(N+2,K/2);
	}
	else {
		if(K%2==0) {
			ret+=comb(N+1,K/2);
			ret+=comb(N+1,K/2-1);
		}
		else {
			ret+=2*comb(N+1,K/2);
		}
	}
	//上下反転
	if(K%2==0) ret+=comb(N+2,K/2);
	//180度=左右上下反転
	if(K%2==0) ret+=comb(N+2,K/2);
	
	cout<<ret%mo*((mo+1)/2)%mo*((mo+1)/2)%mo<<endl;
	
	
}

まとめ

思いっきり引っかかってしまった。