ZOJ3662:Math Magic(完全背包),zoj3662math

來源:互聯網
上載者:User

ZOJ3662:Math Magic(完全背包),zoj3662math

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 23 2 2
Sample Output
12


題意:
給出n,m,k,問k個數的和為n,最小公倍數為m的情況有幾種

思路:
因為最小公倍數為m,可以知道這些數必然是m的因子,那麼我們只需要選出這所有的因子,拿這些因子來背包就可以了
dp[i][j][k]表示放了i個數,和為j,公倍數為k的情況有幾種
但是又問題,首先的問題記憶體,直接存明顯爆記憶體,那麼我們需要最佳化
1.因為我現在放第i個數,必然是根據放好的i-1個數來計算的,我們只需要用滾動數組來解決即可
2.對於公倍數,必然不能超過m,而我所有這些m的因子中的數字,無論選哪些,選多少,他們的最小公倍數依然是這些因子之中的,那麼我們可以進行離散化
解決好了之後就是完全背包的問題了


#include <stdio.h>#include <algorithm>#include <string.h>#include <vector>#include <math.h>using namespace std;const int mod = 1e9+7;int dp[2][1005][105];int a[1005],len,pos[1005];int n,m,k;int hash[1005][1005];int gcd(int a,int b){    return b==0?a:gcd(b,a%b);}int lcm(int a,int b){    return a/gcd(a,b)*b;}int main(){    int i,j,x,y;    for(i = 1; i<=1000; i++)//預先處理最小公倍數    {        for(j = 1; j<=1000; j++)            hash[i][j] = lcm(i,j);    }    while(~scanf("%d%d%d",&n,&m,&k))    {        len = 0;        memset(pos,-1,sizeof(pos));        for(i = 1; i<=m; i++)        {            if(m%i==0)            {                a[len] = i;                pos[i] = len++;//離散化            }        }        memset(dp[0],-1,sizeof(dp[0]));        dp[0][0][0] = 1;        for(i = 1; i<=k; i++)        {            memset(dp[i%2],-1,sizeof(dp[i%2]));            for(j = i-1; j<=n; j++)//因為最小必然放1,而我前面已經放了i-1個數了,前面的和最少必然是i-1            {                for(x = 0; x<len; x++)//枚舉前面數位公倍數                {                    if(dp[(i+1)%2][j][x]==-1)                        continue;                    for(y = 0; y<len && (a[y]+j)<=n; y++)//枚舉這一位放哪些數                    {                        int r = hash[a[y]][a[x]];                        int s = j+a[y];                        if(pos[r]!=-1 && r<=m)                        {                            r = pos[r];                            if(dp[i%2][s][r] == -1) dp[i%2][s][r] = 0;                            dp[i%2][s][r]+=dp[(i+1)%2][j][x];                            dp[i%2][s][r]%=mod;                        }                    }                }            }        }        if(dp[k%2][n][pos[m]]==-1)            printf("0\n");        else            printf("%d\n",dp[k%2][n][pos[m]]);    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.