/* * @(#) msp97_b.c - Problem 'B' ("Multiply operation") solution * of the MSU Personal Programming Contest in 1997. * (c) 1998 Ivan Maidanski http://ivmai.chat.ru * Freeware program source. All rights reserved. ** * Language: ANSI C * Tested with: Borland C++ v3.1 * Last modified: 1998-09-26 22:40:00 GMT+04:00 */ /* Input data file: b.in */ #include /* FILE, EOF, fopen(), fscanf(), fprintf() */ #define MAXCNT 20 #define MAXV 10000 int valcnt; int vals[MAXCNT]; int ptr[MAXV]; void getvals(int k) { int p; if (k>1 && valcntMAXV) { valcnt=MAXCNT; return; } vals[valcnt++]=k; getvals(ptr[k-1]>=0? ptr[k-1] : k/(-ptr[k-1])); if (ptr[k-1]>=0) getvals(ptr[k-1]>k? ptr[k-1]-k : k-ptr[k-1]); } } int calc(int k) { int i,t=0; int len; valcnt=0; getvals(k); len=valcnt; for (i=(k+1)>>1;ivalcnt+1) len=valcnt+1,ptr[k-1]=(k-i>1? k-i : i),t=1; for (i=k<=(MAXV>>1)? (k<<1)-1 : MAXV;i>k;i--) if (valcnt=0,getvals(i),getvals(i-k),len>valcnt+1) len=valcnt+1,ptr[k-1]=i,t=1; for (i=1;((k>>i)<>i),len>valcnt+1) len=valcnt+1,ptr[k-1]=-(1<valcnt+1)) len=valcnt+1,ptr[k-1]=-9,t=1; if (!(k%5) && (valcnt=0,getvals(k/5),len>valcnt+1)) len=valcnt+1,ptr[k-1]=-5,t=1; if (!(k%3) && (valcnt=0,getvals(k/3),len>valcnt+1)) len=valcnt+1,ptr[k-1]=-3,t=1; return t; } int print_res(int k) { int p1,p2; if (k<=1) return 0; for (p1=0;p1=0? ptr[k-1] : k/(-ptr[k-1])); if (ptr[k-1]>=0) p2=print_res(ptr[k-1]>k? ptr[k-1]-k : k-ptr[k-1]); printf("A%d:=",valcnt+1); if ((ptr[k-1]>=0? ptr[k-1] : k/(-ptr[k-1]))>1) printf("A%d",p1); else printf("N"); printf(ptr[k-1]<0? "*" : ptr[k-1]>k? "-" : "+"); if (ptr[k-1]>=0) if ((ptr[k-1]>k? ptr[k-1]-k : k-ptr[k-1])>1) printf("A%d",p2); else printf("N"); else printf("%d",-ptr[k-1]); printf("; // %d*N\n",k); vals[valcnt++]=k; return valcnt; } int main() { int n,k; FILE *f; for (k=1,ptr[0]=0;kMAXV) return 1; printf("%d\n",k); valcnt=0; print_res(k); printf("\n"); } return 0; }