[hihoCoder] #1096 : Divided Product

时间限制:10000ms
单点时限:1000ms
内存限制:256MB

描述

Given two positive integers N and M, please divide N into several integers A1, A2, ..., Ak (k >= 1), so that:

1. 0 < A1 < A2 < ... < Ak;

2. A1 + A2 + ... + Ak = N;

3. A1, A2, ..., Ak are different with each other;

4. The product of them P = A1 * A2 * ... * Ak is a multiple of M;

How many different ways can you achieve this goal?

输入

Two integers N and M. 1 <= N <= 100, 1 <= M <= 50.

输出

Output one integer -- the number of different ways to achieve this goal, module 1,000,000,007.

样例提示

There are 4 different ways to achieve this goal for the sample:

A1=1, A2=2, A3=4;

A1=1, A2=6;

A1=2, A2=5;

A1=3, A2=4.

样例输入
7 2
样例输出
4

说好的这题是动规呢?想了半天动规没想出来,写了个DFS,然后居然通过了。有空再想想。

 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 typedef long long ll;
 7 const ll MOD = 1000000007;
 8 
 9 int N, M;
10 
11 void dfs(ll &res, ll idx, ll sum, ll pro) {
12     if (sum == N) {
13         if (pro % M == 0) ++res;
14         return;
15     }
16     for (int i = idx + 1; i <= N - sum; ++i) {
17         dfs(res, i, sum + i, pro * i);
18     }
19 }
20 
21 void solve() {
22     ll res = 0;
23     dfs(res, 0, 0, 1);
24     cout << res << endl;
25 }
26 
27 int main() {
28     while (cin >> N >> M) {
29         solve();
30     }
31 }

 

posted @ 2015-04-13 17:37  Eason Liu  阅读(416)  评论(0编辑  收藏  举报