UVALive 6073 Math Magic

                                              6073 Math Magic
Yesterday, my teacher taught us about math: +, -, *, /, GCD, LCM... As you know, LCM (Least
common multiple) of two positive numbers can be solved easily because of


                                      a ∗ b = GCD(a, b) ∗ LCM(a, b)


In class, I raised a new idea: ”how to calculate the LCM of K numbers”. It’s also an easy problem
indeed, which only cost me 1 minute to solve it. I raised my hand and told teacher about my outstanding
algorithm. Teacher just smiled and smiled ...
After class, my teacher gave me a new problem and he wanted me solve it in 1 minute, too. If we
know three parameters N, M, K, and two equations:


          1. SUM(A1, A2, . . . , Ai, Ai+1, . . . , AK) = N
          2. LCM(A1, A2, . . . , Ai, Ai+1, . . . , AK) = M


Can you calculate how many kinds of solutions are there for Ai (Ai are all positive numbers). I
began to roll cold sweat but teacher just smiled and smiled.
Can you solve this problem in 1 minute?
Input
  There are multiple test cases.
  Each test case contains three integers N, M, K. (1 ≤ N, M ≤ 1, 000, 1 ≤ K ≤ 100)
Output
  For each test case, output an integer indicating the number of solution modulo 1,000,000,007(1e9 + 7).
  You can get more details in the sample and hint below.
Hint:
  The first test case: the only solution is (2, 2).
  The second test case: the solution are (1, 2) and (2, 1).


Sample Input
4 2 2
3 2 2


Sample Output
1
2

 

 1 //今天算是长见识了,纠结,看了大神的代码,才知道用dp
 2 //dp[k][n][m]表示由k个数组成的和为n,最小公倍数为m的情况总数
 3 
 4 #include <iostream>
 5 #include <cstdio>
 6 #include <cstring>
 7 #include <algorithm>
 8 using namespace std;
 9 const int maxn = 1005;
10 const int mod = 1000000007;
11 int n, m, k;
12 int lcm[maxn][maxn];
13 int dp[2][maxn][maxn];
14 int fact[maxn], cnt;
15 
16 int GCD(int a, int b)
17 {
18     return b==0?a:GCD(b, a%b);
19 }
20 
21 int LCM(int a, int b)
22 {
23     return a / GCD(a,b) * b;
24 }
25 
26 void init()
27 {
28     for(int i = 1; i <=1000; i++)
29         for(int j = 1; j<=i; j++)
30             lcm[j][i] = lcm[i][j] = LCM(i, j);
31 }
32 
33 void solve()
34 {
35     cnt = 0;
36     for(int i = 1; i<=m; i++)
37         if(m%i==0) fact[cnt++] = i;
38 
39     int now = 0;
40     memset(dp[now], 0, sizeof(dp[now]));
41     for(int i = 0; i<cnt; i++)
42         dp[now][fact[i]][fact[i]] = 1;
43 
44     for(int i = 1; i<k; i++)
45     {
46         now ^= 1;
47         for(int p=1; p<=n; p++)
48             for(int q=0; q<cnt; q++)
49             {
50                 dp[now][p][fact[q]] = 0;
51             }
52 
53         for(int p=1; p<=n; p++)
54         {
55             for(int q=0; q<cnt; q++)
56             {
57                 if(dp[now^1][p][fact[q]]==0) continue;
58                 for(int j=0; j<cnt; j++)
59                 {
60                     int now_sum = p + fact[j];
61                     if(now_sum>n) continue;
62                     int now_lcm = lcm[fact[q]][fact[j]];
63                     dp[now][now_sum][now_lcm] += dp[now^1][p][fact[q]];//
64                     dp[now][now_sum][now_lcm] %= mod;//
65                 }
66             }
67         }
68     }
69     printf("%d\n",dp[now][n][m]);
70 }
71 
72 int main()
73 {
74     init();
75     while(scanf("%d%d%d", &n, &m, &k)>0)
76         solve();
77     return 0;
78 }

 

posted @ 2014-07-08 00:34  ForeverEnjoy  阅读(205)  评论(0编辑  收藏  举报