HDU - 3092 Least common multiple

#HDU - 3092 Least common multiple

数论+dp,嗯,我看出来了,但是……不会写啊啊啊啊

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 test 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

Hint: you can divied 6 as 1+2+3 and the LCM(1,2,3)=6 is the largest so we output 6%23=6.

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
double dp[3005];
int ans[3005],num[505],prime[3005];
int main(){
	double tmp;
	int n,i,j,k,p,q,mod;
	k=0;
	memset(prime,0,sizeof(prime));
	for(i=2;i<=3000;i++){
		if(!prime[i]){
			num[k++]=i;
		}
		for(j=0;j<k&&num[j]*i<=3000;j++){
			prime[num[j]*i]=1;
			if(i%num[j]==0)
			break;
		}
	}
	while(scanf("%d%d",&n,&mod)!=EOF){
		for(i=0;i<=n;i++){
			dp[i]=0;
			ans[i]=1;
		}
		for(i=0;i<k&&num[i]<=n;i++){
			for(j=n;j>=num[i];j--){
				tmp=log(num[i]*1.0);
				for(p=num[i],q=1;p<=j;p*=num[i],q++){
					if(dp[j-p]+q*tmp>dp[j]){
						dp[j]=dp[j-p]+q*tmp;
						ans[j]=(ans[j-p]*p)%mod;
					}
				}
			}
		}
		printf("%d\n",ans[n]);
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值