递归
来源:互联网 发布:中国好吃的泡面知乎 编辑:程序博客网 时间:2024/06/11 02:01
题目描述
Given two positive integers N and M, please divide N into several integers A1, A2, …, Ak (k >= 1), so that:
0 < A1 < A2 < … < Ak;
A1 + A2 + … + Ak = N;
A1, A2, …, Ak are different with each other;
The product of them P = A1 * A2 * … * Ak is a multiple of M;
How many different ways can you achieve this goal?
递归解法
#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>using namespace std;enum {maxn = 100+3, maxm = 50+3};//int f[maxn][maxn][maxm];int dfs(int n, int m, int last, int sum, int mul){ if (sum == n) if (mul %m == 0) return 1; else return 0; int cnt = 0; for (int i=last+1; i <= n -sum; i++) cnt += dfs(n, m, i, sum+i, mul *i); return cnt;}#define OJint main(){ #ifndef OJ freopen("in.txt", "r", stdin); #endif // OJ int n, m; scanf("%d %d", &n, &m); printf("%d\n", dfs(n, m, 0, 0, 1));}
更好的解法:
http://hihocoder.com/discuss/question/2670
0 0