标签:des style color os io strong for ar art
Description
Input
Output
Sample Input
1 0 1 2 0 0 1 0 0 0 1 1 0 0 0 0 0 0
Sample Output
Collection #1: Can‘t be divided. Collection #2: Can be divided.
多重背包
m = -1 ; while( (1<<(m+1) )-1 < a[i] ) m++ ;
得到m值,对应2^0 , 2^1, 。。2^(m-1) , a[i] - 2^m+1 ;优化每个组的数量
#include <cstdio> #include <cstring> #include <algorithm> int a[10] ; int dp[200000] ; int main() { int ans , i , j , k , t = 0 , m , s ; while( scanf("%d", &a[1]) ) { t++ ; ans = a[1]*1 ; for(i = 2 ; i <= 6 ; i++) { scanf("%d", &a[i]); ans += a[i]*i ; } if(ans == 0) break; printf("Collection #%d:\n", t ); if( ans%2 ) { printf("Can't be divided.\n\n"); continue ; } ans /= 2 ; memset(dp,0,sizeof(dp)); dp[0] = 1 ; for(i = 1 ; i <= 6 ; i++) { m = -1 ; while( (1<<(m+1) )-1 < a[i] ) m++ ; for(j = 0 ; j <= m ; j++) { if(j == m) s = a[i] - (1<<j) + 1 ; else s = 1<<j ; for(k = ans ; k >= i*s ; k--) { if( !dp[k] && dp[k- (i*s) ] ) dp[k] = 1 ; if( dp[ans] ) break; } if( dp[ans] ) break; } if( dp[ans] ) break; } if( dp[ans] ) printf("Can be divided.\n\n"); else printf("Can't be divided.\n\n"); } return 0; }
poj1014+hdu1059--A - Dividing(多重背包,二进制优化)
标签:des style color os io strong for ar art
原文地址:http://blog.csdn.net/winddreams/article/details/38892961