A Very Special Multiple
Difficulty: Hard

Charlie and Johnny play a game. For every integer $X$ Charlie gives, Johnny has to find the smallest positive integer $Y$ such that $X \times Y$ ($X$ multiplied by $Y$) contains only 4s and 0s and starts with one or more 4s followed by zero or more 0s. For example, 404 is an invalid number but 4400, 440, and 444 are valid numbers. 

If $a$ is the number of 4s and $b$ is the number of 0s, can you print the value of $(2\times a) + b$?  

**Input Format**  

The first line of input contains a single integer $T$, the number of test cases.  

$T$ lines follow, each line containing the integer $X$ as stated above.

**Output Format**  

For every $X$, print the output $(2\times a) + b$ in a newline as stated in the problem statement.  

**Constraints**  

$1 \le T \le 100$  
$1 \le X \le 10^{10}$  

**Sample Input**  
 
    3
    4
    5
    80

**Sample Output**  
 
    2
    3
    4

**Explanation**  

For the 1<sup>st</sup> test case, the smallest such multiple of $4$ is 4 itself. Hence the value of $a$ will be $1$ and and the value of $b$ will be $0$, and the answer is $(2\times a) + b = 2$.  

For the 2<sup>nd</sup> test case, $Y = 8$ and 40 is the minimum such multiple of $5$. Hence the values of $a$, $b$ and $(2 \times a) + b$ will be $1$, $1$ and $3$ respectively.


Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:
#
# Complete the 'solve' function below.
#
# The function is expected to return an INTEGER.
# The function accepts LONG_INTEGER x as parameter.
#

def solve(x):
    # Write your code here



Editorial: A Very Special Multiple Infinitum Mar14 Editorial
<style id="MathJax_SVG_styles">.MathJax_SVG_Display {text-align: center; margin: 1em 0em; position: relative; display: block!important; text-indent: 0; max-width: none; max-height: none; min-width: 0; min-height: 0; width: 100%}
.MathJax_SVG .MJX-monospace {font-family: monospace}
.MathJax_SVG .MJX-sans-serif {font-family: sans-serif}
.MathJax_SVG {display: inline; font-style: normal; font-weight: normal; line-height: normal; font-size: 100%; font-size-adjust: none; text-indent: 0; text-align: left; text-transform: none; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0; min-height: 0; border: 0; padding: 0; margin: 0}
.MathJax_SVG * {transition: none; -webkit-transition: none; -moz-transition: none; -ms-transition: none; -o-transition: none}
.mjx-svg-href {fill: blue; stroke: blue}
</style><svg style="display: none;"><defs id="MathJax_SVG_glyphs"></defs></svg><p><a href="https://www.hackerrank.com/contests/infinitum-mar14/challenges/a-very-special-multiple">Problem Statement</a>  </p>

<p><strong>Problem Setter</strong> : <a href="https://www.hackerrank.com/shashank21j">Shashank Sharma</a> <br>
<strong>Problem Tester</strong> : <a href="http://hackerrank.com/itsmecoders">Satvik Gupta</a> <br>
<strong>Required Knowledge</strong>: Fermat's Theorm, Sieve of Eratosthenes, Totient Function, Division
<strong>Time Complexity</strong> : O(root(N))</p>

<p><strong>Approach</strong>  </p>

<p>The Multiple X*Y can be divided into 4*(1111....00000...) <br>
Now 0's are made up of all multiple of 2/5.  </p>

<p>now our multiple (1111...)is rid of all 2,5 hence it is coprime to 10, so Fermat's theorm holds  </p>

<p>1111...n times  can be written as (10<sup>n</sup> - 1)/9, so  </p>

<p><img src="http://hr-blog.s3.amazonaws.com/infinitum-mar14/specialmultiple1.gif" alt="latex" title="">  </p>

<p>hence phi(9x) is a solution but it is not the smallest. <br>
We take the divisors of 9x, minimum of those which solves it is the required number. </p>

<p>but 9x can be a large number. Hence we solve for prime factors of 9x and get solutions s1,s2 etc. We take lcm of those values to get the right answer. </p>

<p>in the end we return 2*a + b</p>

