您的位置:首页 > 其它

BZOJ 1263 整数划分

2015-03-07 22:18 337 查看

Description


从文件中读入一个正整数\(n\)。要求将\(n\)写成若干个正整数之和,并且使这些正整数的乘积最大。 例如,\(n=13\),则当\(n\)表示为\(4+3+3+3\)(或\(2+2+3+3+3\))时,乘积\(=108\)为最大。


Input


只有一个正整数\(n\)。


Output


第1行输出一个整数,为最大乘积的位数。 第2行输出最大乘积的前\(100\)位,如果不足\(100\)位,则按实际位数输出最大乘积。 (提示:在给定的范围内,最大乘积的位数不超过\(5000\)位)。


Sample Input


13


Sample Output


3

108


Hint


\(10 \le n \le 31000\)


小学奥数结论题,尽可能地拆\(3\)即可,最后省\(4\)时拆成\(2\)与\(2\)(\(2 \times 2 > 3 \times 1\))。

#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;

int n;
struct node
{
short a[10010],l;
node()
{
memset(a,0,sizeof(a));
l = 1;
}
friend inline node operator *(int x,node &y)
{
node ret; ret.l = y.l+1;
for (int i = 1;i <= y.l;++i)
{
ret.a[i] += y.a[i]*x;
ret.a[i+1] += ret.a[i]/10;
ret.a[i] %= 10;
}
if (ret.a[ret.l] == 0) ret.l--;
return ret;
}
inline void print()
{
printf("%d\n",l);
int t = 0;
for (int i = l;i >= 1;--i)
{
printf("%d",this->a[i]);
if (++t == 100) break;
}
}
}ans;

int main()
{
freopen("1263.in","r",stdin);
freopen("1263.out","w",stdout);
scanf("%d",&n); ans.a[1] = 1;
while (n)
{
if (n > 4) n -= 3,ans = 3*ans;
else if (n == 4) n-= 4,ans = 4*ans;
else ans = n*ans,n = 0;
}
ans.print();
fclose(stdin); fclose(stdout);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: