当前位置: 代码迷 >> 综合 >> dinner party dp 暴力
  详细解决方案

dinner party dp 暴力

热度:35   发布时间:2024-01-11 16:12:33.0


dp用bitset处理

读取时注意判断


#include <bits/stdc++.h>
using namespace std ;
typedef long long ll ;/*bitset<32> bitvec2(0xffff); // bits 0 ... 15 are set to 1; 16 ... 31 are 0cout << "bitvec2: " << bitvec2 << endl;bitset<32> a(3) ;cout << a << endl ;*////bitset is very usefulconst int N = 1010 , M = 2010 ;
int T,n,m,i,j,x,y,cnt;
bitset<M>f[N];
void solve(){scanf("%d %d" , &n , &m) ;if(m % 2){puts("No") ;return ;}m /= 2 ;if( !f[n][m] ){puts("No") ;return ;}puts("Yes") ;int cnt = 0 ;//staticint q[100000][2] ;while( n ){int X = 0 , Y = 0 ;for(x = 1 ; x <= n ; x ++ ){for(y = 1 ; y * x <= n ; y ++ ) if(x + y <= m ) if(f[n - x * y][m - x - y ]){X = x , Y = y ;break ;}if(X) break ;}q[ ++ cnt][0] = X ;q[cnt][1] = Y ;n -= X * Y ;m -= X + Y ;}printf("%d\n" , cnt) ;for(int i = 1 ; i <= cnt ; i ++ ) printf("%d %d\n" , q[i][0] , q[i][1]) ;
}
int main(){n = 1000 ;m = 2000 ;f[0][0] = 1 ;for(i = 0 ; i <= n ; i ++ )for(x = 1 ; x + i <= n ; x ++ )for(y = 1 ; x * y + i <= n ; y ++ )f[i + x * y] |= f[i] << (x + y) ;//printf("ashgfkla") ;//for(int i = 0 ; i < 1 ; i ++ )//  cout << f[i] << endl ;scanf("%d" ,&T) ;while( T -- ) solve() ;
}