話說大詩人李白,一生好飲。幸好他從不開車。 一天,他提着酒壺,從家裏出來,酒壺中有酒兩鬥。他邊走邊唱:無事街上走,提壺去打酒。 逢店加一倍,遇花喝一斗。
這一路上,他一共遇到店 5 次,遇到花 10 次,已知最後一次遇到的是花,他正好把酒喝光了。請你計算李白遇到店和花的次序,有多少種可能的方案。
我們已知遇店 5 次,遇花 10 次,並且最後一次遇到花,正好把酒喝光。那麼我們可以把店作爲二進制中的 1,把花作爲二進制中的 0,因爲已經確定最後一次遇到的是花,所以我們需要判斷枚舉的結果是否剛好有 5個 1 和 9個 0。那麼我們就枚舉出 14 位二進制的所有可能並加以判斷即可,判斷思路爲判斷二進制是否有 9 個 0,5個 1,並且最終酒剛好剩 1 鬥 1<<j 在1後加j個零
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <algorithm> #include <cmath> using namespace std; const int inf=0x3f3f3f3f; int main(){ int ans=0; for(int i=0;i<(1<<14);i++){ int tot_1=0; int tot_0=0; int num=2; for(int j=0;j<14;j++){ if(i&(1<<j)){ tot_1++; num=num*2; }else{ tot_0++; num=num-1; } } if(tot_1==5&&tot_0==9&&num==1) ans++; } printf("%d\n",ans); return 0; }