【DP】 HDU 4427 Math Magic

点击打开链接

题意:求K 个数的数列  ,和为N , LCM 为M 的个数

因为M 的 除数很少 最多10 个  先处理出来就可以O(K*N*10*10) 处理了

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 1<<29;
const LL mod = 1e9+7;
const int MAXN = 110100;//点数的最大值
const int MAXM = 520;//边数的最大值
const double pi = acos(-1.0);
int dp[2][1010][1010];
int gcd(int a,int b)
{
    if(b==0) return a;
    else return gcd(b,a%b);
}
int lcm(int a,int b)
{
    return a/gcd(a,b)*b;
}
void Mod(int &a,int b)
{
    a+=b;
    if(a>=mod) a-=mod;
}
int Lcm[1010][1010];
vector<int> di;
int main()
{
    int n,m,k;
    for(int i=1;i<=1e3;i++)
        for(int j=1;j<=1e3;j++)
            Lcm[i][j]=lcm(i,j);
    while(scanf("%d%d%d",&n,&m,&k)!=EOF)
    {
        di.clear();
        for(int i=1;i<=m;i++)
            if(m%i==0) di.push_back(i);
        memset(dp,0,sizeof(dp));
        for(int i=0;i<di.size();i++)//起始状态
            dp[0][di[i]][di[i]]=1;
        int now=0,last;
        for(int i=1;i<k;i++)
        {
            last=now;
            now^=1;
            for(int j=i;j<=n;j++)
            {
                for(int w=0;w<di.size();w++)
                {
                    if(dp[last][j][di[w]]==0)
                        continue;
                    for(int d=0;d<di.size()&&j+di[d]<=n;d++)
                    {
                        if(m%Lcm[di[w]][di[d]]==0)
                            Mod(dp[now][j+di[d]][Lcm[di[w]][di[d]]],dp[last][j][di[w]]);
                    }
                    dp[last][j][di[w]]=0;//清空
                }
            }
        }
        printf("%d\n",dp[now][n][m]);
    }
    return 0;
}
/*
*/



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值