天天看點

POJ 3696 The Luckiest number

Description

Chinese people think of '8' as the lucky digit. Bob also likes digit '8'. Moreover, Bob has his own lucky number L. Now he wants to construct his luckiest number which is the minimum among all positive integers that are a multiple of L and consist of only digit '8'.

Input

The input consists of multiple test cases. Each test case contains exactly one line containing L(1 ≤ L ≤ 2,000,000,000).

The last test case is followed by a line containing a zero.

Output

For each test case, print a line containing the test case number( beginning with 1) followed by a integer which is the length of Bob's luckiest number. If Bob can't construct his luckiest number, print a zero.

Sample Input

8

11

16

Sample Output

Case 1: 1

Case 2: 2

Case 3: 0

#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
#define loop(i,j,k) for (int i = j;i != -1; i = k[i])
#define lson x << 1, l, mid
#define rson x << 1 | 1, mid + 1, r
#define ff first
#define ss second
#define mp(i,j) make_pair(i,j)
#define pb push_back
#define pii pair<int,LL>
#define in(x) scanf("%d", &x);
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const double eps = 1e-4;
const int INF = 0x7FFFFFFF;
//const int mod = 998244353;
const int N = 1e6 + 10;
LL n, m, mod, cas = 0;

LL phi(LL x)
{
  LL res = 1;
  for (LL i = 2; i * i <= x; i++)
  {
    if (x%i) continue;
    res *= i - 1; x /= i;
    while (x%i == 0) res *= i, x /= i;
  }
  return res * max(1LL, x - 1);
}

LL gcd(LL x, LL y) { return x%y ? gcd(y, x%y) : y; }

void mul(LL &x, LL y)
{
  LL res = 0;
  for (LL i = x; y; y >>= 1)
  {
    if (y & 1) (res += i) %= mod;
    (i <<= 1) %= mod;
  }
  x = res;
}

bool check(LL x)
{ 
  LL res = 1;
  for (LL i = 10; x; x >>= 1)
  {
    if (x & 1) mul(res, i);
    mul(i, i);
  }
  return res == 1;
}

int main()
{
  while (scanf("%lld", &n), n)
  {
    m = phi(mod = 9 * n / gcd(n, 8));
    LL ans = m;
    if (gcd(mod, 10LL) > 1) ans = 0;
    for (LL i = 1; i * i <= m; i++)
    {
      if (m % i) continue;
      if (check(i)) ans = min(ans, i);
      if (check(m / i)) ans = min(ans, m / i);
    }
    printf("Case %lld: %lld\n", ++cas, ans);
  }
  return 0;
}