hdu 4427 Math Magic(DP,4级)

H - Math Magic
Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

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(109 + 7).
You can get more details in the sample and hint below.
 

Sample Input

4 2 2 3 2 2
 

Sample Output

1 2

Hint

The first test case: the only solution is (2, 2).
The second test case: the solution are (1, 2) and (2, 1).
 
 

思路: dp[K][N][M]; K个 ,和,LCM

             dp[K][N][M]=sum(dp[K-1][N-x][y]);LCM(x,y)=M;

     

#include<cstdio>
#include<cstring>
#include<iostream>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define clr(f,z) memset(f,z,sizeof(f))
using namespace std;
const int mm=1002;
const int mod=1e9+7;
int LCM[mm][mm];
int dp[2][mm][mm],ppp[mm],pos;
int gcd(int a,int b)
{ int c;
  while(b)
  {
    c=b;b=a%b;a=c;
  }
  return a;
}
int lcm(int a,int b)
{
 return a*b/gcd(a,b);
}

int main()
{
  FOR(i,1,mm-1)FOR(j,1,mm-1)
  LCM[i][j]=lcm(i,j);
  int N,M,K;///dp num sum LCM
  while(~scanf("%d%d%d",&N,&M,&K))
  {
    pos=0;
    FOR(i,1,M)
    if(M%i==0)
    ppp[pos++]=i;
    int now=0;
    FOR(i,0,N)FOR(j,0,pos-1)
    dp[now][i][ ppp[j] ]=0;
    dp[now][0][1]=1;
    FOR(i,1,K)///K次
    {
      now^=1;
      FOR(j,0,N)FOR(k,0,pos-1)
      dp[now][j][ ppp[k] ]=0;///clear dp

      FOR(j,i-1,N)///至少是1
      FOR(k,0,pos-1)
      {
        if(dp[now^1][j][ ppp[k] ]==0)continue;
        FOR(p,0,pos-1)///新加p
        {
         int x=j+ppp[p];
         int y=LCM[ ppp[p] ][ ppp[k] ];
         if(x>N||M%y!=0)continue;
         dp[now][x][y]+=dp[now^1][j][ ppp[k] ];
         if(dp[now][x][y]>=mod)
            dp[now][x][y]-=mod;
        }
      }
    }
    printf("%d\n",dp[now][N][M]);
  }
}


posted @ 2013-10-05 15:30  剑不飞  阅读(151)  评论(0编辑  收藏  举报