<p><strong>Tester's Code</strong>  </p>

<pre><code>#include &lt;bits/stdc++.h&gt;
using namespace std;
typedef long long int ll;

ll sieve(ll a[],ll b[],ll n)
{
    ll i,k,j;
    for(i=0;i&lt;n;i++)
    {        
       a[i]=i,b[i]=0;
    }
    for (j=2;j*j&lt;n;j++)
    { 
       k=j;
       if(a[j]!=0)
       { 
           while(k*j&lt;n)
           {                
               a[k*j]=0;
               k=k+1;
           }
       }
    }
    i=2;j=0;
    while(i&lt;n)
    {
       if(a[i]!=0)
       {
           b[j]=a[i];
           j++;
       }
       i++;
    }
    return j;
}

ll gcd(ll a,ll b)
{
   ll d;
   a=a&gt;=0?a:-a;
   b=b&gt;=0?b:-b;
   while(b!=0)
   {
       d=a%b;
       a=b;
       b=d;
   }
   return a;
}

ll abmodP(ll a, ll b,ll mod)
{
   ll aprod = a;
   ll ans = 0;
   while(b &gt; 0) {
       if(b &amp; 1) ans = (ans + aprod) % mod;
       b = b &gt;&gt; 1;
       aprod = (aprod &lt;&lt; 1) % mod;
   }
   return ans;
}
long long int power(long long int a,long long int b,long long int c)//To calculate A^B%C
{
   long long int res=1,apower=a;
   while(b &gt; 0)
   {
       if(b%2 == 1)
       {
           res=abmodP(res,apower,c);
       }
       apower = abmodP(apower,apower,c);
       b=b/2 ;
   }

   return res%c;
}



ll a[100005],b[100005];

int main()
{
   ll n = 100000;
   ll k = sieve(a,b,n);
   ll t,x;
   ll i,j;
   scanf("%lld",&amp;t);
   while(t--)
   {
       ll d[20][3];
       scanf("%lld",&amp;x);
       if(x==0)
           cout &lt;&lt; 0 &lt;&lt; endl;
       else if(x==1)
           cout &lt;&lt; 2 &lt;&lt; endl;
       else
       {
           x = x*9;
           ll len=0;
           for(i=0;b[i]*b[i] &lt;= x &amp;&amp; i&lt;k ; i++)
           {
               ll count=0;
               if(x%b[i]==0)
               {
                   ll an1=1;
                   d[len][0]=b[i];
                   while(x%b[i]==0)
                       x=x/b[i],count+=1,an1*=b[i];
                       d[len][1]=count,d[len][2]=an1;
                   len+=1;
               }
           }
           if(x!=1)
               d[len][0]=x,d[len][1]=1,d[len][2]=x,len=len+1;
           ll c2=0,c5=0,c3=0;
           if(d[0][0]==2)
               c2=d[0][1];
           if(d[0][0]==5)
               c5=d[0][1];
           if(len &gt; 1 &amp;&amp; d[1][0]==5)
               c5=d[1][1];
           if(len &gt; 2 &amp;&amp; d[2][0]==5)
               c5=d[2][1];
           ll y=max(c5,c2-2);
           ll e[20];
           ll len1=0;
           for(i=0;i&lt;len;i++)
           {
               if(d[i][0]!=2 &amp;&amp; d[i][0]!=5)
               {
                   ll a1 = d[i][2]/d[i][0];
                   a1 = a1 * (d[i][0]-1);
                   ll  min1=a1;
                   for(j=1;j*j&lt;a1;j++)
                   {
                       if(a1%j==0)
                       {
                           ll po= power((ll)10,j,d[i][2]);
                           if(po &lt; 0)
                                   po+=d[i][2];
                           if(po==1)
                               min1 = min(j,min1);
                           else
                           {
                               po= power((ll)10,(a1/j),d[i][2]);
                               if(po &lt; 0)
                                   po+=d[i][2];
                               if(po==1)
                                   min1 = min(a1/j,min1);
                           }

                       }
                   }
                   e[len1]=min1;
                   len1+=1;
               }
           }
           ll lcm = e[0];
           for(i=1;i&lt;len1;i++)
           {
               lcm = (lcm * e[i])/gcd(lcm,e[i]);   
           }
           printf("%lld\n",2*lcm + y);    
       }

   }

   return 0;
}
</code></pre>

