1263: [scoi2006] integer division time limit: 1 sec memory limit: 162 MB
Submit: 677 solved: 332
[Submit] [Status] Description reads a positive integer N (10 ≤ n ≤ 31000) from the file ). It is required that n be written into the sum of several positive integers, and the product of these positive integers is maximized. For example, n = 13, when n is 4 + 3 + 3 + 3 (or 2 + 2 + 3 + 3 + 3), the product = 108 is the maximum. Input has only one positive integer: n (10 ≤ n ≤ 31000) Output 1st rows output an integer, which is the maximum number of digits of the product. The first 2nd bits of the Maximum Product of 100 rows of output, if less than 100 bits, the maximum product of the actual number of BITs is output. (Note: within a given range, the maximum number of bits of product cannot exceed 5000 bits ). Sample input13
Sample output3
108
Hint Source
Question:
The natural logarithm E has the optimum decomposition, which is a magical constant.
So our algorithm comes:
If n % 3 = 0, divide N into N/3 3
If n % 3 = 1, divide N into N/3-1 3 and 1 4
If n % 3 = 2, divide N into N/3 3 and 1 2
Code:
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<iostream> 7 #include<vector> 8 #include<map> 9 #include<set>10 #include<queue>11 #include<string>12 #define inf 100000000013 #define maxn 5000+10014 #define maxm 500+10015 #define eps 1e-1016 #define ll long long17 #define pa pair<int,int>18 #define for0(i,n) for(int i=0;i<=(n);i++)19 #define for1(i,n) for(int i=1;i<=(n);i++)20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)21 #define for3(i,x,y) for(int i=(x);i>=(y);i--)22 #define mod 1023 using namespace std;24 inline int read()25 {26 int x=0,f=1;char ch=getchar();27 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}28 while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}29 return x*f;30 }31 int n,a[maxn];32 void mul(int x)33 {34 for1(i,a[0])a[i]*=x;35 for1(i,a[0])36 {37 a[i+1]+=a[i]/mod;38 a[i]%=mod;39 }40 while(a[a[0]+1])a[0]++;41 }42 int main()43 {44 freopen("input.txt","r",stdin);45 freopen("output.txt","w",stdout);46 n=read();47 a[a[0]=1]=1;48 if(n%3==0)for1(i,n/3)mul(3);49 else if(n%3==1)50 {51 for1(i,(n/3-1))mul(3);52 mul(4);53 }54 else 55 {56 for1(i,n/3)mul(3);57 mul(2);58 }59 printf("%d\n",a[0]);60 for3(i,a[0],max(a[0]-100+1,1))printf("%d",a[i]);61 printf("\n"); 62 return 0;63 }
View code
Bzoj1263: [scoi2006] Integer Division