BZOJ1263 [SCOI2006]整数划分 高精度
欢迎访问~原文出处——博客园-zhouzhendong
去博客园看该题解
题目传送门 - BZOJ1263
题意概括
将n写成若干个正整数之和,并且使这些正整数的乘积最大。 例如,n=13,则当n表示为4+3+3+3(或2+2+3+3+3)时,乘积=108为最大。
题解
设F(n)为n的乘积ans。
那么有:
F(n) = 3 * F(n - 3) n>4
F(n) = n n<=4
然后压位高精度跑一跑就可以了。
证明我想大家都会吧。
呵呵的我一开始输出了后100位,差错半天……
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | #include <cstring> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorithm> using namespace std; typedef long long LL; int n,x; struct BigInt{ static const int MaxLen=600; static const LL mod=1e9; int d; LL v[MaxLen]; void Print(){ printf ( "%lld" ,v[d]); for ( int i=d-1;i>0;i--) printf ( "%09lld" ,v[i]); } void operator = ( int x){ memset (v,0, sizeof v); d=1,v[1]=x; } BigInt operator * ( int x){ BigInt res=* this ; for ( int i=1;i<=res.d;i++) res.v[i]*=x; for ( int i=1;i<=res.d;i++) res.v[i+1]+=res.v[i]/mod,res.v[i]%=mod; while (res.v[res.d+1]>0) res.d++,res.v[res.d+1]=res.v[res.d]/mod,res.v[res.d]%=mod; return res; } void operator *= ( int x){ * this =* this *x; } }Ans; int digit(LL x){ int ans=0; while (x) x/=10,ans++; return ans; } int Find_Digit(LL x, int y){ y--; while (y--) x/=10; return x%10; } int main(){ scanf ( "%d" ,&n); x=n%3; if (x!=2) x+=3; n=(n-x)/3; Ans=x; for ( int i=1;i<=n;i++) Ans*=3; printf ( "%d\n" ,(Ans.d-1)*9+( int )( log (( double )Ans.v[Ans.d])/ log (10)+1)); if (Ans.d>11){ int cnt=digit(Ans.v[Ans.d]),m=100-cnt; printf ( "%lld" ,Ans.v[Ans.d]); int i,j; for (i=Ans.d-1;m>9;i--,m-=9) printf ( "%09lld" ,Ans.v[i]); for (j=m;j>=1;j--) printf ( "%d" ,Find_Digit(Ans.v[i],j+9-m)); } else Ans.Print(); return 0; } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步