Reference solution (C++):
###C++  
```cpp  

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
    
ll sieve(ll a[],ll b[],ll n)
{
    ll i,k,j;
    for(i=0;i<n;i++)
    {        
       a[i]=i,b[i]=0;
    }
    for (j=2;j*j<n;j++)
    { 
       k=j;
       if(a[j]!=0)
       { 
           while(k*j<n)
           {                
               a[k*j]=0;
               k=k+1;
           }
       }
    }
    i=2;j=0;
    while(i<n)
    {
       if(a[i]!=0)
       {
           b[j]=a[i];
           j++;
       }
       i++;
    }
    return j;
}
    
ll gcd(ll a,ll b)
{
   ll d;
   a=a>=0?a:-a;
   b=b>=0?b:-b;
   while(b!=0)
   {
       d=a%b;
       a=b;
       b=d;
   }
   return a;
}
    
ll abmodP(ll a, ll b,ll mod)
{
   ll aprod = a;
   ll ans = 0;
   while(b > 0) {
       if(b & 1) ans = (ans + aprod) % mod;
       b = b >> 1;
       aprod = (aprod << 1) % mod;
   }
   return ans;
}
long long int power(long long int a,long long int b,long long int c)//To calculate A^B%C
{
   long long int res=1,apower=a;
   while(b > 0)
   {
       if(b%2 == 1)
       {
           res=abmodP(res,apower,c);
       }
       apower = abmodP(apower,apower,c);
       b=b/2 ;
   }
  
   return res%c;
}
    
    
    
ll a[100005],b[100005];
    
int main()
{
   ll n = 100000;
   ll k = sieve(a,b,n);
   ll t,x;
   ll i,j;
   scanf("%lld",&t);
   while(t--)
   {
       ll d[20][3];
       scanf("%lld",&x);
       if(x==0)
           cout << 0 << endl;
       else if(x==1)
           cout << 2 << endl;
       else
       {
           x = x*9;
           ll len=0;
           for(i=0;b[i]*b[i] <= x && i<k ; i++)
           {
               ll count=0;
               if(x%b[i]==0)
               {
                   ll an1=1;
                   d[len][0]=b[i];
                   while(x%b[i]==0)
                       x=x/b[i],count+=1,an1*=b[i];
                       d[len][1]=count,d[len][2]=an1;
                   len+=1;
               }
           }
           if(x!=1)
               d[len][0]=x,d[len][1]=1,d[len][2]=x,len=len+1;
           ll c2=0,c5=0,c3=0;
           if(d[0][0]==2)
               c2=d[0][1];
           if(d[0][0]==5)
               c5=d[0][1];
           if(len > 1 && d[1][0]==5)
               c5=d[1][1];
           if(len > 2 && d[2][0]==5)
               c5=d[2][1];
           ll y=max(c5,c2-2);
           ll e[20];
           ll len1=0;
           for(i=0;i<len;i++)
           {
               if(d[i][0]!=2 && d[i][0]!=5)
               {
                   ll a1 = d[i][2]/d[i][0];
                   a1 = a1 * (d[i][0]-1);
                   ll  min1=a1;
                   for(j=1;j*j<a1;j++)
                   {
                       if(a1%j==0)
                       {
                           ll po= power((ll)10,j,d[i][2]);
                           if(po < 0)
                                   po+=d[i][2];
                           if(po==1)
                               min1 = min(j,min1);
                           else
                           {
                               po= power((ll)10,(a1/j),d[i][2]);
                               if(po < 0)
                                   po+=d[i][2];
                               if(po==1)
                                   min1 = min(a1/j,min1);
                           }
    
                       }
                   }
                   e[len1]=min1;
                   len1+=1;
               }
           }
           ll lcm = e[0];
           for(i=1;i<len1;i++)
           {
               lcm = (lcm * e[i])/gcd(lcm,e[i]);   
           }
           printf("%lld\n",2*lcm + y);    
       }
       
   }
    
   return 0;
}
    
```