SCOI 2006 整数划分 题解

题目传送门

题目大意: 给一个整数 n n n,将 n n n 拆开成若干个数的和,贡献为这些数的乘积,求最大贡献。

题解

对于 n n n 而言,从它身上拆一个 x    ( x ≥ 4 ) x~~(x\geq 4) x  (x4) 出来,肯定不如拆两个 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即可
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值