hdu 4427 Math Magic(DP,4级)

本文探讨如何在给定三个参数N、M和K的情况下,解决求解一组K个正整数的和为N且最小公倍数为M的组合数问题。通过动态规划的方法,实现复杂度优化,最终输出解决方案的数量对1,000,000,007取模的结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 (A 1, A 2, ..., A i, A i+1,..., A K) = N
2. LCM (A 1, A 2, ..., A i, A i+1,..., A K) = M
Can you calculate how many kinds of solutions are there for A i (A i 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(10 9 + 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]);
  }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值