鉴于数据量比较小,直接使用递归省时省事(在敲代码方面)。往后递归,直至递归M次成功就是一个题目要求的子集。
Run Time: 0sec
Run Memory: 304KB
Code length: 599Bytes
Submit Time: 2011-12-14 00:17:49
// Problem#: 4428
// Submission#: 1074713
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include<cstdio>
using namespace std;
int count;
int N, M;
void F( int n, int m ) {
if ( m == M )
count++;
else {
for ( int i = 2; n + i + 2 * ( M - m - 1 ) <= N; i++ )
F( n + i, m + 1 );
}
}
int main()
{
int T;
scanf( "%d", &T );
while ( T-- ) {
scanf( "%d%d", &N, &M );
if ( M == 0 )
printf( "1\n" );
else {
count = 0;
for ( int i = 1; i <= N; i++ )
F( i, 1 );
printf( "%d\n", count );
}
}
return 0;
}