题目链接:http://poj.org/problem?id=1664
思路:
数据较小,考虑深度优先搜索搜索解空间。
代码:
#include <iostream> using namespace std; int M, N, Count = 0; void dfs( int deep, int x, int put ) { if ( deep == N ) { if ( put == M ) Count++; else return; } else { for ( int i = x; i <= M; ++i ) { if ( put + i > M ) break; dfs( deep+1, i, put+i ); } } } int main() { int t; cin >> t; while ( t-- ) { Count = 0; cin >> M >> N; dfs( 0, 0, 0 ); cout << Count << endl; } return 0; }
原文:http://www.cnblogs.com/tallisHe/p/4049384.html