zoj Math Magic

Math Magic
Time Limit: 3 Seconds      Memory Limit: 32768 KB
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.


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).




题目大意:问有多少组满足,个数为k个,和为n最小公倍数为m




#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
using namespace std;
const int MOD=1000000007;
int dp[2][1010][1010];


int num[1000];


int gcd(int a,int b)
{
    if(b==0)return a;
    return gcd(b,a%b);
}
int lcm(int a,int b)
{
    return a/gcd(a,b)*b;
}
int LCM[1010][1010];
int main()                      //dp[t][i][j] , 表示取t个数(总共有K个数),和为i时(总共为n),最小公倍数为j(最大为m)的方案数; 
{
    int n,m,k;
    for(int i=1;i<=1000;i++)
      for(int j=1;j<=1000;j++)
        LCM[i][j]=lcm(i,j);        //i和j的最小公倍数。 




    while(scanf("%d%d%d",&n,&m,&k)!=EOF)
    {
        int cnt=0;
        for(int i=1;i<=m;i++)
        {
            if(m%i==0)
               num[cnt++]=i;         //num【1~cnt】中包含这K个数 
        }
        int now=0;
        //memset(dp[now],0,sizeof(dp[now]));
        for(int i=0;i<=n;i++)
          for(int j=0;j<cnt;j++)
            dp[now][i][num[j]]=0;
        dp[now][0][1]=1;


        for(int t=1;t<=k;t++)    //有K个数,循环K次,取t个数的状态由取t-1状态推出来的。直到推到取K个数。 
        {
            now^=1;                    //now为当前状态 ,now^1相当于取反。表示上一个状态。 
          
          for(int i=0;i<=n;i++)
            for(int j=0;j<cnt;j++)
              dp[now][i][num[j]]=0;    //上个循环状态递推完后把上个循环状态的上一个状态清0,顺便接收此次循环的值。 
            for(int i=0;i<=n;i++)       //递推和,知道递推到和为n时。 
              for(int j=0;j<cnt;j++)      //递推 num【1~cnt】里面的因子
              {
                  if(dp[now^1][i][num[j]]==0)continue;  //如果上一个状态和为i,最小公倍数为num【j】不存在这种情况,则不赋值。 
                  for(int p=0;p<cnt;p++)      //因为(1, 2) and (2, 1)不同,所以这里也要从0开始。 
                  {
                      int x=i+num[p];         //加入一个因子num【p】进行讨论。 
                      int y=LCM[num[j]][num[p]];   //算出加入的因子 num【p】和原来的num【j】 的最小公倍数。 
                      if(x>n||m%y!=0)continue;    //如果符合条件。 
                      dp[now][x][y]+=dp[now^1][i][num[j]];    //则新递推得到的状态等于上个状态(now^1)加上本身。 
                      dp[now][x][y]%=MOD;    //得到的新状态要模1000000007; 
                  }
              }
        }
        printf("%d\n",dp[now][n][m]);  //因为是递推的K就就结束,所以此时now就是k,相当于 dp[k][n][m];就是结果。 
    }
    return 0;
}        //因为某人不肯开K大的三维数组(dp[K][n][m]);只开dp[2][n][m];所以当在取t个数的循环下时:dp[now][i][j]相当于dp[t][i][j]
           //而dp[now^1][i][j] 相当于dp[t-1][i][j];   加入一个因子num[p]后 变成dp[t-1+1][i+num[p]][LCM[num[j]][num[p]]]  ==  dp[t][x][y]
  //而递推方程就可理解为: 
           //   dp[now][x][y]+=dp[now^1][i][num[j]];   取t个数,和为x,LCM为y的新状态等于,本身 + 取t-1个数和为i,LCM为j是的状态。  
           























评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值