现在的位置: 首页 > 综合 > 正文

ZOJ3662Math Magic(分组背包+完全背包)

2018年02月21日 ⁄ 综合 ⁄ 共 2132字 ⁄ 字号 评论关闭
I - Math Magic

Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld
& %llu

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(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,求有多少种组合。

分析:因这K个数是M的因子,而M的所有因子又是所有可能组成的最小公倍数的情况,然而N,M<=1000,所以1000以内的数的最多因子个数为35个,所以只需记录下来,用离散化的思想就可以解决。

#include<stdio.h>
#include<string.h>

const int MOD = 1e9+7;
int dp[2][1005][1005];
int LCM[1005][1005],N,M,K,num[105],cet;

int gcd(int a,int b)
{
    if(b==0) return a;
    return gcd(b,a%b);
}
int lcm(int a,int b)
{
    return (a*b)/gcd(a,b);
}

int main()
{
    for(int i=1;i<=1000;i++)
        for(int j=1;j<=1000;j++)
        LCM[i][j]=lcm(i,j);

    while(scanf("%d%d%d",&N,&M,&K)!=EOF)
    {
        cet=0;
        for(int i=1;i<=M;i++)
            if(M%i==0)
                num[++cet]=i;

        for(int i=0;i<=N;i++)//和
            for(int j=1;j<=cet;j++)//最小公倍数
            dp[0][i][num[j]]=0;
        dp[0][0][1]=1;

        int flag=0,ss,ff;
        for(int k=1;k<=K;k++)
        {
            flag^=1;
            for(int i=k;i<=N;i++)
            for(int j=1;j<=cet;j++)
            dp[flag][i][num[j]]=0;

            for(int s=k-1;s<=N;s++)//k-1个数至少和为k-1
            for(int c=1;c<=cet;c++)//每个最小公倍数
            {
                if(dp[flag^1][s][num[c]]==0)
                    continue;
                
                for(int i=1;i<=cet;i++)//每个因子
                {
                    ss=s+num[i];
                    ff=LCM[num[c]][num[i]];

                    if(ss>N||M%ff!=0)continue;

                    dp[flag][ss][ff]+=dp[flag^1][s][num[c]];
                    dp[flag][ss][ff]%=MOD;
                }
            }
        }

        printf("%d\n",dp[flag][N][M]);
    }
}

抱歉!评论已关闭.