problem with problem
this is my source code,please don't dispare.
its just that i really have done somethin like this:
i decomposed N=2^d[0] * 3^d[1] * 5^d[2] * 7^d[3]
so now i can compact 2^d[0] into 8^(d[0]/3)
or 2^d[0] into 4^(d[0]/2) or
to pile up 2^d[0] * 3^d[1]=
6^min(d[0],d[1]) * 2^( min(d[0],d[1])-d[0] ) *
* 3^(max(d[0],d[1]) -d[1])
or to compact 3^d[1]=9^(d[1]/2).
and the program chooses the smallest number using
this formula's to pack up the Q.
#include <iostream.h>
#define e(n) n==0?0:1
unsigned long N,N2,d[3];
void xde2(unsigned long n){unsigned long i;for(i=1;i<=n;i++)cout<<2;}
void xde3(unsigned long n){unsigned long i;for(i=1;i<=n;i++)cout<<3;}
void xde4(unsigned long n){unsigned long i;for(i=1;i<=n;i++)cout<<4;}
void xde5(unsigned long n){unsigned long i;for(i=1;i<=n;i++)cout<<5;}
void xde6(unsigned long n){unsigned long i;for(i=1;i<=n;i++)cout<<6;}
void xde7(unsigned long n){unsigned long i;for(i=1;i<=n;i++)cout<<7;}
void xde8(unsigned long n){unsigned long i;for(i=1;i<=n;i++)cout<<8;}
void xde9(unsigned long n){unsigned long i;for(i=1;i<=n;i++)cout<<9;}
unsigned long sumcif(unsigned long n){
unsigned long rez=0;
while(n>0)rez+=n%10,n/=10;
return rez;
}
void rez(unsigned long n){
d[0]=0;d[1]=0;d[2]=0;d[3]=0;
N2=n;
while(N2>1){
if(N2%2==0)d[0]++,N2/=2;
if(N2%10==0||N2%10==5)d[2]++,N2/=5;
if(sumcif(N2)%3==0)d[1]++,N2/=3;
if(N2%7==0)d[3]++,N2/=7;
}
if(e(d[0])&e(d[1]))
{unsigned long q2=0,q3=0,q4=0,q5=0,q6=0,q7=0,q8=0,q9=0;
q5=d[2],q7=d[3];
if(d[0]>d[1])
{
q8=(d[0]-d[1])/3;
q6=d[1];
q4=(d[0]-(q8*3)-q6)/2;
q2=d[0]-(q8*3)-(q4*2)-q6;
q3=0;
xde2(q2),xde3(q3),xde4(q4),xde5(q5),xde6(q6),xde7(q7),xde8(q8),xde9(q9);
}
else if(d[0]<d[1]){
q9=(d[1]-d[0])/2;
q6=d[0];
q3=d[1]-d[0]-(q9*2);
q2=0;
q4=0;
xde2(q2),xde3(q3),xde4(q4),xde5(q5),xde6(q6),xde7(q7),xde8(q8),xde9(q9);
}
else if(d[0]==d[1]){
if(d[0]%2==0){
q8=d[0]/3;
q4=(d[0]-(q8*3))/2;
q9=d[1]/2;
xde2(q2),xde3(q3),xde4(q4),xde5(q5),xde6(q6),xde7(q7),xde8(q8),xde9(q9);
}
else if (d[0]%2==1)
{
if(d[0]%3==0){
q8=d[0]/3;
q9=d[1]/2;
q3=d[1]-(q9*2);
xde2(q2),xde3(q3),xde4(q4),xde5(q5),xde6(q6),xde7(q7),xde8(q8),xde9(q9);
}
else if(d[0]%3!=0)
{
q8=d[0]/3;
q4=(d[0]-(q8*3))/2;
q2=d[0]-(q4*2)-(q8*3);
q9=d[1]/3;
q3=d[1]-(q9*2);
xde2(q2),xde3(q3),xde4(q4),xde5(q5),xde6(q6),xde7(q7),xde8(q8),xde9(q9);
}
}
}
}
else if(d[0]==0&&d[1]!=0)
{
if (d[1]%2==0&&d[1]>=2)
xde5(d[2]),xde7(d[3]),xde9(d[1]/2);
else if(d[1]%2==1&&d[1]>=3)
xde3(1),xde5(d[2]),xde7(d[3]),xde9((d[1]-1)/2);
else if(d[1]==1)
xde3(1),xde5(d[2]),xde7(d[3]);
}
else if(d[1]==0&&d[0]!=0)
{
if (d[0]%3==0&&d[0]>=3)xde5(d[2]),xde7(d[3]),xde8(d[0]/3);
else if (d[0]%3==1&&d[0]>=4)
xde2(1),xde5(d[2]),xde7(d[3]),xde8((d[0]-1)/3);
else if (d[0]%3==2&&d[0]>=5)
xde4(1),xde5(d[2]),xde7(d[3]),xde8((d[0]-2)/3);
else if (d[0]==2)
xde4(1),xde5(d[2]),xde7(d[3]);
else if (d[0]==1)
xde2(1),xde5(d[2]),xde7(d[3]);
}
else if(d[1]==0&&d[1]==0){xde5(d[2]);xde7(d[3]);};
}
int main()
{
cin>>N;
rez(N);
return 0;
}
Edited by author 07.03.2005 19:51