HDU - 3092 Least common multiple 分组背包

Partychen like to do mathematical problems. One day, when he was doing on a least common multiple(LCM) problem, he suddenly thought of a very interesting question: if given a number of S, and we divided S into some numbers , then what is the largest LCM of these numbers? partychen thought this problems for a long time but with no result, so he turned to you for help!
Since the answer can very big,you should give the answer modulo M.

Input

There are many groups of test case.On each te

st case only two integers S( 0 < S <= 3000) and M( 2<=M<=10000) as mentioned above.

Output

Output the largest LCM modulo M of given S.

Sample Input

6 23

Sample Output

6

题意:把一个数拆成几个数,求这几个数的最小公倍数最大

题解:我们就把n拆成互为素数的数 在用分组背包  因为数比较大 所以我们另保存一个 取log之后的值

 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int N=3010;
int n,m;
int ans[N],ok[N],prime[N],len;
double dp[N];
void init()
{
    len=0;
    prime[0]=1;
    for(int i=2;i<=3000;i++)
    {
        if(ok[i]==0)prime[++len]=i;
        for(int j=1;j<=len&&prime[j]*i<=3000;j++)
        {
            ok[i*prime[j]]=i;
            if(i%prime[j]==0)break;
        }
    }
}
int main()
{
    init();
    while(~scanf("%d%d",&n,&m))
    {
        memset(dp,0,sizeof(dp));
       for(int i=0;i<=n;i++)ans[i]=1;
        
        for(int i=1;i<=len&&prime[i]<=n;i++)
        {
            for(int j=n;j>=prime[i];j--)
            {
                double tmp=log(1.0*prime[i]);
                for(int t=1,k=prime[i];k<=j;k*=prime[i],t++)
                {
                    if(dp[j]<dp[j-k]+tmp*t)
                    {
                        dp[j]=dp[j-k]+tmp*t;
                        ans[j]=ans[j-k]*k;
                        ans[j]%=m; 
                    }
                }
            }
        }
        printf("%d\n",ans[n]);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值