Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <string>
- using namespace std;
- #define N 33
- #define M 1000000
- char f[2][N][M];
- int n,ans[N],c,t,p;
- string cur;
- void print(int x,int Q){
- int i=x,k=Q,modd=0;
- while (i){
- ans[++c]=k+1;
- bool r=1;
- for (int a=0;a<=1 && r;a++)
- for (int j=1;j<n;j++)
- if ((t*j+k+1)%n==modd && f[a][i-1][j]){
- modd=j;
- r=0;
- k=a;
- break;
- }
- --i;
- }
- while (c--) printf("%d",ans[c+1]);
- }
- int main(){
- scanf("%d",&n);t=10%n;
- if (n<3){
- printf("%d\n",n);
- return 0;
- }
- f[0][1][1]=f[1][1][2]=1;
- for (int i=1;i<=30;i++){
- if (f[0][i][0]|f[1][i][0]){
- print(i,f[0][i][0]?0:1);
- return 0;
- }
- for (int j=1;j<n;j++){
- p=t*j+1;
- if (p>=n-1){
- f[0][i+1][p%n]|=f[0][i][j]|f[1][i][j];
- f[1][i+1][(p+1)%n]|=f[0][i][j]|f[1][i][j];
- } else{
- f[0][i+1][p]|=f[0][i][j]|f[1][i][j];
- f[1][i+1][p+1]|=f[0][i][j]|f[1][i][j];
- }
- }
- }
- printf("Impossible\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment