#pragma GCC optimize ("Ofast") #include using namespace std; struct Rand{ unsigned x; unsigned y; unsigned z; unsigned w; Rand(void){ x=123456789; y=362436069; z=521288629; w=(unsigned)time(NULL); } Rand(unsigned seed){ x=123456789; y=362436069; z=521288629; w=seed; } inline unsigned get(void){ unsigned t; t = (x^(x<<11)); x=y; y=z; z=w; w = (w^(w>>19))^(t^(t>>8)); return w; } inline double getUni(void){ return get()/4294967296.0; } inline int get(int a){ return (int)(a*getUni()); } inline int get(int a, int b){ return a+(int)((b-a+1)*getUni()); } inline long long get(long long a){ return(long long)(a*getUni()); } inline long long get(long long a, long long b){ return a+(long long)((b-a+1)*getUni()); } inline double get(double a, double b){ return a+(b-a)*getUni(); } inline int getExp(int a){ return(int)(exp(getUni()*log(a+1.0))-1.0); } inline int getExp(int a, int b){ return a+(int)(exp(getUni()*log((b-a+1)+1.0))-1.0); } } ; inline int my_getchar_unlocked(){ static char buf[1048576]; static int s = 1048576; static int e = 1048576; if(s == e && e == 1048576){ e = fread_unlocked(buf, 1, 1048576, stdin); s = 0; } if(s == e){ return EOF; } return buf[s++]; } inline void rd(int &x){ int k; int m=0; x=0; for(;;){ k = my_getchar_unlocked(); if(k=='-'){ m=1; break; } if('0'<=k&&k<='9'){ x=k-'0'; break; } } for(;;){ k = my_getchar_unlocked(); if(k<'0'||k>'9'){ break; } x=x*10+k-'0'; } if(m){ x=-x; } } inline void rd(long long &x){ int k; int m=0; x=0; for(;;){ k = my_getchar_unlocked(); if(k=='-'){ m=1; break; } if('0'<=k&&k<='9'){ x=k-'0'; break; } } for(;;){ k = my_getchar_unlocked(); if(k<'0'||k>'9'){ break; } x=x*10+k-'0'; } if(m){ x=-x; } } inline int rd_int(void){ int x; rd(x); return x; } struct MY_WRITER{ char buf[1048576]; int s; int e; MY_WRITER(){ s = 0; e = 1048576; } ~MY_WRITER(){ if(s){ fwrite_unlocked(buf, 1, s, stdout); } } } ; MY_WRITER MY_WRITER_VAR; void my_putchar_unlocked(int a){ if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){ fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout); MY_WRITER_VAR.s = 0; } MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a; } inline void wt_L(char a){ my_putchar_unlocked(a); } inline void wt_L(long long x){ int s=0; int m=0; char f[20]; if(x<0){ m=1; x=-x; } while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } if(m){ my_putchar_unlocked('-'); } while(s--){ my_putchar_unlocked(f[s]+'0'); } } unsigned long long powmod(unsigned long long a, unsigned long long b, unsigned long long m){ unsigned long long r = 1; while(b){ if(b&1){ r = r * a % m; } b>>=1; a = a * a % m; } return r; } unsigned long long HashMap_ullP_L[4]; template struct HashMap{ char*used; KEY*key; VAL*val; int mem; int n; int mask; HashMap(){ mem = 0; } ~HashMap(){ free(); } void expand(int nn){ if(mem >= nn){ return; } if(mem){ free(); } mem = nn; used = new char[nn]; key = new KEY[nn]; val = new VAL[nn]; } void free(){ if(mem){ mem = 0; delete[] used; delete[] key; delete[] val; } } void init(int nn){ int i; n = 1; nn = nn + (nn + 1) / 2; while(n < nn){ n *= 2; } mask = n - 1; expand(n); for(i=(0);i<(n);i++){ used[i] = 0; } } inline int getHash(const int a){ unsigned long long d = a; d = (((d * HashMap_ullP_L[0]) >> 32) * HashMap_ullP_L[1]) & mask; return d; } inline int getHash(const unsigned a){ unsigned long long d = a; d = (((d * HashMap_ullP_L[0]) >> 32) * HashMap_ullP_L[1]) & mask; return d; } inline int getHash(const long long a){ unsigned long long d = a; d = (((((d * HashMap_ullP_L[0]) >> 32) * HashMap_ullP_L[1]) >> 32) * HashMap_ullP_L[2]) & mask; return d; } inline int getHash(const unsigned long long a){ unsigned long long d = a; d = (((((d * HashMap_ullP_L[0]) >> 32) * HashMap_ullP_L[1]) >> 32) * HashMap_ullP_L[2]) & mask; return d; } inline int getHash(const pair a){ unsigned long long d = (((unsigned long long)a.first) << 32) + ((unsigned long long)a.second); d = (((((d * HashMap_ullP_L[0]) >> 32) * HashMap_ullP_L[1]) >> 32) * HashMap_ullP_L[2]) & mask; return d; } inline VAL& operator[](const KEY a){ int k = getHash(a); for(;;){ if(used[k]==1 && key[k]==a){ break; } if(used[k]==0){ used[k] = 1; key[k] = a; break; } k = (k+1) & mask; } return val[k]; } inline bool exist(const KEY a){ int k = getHash(a); for(;;){ if(used[k]==1 && key[k]==a){ return true; } if(used[k]==0){ break; } k = (k+1) & mask; } return false; } template inline bool exist(const KEY a, S &res){ int k = getHash(a); for(;;){ if(used[k]==1 && key[k]==a){ res = val[k]; return true; } if(used[k]==0){ break; } k = (k+1) & mask; } return false; } } ; int main(){ int Lj4PdHRW; { int i; int j; int k; Rand rnd; for(i=(0);i<(20);i++){ rnd.get(2); } for(i=(0);i<(4);i++){ for(j=(0);j<(32);j++){ k = rnd.get(1,62); HashMap_ullP_L[i] |= (1ULL << k); } HashMap_ullP_L[i] |= (1ULL << 0); HashMap_ullP_L[i] |= (1ULL << 63); } } const int m = 40000; HashMap hs; int KL2GvlyY = rd_int(); for(Lj4PdHRW=(0);Lj4PdHRW<(KL2GvlyY);Lj4PdHRW++){ long long N; rd(N); long long i; long long j; long long k; long long s; long long cof; long long res; k = s = powmod(10, 10000000000LL, N); hs.init(m); hs[s] = 0; for(i=(1);i<(m);i++){ k = k * 10 % N; if(k==s){ wt_L(i); wt_L('\n'); goto Q5VJL1cS; } hs[k] = i; } cof = powmod(10, m, N); k = s; for(i=1;;i++){ k = k * cof % N; if(hs.exist(k)){ res = i * m - hs[k]; break; } } wt_L(res); wt_L('\n'); Q5VJL1cS:; } return 0; } // cLay version 20210103-1 [bug fixed 1] // --- original code --- // { // const int m = 4d4; // HashMap hs; // REP(rd_int()){ // ll @N, i, j, k, s, cof, res; // k = s = powmod(10, 1d10, N); // hs.init(m); // hs[s] = 0; // rep(i,1,m){ // k = k * 10 % N; // if(k==s) wt(i), break_continue; // hs[k] = i; // } // // cof = powmod(10, m, N); // k = s; // for(i=1;;i++){ // k = k * cof % N; // if(hs.exist(k)) res = i * m - hs[k], break; // } // wt(res); // } // }