#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;
}