Tranvick

Untitled

Dec 13th, 2011
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. #include <cstdio>
  2. #include <string>
  3. using namespace std;
  4. #define N 33
  5. #define M 1000000
  6.  
  7. char f[2][N][M];
  8. int n,ans[N],c,t,p;
  9. string cur;
  10.  
  11. void print(int x,int Q){
  12.     int i=x,k=Q,modd=0;
  13.     while (i){        
  14.         ans[++c]=k+1;
  15.         bool r=1;
  16.         for (int a=0;a<=1 && r;a++)
  17.             for (int j=1;j<n;j++)
  18.                 if ((t*j+k+1)%n==modd && f[a][i-1][j]){
  19.                     modd=j;
  20.                     r=0;
  21.                     k=a;
  22.                                 break;
  23.                 }
  24.                
  25.         --i;
  26.     }
  27.     while (c--) printf("%d",ans[c+1]);
  28. }  
  29.  
  30.  
  31. int main(){
  32.     scanf("%d",&n);t=10%n;
  33.     if (n<3){
  34.         printf("%d\n",n);
  35.         return 0;
  36.     }
  37.     f[0][1][1]=f[1][1][2]=1;
  38.     for (int i=1;i<=30;i++){
  39.         if (f[0][i][0]|f[1][i][0]){
  40.             print(i,f[0][i][0]?0:1);
  41.             return 0;
  42.         }
  43.         for (int j=1;j<n;j++){
  44.             p=t*j+1;
  45.             if (p>=n-1){
  46.                 f[0][i+1][p%n]|=f[0][i][j]|f[1][i][j];
  47.                 f[1][i+1][(p+1)%n]|=f[0][i][j]|f[1][i][j];
  48.             } else{
  49.                 f[0][i+1][p]|=f[0][i][j]|f[1][i][j];
  50.                 f[1][i+1][p+1]|=f[0][i][j]|f[1][i][j];
  51.             }
  52.         }
  53.     }
  54.     printf("Impossible\n");
  55.     return 0;
  56. }
  57.            
  58.  
Advertisement
Add Comment
Please, Sign In to add comment