Uva 12063 Zero and Ones
给个链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3215
题目大意是求有多少个n位二进制数,0和1的个数相等,并且是k的倍数。
这个dp一下就可以了,设f[i][j][l]为i位的mod k=j的有l个1 的数的个数。
#include<iostream> #include<cstdio> #include<cstdlib> #include<cmath> #include<algorithm> #include<cstring> #define ll long long using namespace std; ll f[71][105][71]; //f[i][j][k] means the number of binary_numbers,which has i digits(without 0 before the first 1) //and k digits of 1.And it's equal to j (mod K) int T,n,k,cnt; inline int add(int x,int y,const int ha){ x+=y; if(x>=ha) return x-ha; else return x; } inline void dp(){ memset(f,0,sizeof(f)); f[1][!(k==1)][1]=1; for(int i=1;i<n;i++) for(int j=0;j<k;j++) for(int l=1;l<=i;l++) if(f[i][j][l]){ ll base=f[i][j][l]; int to=add(j,j,k); f[i+1][to][l]+=base; to=add(to,1,k); f[i+1][to][l+1]+=base; } } int main(){ scanf("%d",&T); while(T--){ scanf("%d%d",&n,&k); printf("Case %d: ",++cnt); if((n&1)||!k){ puts("0"); continue; } dp(); printf("%lld\n",f[n][0][n>>1]); } return 0; }
我爱学习,学习使我快乐