Math Magic(完全背包)

Math Magic

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

Submit Status

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

题意:

给出n,m,k,问k个数的和为n,最小公倍数为m的情况有几种

思路:

因为最小公倍数为m,可以知道这些数必然是m的因子,那么我们只需要选出这所有的因子,拿这些因子来背包就可以了

dp[now][i][j]表示当前状态下,和为i,最小公倍数为j的解的个数。递推K次就出答案了。

注意需要优化!!!

详见代码

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
#define mod 1000000007

int num[1000];
int dp[2][1010][1010];
int LCM[1010][1010];

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()
{
    int n,m,k;
    int i,j;
    for(i=1;i<=1000;i++)//预处理,前1000的最小公倍数
    {
        for(j=1;j<=1000;j++)
        {
            LCM[i][j]=lcm(i,j);
        }
    }
    while(scanf("%d%d%d",&n,&m,&k)!=EOF)
    {
        int cnt=0;
        //因为最小公倍数m已知,所以Ai必定是他的因子
        for(i=1;i<=m;i++)
        {
            if(m%i==0)
                num[cnt++]=i;
        }

        //dp[now][i][j]now表示当前状态下,和为i,最小公倍数为j的解的个数。递推K次就出答案了。
        int now=0;
        //memset(dp[nom],0,sizeof(dp[nom]));
        for(i=0;i<=n;i++)
        {
            for(j=0;j<cnt;j++)
            {
                //初始化,和为i,最小公倍数是num[j]的
                dp[now][i][num[j]]=0;
            }
        }
        dp[0][0][1]=1;

        for(int t=1;t<=k;t++)
        {
            now^=1;
            for(i=0;i<=n;i++)
            {
                for(j=0;j<cnt;j++)
                {
                    dp[now][i][num[j]]=0;
                }
            }

            for(i=t-1;i<=n;i++)
            {
                for(j=0;j<cnt;j++)
                {
                    if(dp[now^1][i][num[j]]==0)continue;
                    for(int p=0;p<cnt;p++)
                    {
                        int x=i+num[p];
                        int y=LCM[num[j]][num[p]];
                        if(x>n||m%y!=0) continue;
                        dp[now][x][y]+=dp[now^1][i][num[j]];
                        dp[now][x][y]%=mod;
                    }
                }
            }
        }
        printf("%d\n",dp[now][n][m]);
    }
    return 0;
}
时间: 2024-10-10 02:38:54

Math Magic(完全背包)的相关文章

ZOJ3662: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

ZOJ3662: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

UVALive 6073 Math Magic

                                              6073 Math MagicYesterday, my teacher taught us about math: +, -, *, /, GCD, LCM... As you know, LCM (Leastcommon multiple) of two positive numbers can be solved easily because of a ∗ b = GCD(a, b) ∗ LCM(a

HDU 4427 Math Magic(三维dp)

题目大意:给你三个数n,m,k.表示有k个数,他们的和为n,k个数的最小公倍数是m.让你求出符合这个条件的k个数的序列有多少种. 一看以为是个数论题,还尝试这各种分解m,然后进行组合数求情况.但是组合出来的数没法做减法啊... 结果是道dp题目.i,j,k表示到了第i个数此时和为j,最小公倍数为k.已经有了多少种组合方法了,直接向后推就可以了啊.数组太大开不开啊,滚动一下就可以了啊. Math Magic Time Limit: 4000/2000 MS (Java/Others)    Mem

DZY Loves Math II:多重背包dp+组合数

Description Input 第一行,两个正整数 S 和 q,q 表示询问数量.接下来 q 行,每行一个正整数 n. Output 输出共 q 行,分别为每个询问的答案. Sample Input 30 3 9 29 1000000000000000000 Sample Output 0 9 450000036 Hint 感谢the Loser协助更正数据对于100%的数据,2<=S<=2e6??,1<=n<=101810^{18}10?18??,1<=q<=10

[ZOJ 3662] Math Magic (动态规划+状态压缩)

先贴代码,晚上回去说 1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 #include <cmath> 5 #include <map> 6 #include <iterator> 7 #include <vector> 8 using namespace std; 9 typedef long long LL; 10 11 int n,m

DP(优化) UVALive 6073 Math Magic

/************************************************ * Author :Running_Time * Created Time :2015/10/28 星期三 20:20:09 * File Name :H.cpp ************************************************/ #include <cstdio> #include <algorithm> #include <iostream&

HDU 4427 Math Magic (2012年长春现场赛H题)

1.题目描述:点击打开链接 2.解题思路:本题要求寻找k个正整数,它们的和恰好是N,它们的LCM恰好是M的解的个数.可以设置一个三维的dp来解决.用dp(i,j,k)表示选择i个数,它们的和恰好是j,它们的LCM恰好是k的个数.那么答案就是dp(k,n,m).不过这里介绍一种利用状态压缩思想求解的方法. 通过题意可以发现,N,M的范围都比较小,不超过1000,而1000之内的所有数的不同素因子的种类数目不超过4个,这是因为2*3*5*7<1000,而2*3*5*7*11>1000.考虑到素因子

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

I - Math Magic Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Description Yesterday, my teacher taught us about math: +, -, *, /, GCD, LCM... As you know, LCM (Least common multiple) of two positive numbers c