http://poj.org/problem?id=1426
Tested, all values from 1-200 have a long long solution, so can be directly stored with a long long
Starting from 1, each transfer to 10*s and 10*s+1, only the remainder can be stored,
For the remainder I, only the first remainder of I is the most useful, only record this value can be
#include <cstdio> #include <cstring> #include <queue>using namespace Std;const int Maxn=222;typedef Long Long ll;ll dp[maxn];int n;queue<int >que;int main () {while (scanf ("%d", &n) ==1&&n) { if (n==1) {puts ("1"); continue;} Memset (Dp,-1,sizeof (DP)); while (!que.empty ()) Que.pop (); Dp[1]=1; Que.push (1); BOOL Fl=false; while (!que.empty ()) {int S=que.front (); Que.pop (); if (s==0) {printf ("%i64d\n", Dp[s]); Fl=true; Break } int t=s*10%n; if (dp[t]==-1) {dp[t]=10*dp[s]; Que.push (t); } t= (s*10+1)%n; if (dp[t]==-1) { dp[t]=10*dp[s]+1; Que.push (t); }} if (!FL) puts ("-1"); } return 0;}
POJ 1426 Find The multiple idea, linear congruence, search difficulty: 2