解题思路:
1)每5位为一个基数,枚举5位所有只包含0,1的数ans[],map[i][j]保存(ans[j]*100000^i)%n的值
2)BFS计算所有组合出现的模,直到出现0
#include < iostream >
using namespace std;
#define MAXN 201
#define radix 100000
const int eu[] = { 1 , 10 , 11 , 100 , 101 , 110 , 111 , 1000 , 1001 , 1010 , 1011 , 1100 , 1101 , 1110 , 1111 , 10000 , 10001 , 10010 , 10011 , 10100 , 10101 , 10110 , 10111 , 11000 , 11001 , 11010 , 11011 , 11100 , 11101 , 11110 , 11111 };
int main()
{
bool visit[MAXN];
int id[MAXN][ 2 ],mod[ 20 ][ 32 ], q[MAXN], next[MAXN], ans[ 20 ];
int n, p, i, j, k, t, iter, e;
while (scanf( " %d " , & n) && n)
{
for (i = 0 ;i < MAXN;i ++ )visit[i] = 0 ,next[i] =- 1 ;
memset(ans, 0 , sizeof (ans));
for (i = 0 ;i < 20 ; i ++ )
{
for (j = 0 ;j < 31 ;j ++ )
{
if (i > 0 )mod[i][j] = (mod[i - 1 ][j] * radix) % n;
else mod[i][j] = eu[j] % n;
if (i == 0 &&! visit[mod[i][j]])visit[mod[i][j]] = true ,id[mod[i][j]][ 0 ] = i,id[mod[i][j]][ 1 ] = j;
}
}
for (p = i = 0 ;i < n;i ++ ) if (visit[i])q[p ++ ] = i;
for (i = 1 ;i < 20 &&! visit[ 0 ];i ++ )
{
for (e = p,j = 0 ;j < 31 &&! visit[ 0 ];j ++ )
{
t = mod[i][j];
if (t == 0 )
int b = 0 ;
if ( ! visit[t])visit[t] = true ,id[t][ 0 ] = i,id[t][ 1 ] = j,q[p ++ ] = t;
for (k = 0 ;k < e &&! visit[ 0 ];k ++ )
{
t = (mod[i][j] + q[k]) % n;
if (t == 0 )
int b = 0 ;
if ( ! visit[t])visit[t] = true ,id[t][ 0 ] = i,id[t][ 1 ] = j,next[t] = q[k],q[p ++ ] = t;
}
}
}
for (iter = id[ 0 ][ 0 ],i = 0 ;i !=- 1 ;i = next[i])
{
j = id[i][ 0 ],k = id[i][ 1 ];
p = iter - j;
ans[p] = eu[k];
}
printf( " %d " ,ans[ 0 ]);
for (i = 1 ;i <= iter;i ++ )
printf( " %05d " , ans[i]);
printf( " \n " );
}
return 0 ;
}