题目大意: 给一个整数 n n n,将 n n n 拆开成若干个数的和,贡献为这些数的乘积,求最大贡献。
题解
对于 n n n 而言,从它身上拆一个 x ( x ≥ 4 ) x~~(x\geq 4) x (x≥4) 出来,肯定不如拆两个 x 2 \frac x 2 2x 出来优秀,所以按照这种思路,我们可以将 n n n 分成两半,对于每一半,又继续分下去,直到 ≤ 3 \leq 3 ≤3。
对于现在得到的一堆 ≤ 3 \leq 3 ≤3 的数(显然这些数只可能是 2 2 2 或 3 3 3),又有一个贪心:假如有 3 3 3 个 2 2 2,那么可以加起来得到一个 6 6 6,然后再拆成 2 2 2 个 3 3 3,显然 3 × 3 > 2 × 2 × 2 3\times 3 > 2\times 2 \times 2 3×3>2×2×2,这样是更优的。
那么最后 2 2 2 的数量肯定小于 3 3 3 个,并且剩下的数肯定都是 3 3 3,那么按照这种贪心我们将 n n n 进行分解即可。
然后套一个高精度模板即可AC。
代码如下:
#include <cstdio>
#include <cstring>
int n;
struct node{
int a[10010],t;
node(){t=1;memset(a,0,sizeof(a));}
void jinwei()
{
for(int i=1;i<=t;i++)
{
if(a[i]>9)
{
a[i+1]+=a[i]/10;
a[i]%=10;
if(i==t)t++;
}
}
}
node operator *(const node b)
{
node c;
for(int i=1;i<=t;i++)
for(int j=1;j<=b.t;j++)
c.a[i+j-1]+=a[i]*b.a[j];
c.t=t+b.t-1;
c.jinwei();
return c;
}
};
node ksm(node x,int y)
{
node re,tot=x;
re.a[1]=1;
while(y>0)
{
if(y%2==1)re=re*tot;
tot=tot*tot;
y/=2;
}
return re;
}
void write(node x)
{
printf("%d\n",x.t);
if(x.t>100)
{
for(int i=x.t;i>=x.t-99;i--)
printf("%d",x.a[i]);
}
else
{
for(int i=x.t;i>=1;i--)
printf("%d",x.a[i]);
}
}
node two,three;
int main()
{
scanf("%d",&n);
two.a[1]=2;three.a[1]=3;
if(n<=4)printf("%d",n);//特判小于4的情况,其实4特判不特判都一样
else if(n%3==0)write(ksm(three,n/3));//假如可以全部拆成3
else if(n%3==1)write(ksm(two,2)*ksm(three,(n-4)/3));//假如模3余1,那么就要拆出两个2,然后剩下的全拆成3
else write(ksm(two,1)*ksm(three,(n-2)/3));//假如模3余2,那么拆一个2出来,剩下的全拆成3即可
}