[Binary Enumeration] Counting Garlic: Li Bai Dajiu

How many cases are there in total?

The initial value is 2, multiplied by 2 when encountering a store, and subtracted by 1 when encountering a flower

The last time I met Hua, I met the store 4 times in the previous 14 times, and I met Hua 9 times. And after fourteen times, there is one bucket of wine left.

Binary enumeration:

Use 1 to represent the store and 0 to represent the flower, then this process can be represented by a 14-bit binary number.

for(int i=0;i<(1<<14);i++){
     for(int j=0;j<14;j++){
           if(i&(1<<j)){
             //判断第j位是否为1
           } 
     } 
}

Code:

#include<iostream>
#include<cstring>
using namespace std;
int bit[15];
bool check(){
	int n=2;
	int cnt_f=0;
	int cnt_d=0; 
	for(int i=0;i<14;i++){
		if(bit[i]==0){
			n--;
			cnt_f++;
			if(n<0)return false;
			if(cnt_f>9)return false;
		}
		else{
			n*=2;
			cnt_d++;
			if(cnt_d>5)return false;
		}
	}
	return n==1&&cnt_d==5&&cnt_f==9;
}
int main(){
	int n=2;
	int res=0;
	for(int i=0;i<(1<<14);i++){
		memset(bit,0,sizeof(bit));
		for(int j=0;j<14;j++){
			if(i&(1<<j)){
			   bit[j]=1;	
			}
		}
		if(check()){
			res++;
		}
	}
	cout<<res;
}

Guess you like

Origin blog.csdn.net/m0_52043808/article/details/123930975