I have try to solve this problem with generate all the combination that possible (Recursive),
but i still get WA!
Can anyone tell me the other method ?
regards,
Red Scorpion

Moderator: Board moderators
No "a terrific backtrack" would NEVER get TL...I LIKE GN wrote: a terrific backtrack would get TL...
Code: Select all
#include<stdio.h>
//#include<math.h>
//#include<string.h>
#define range 1100005
#define drange 2100
#define crange 1100
#define type int
type sum[range],item[range],Total,x[drange],track[crange][crange];
type status[crange][crange],t=1;
void memo(type val,type N)
{
type k,i,j,count=Total;
for(k=0;k<Total;k++)
{
i=val+sum[k]; i%=N; j=item[k]+1; if(j>N)continue;
if(status[i][j]==t)continue;
status[i][j]=t; track[i][j]=val; if(status[0][N]==t)return;
sum[count]=i; item[count]=j; count++;
}
Total=count; i=val%N; j=1; if(status[i][j]==t)return ;
status[i][j]=t; track[i][j]=val; sum[Total]=i; item[Total]=j; Total++;
}
void input(type n,type N)
{
type i,j; for(i=0;i<n;i++)scanf("%d",&x[i]); Total=0;
for(i=0;((status[0][N]<t)&&(i<n));i++)memo(x[i],N);
}
void dispaly(type sum,type n,type N)
{
if(n==1){ printf("%d",track[sum][1]); return; }
type val; val=track[sum][n]; sum-=(val%N);
if(sum<0)sum+=N;
dispaly(sum,n-1,N);
printf(" %d",val);
}
void main()
{
type n,N;
//clrscr();
//freopen("E:\\input.cpp","r",stdin);
//freopen("E:\\myput.cpp","w",stdout);
while(scanf("%d",&N)==1)
{
if(N==0)break; n=2*N-1; input(n,N);
if(status[0][N]<t)printf("No\n");
else { printf("Yes\n"); dispaly(0,N,N); }
printf("\n"); t++;
}