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