poj_3696_The Luckiest number

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
0

Sample Output

Case 1: 1
Case 2: 2
Case 3: 0

题目化简如下:  8*(10^x  -1)/9=n*L  d=gcd(8,n)  8*(10^x  -1)/d=9*n*L/d  p=8/d,q=9*n/d  p*(10^x  -1)=L*q  p,q互质,10^x=1 mod q  

根据同余定理可知,10^x ≡1(mod q)

根据欧拉定理可知当gcd(a,b)==1时,a^φ(b)≡1(mod b);

即可得出:当gcd(10,q)==1时    10^φ(q)≡1(mod q)   即通过枚举φ(q)的因子(最小因子)就能得出结果



#include<iostream>
#include<cstdio>
#include<algorithm>
#define LL long long
#define ll long long
#define N 1000010
using namespace std;
int prime[N];
int pn=0;
bool vis[N];
int ans[N];
LL gcd(LL a,LL b)
{
     return b==0?a:gcd(b,a%b);
}
long long P(long long  n){ //返回euler(n)
     long long  res=n,a=n;
     for(long long  i=2;i*i<=a;i++){
         if(a%i==0){
             res=res/i*(i-1);//先进行除法是为了防止中间数据的溢出
             while(a%i==0) a/=i;
         }
     }
     if(a>1) res=res/a*(a-1);
     return res;
}
ll mul(ll a,ll b,ll m)
{
    ll res=0;
    while(b)
    {
        if(b&1) res+=a;
        if(res>m) res-=m;
        a+=a;
        if(a>m)
        a-=m;
        b>>=1;
    }
    return res;
}
ll pow(ll a,ll b,ll m)
{
    ll res=1;
    while(b)
    {
        if(b&1) res=mul(res,a,m);
        a=mul(a,a,m);
        b>>=1;
    }
    return res;
}
ll p[10000],cnt[10000];
ll fac[100000];
ll cc,s;
ll phi(ll n)
{
    ll ans=1,i;
    for(i=2;i*i<=n;i++)
    {
        if(n%i==0)
        {
            n/=i;
            ans*=i-1;
            while(n%i==0)
            {
                n/=i;ans*=i;
            }
        }
    }
    if(n>1)
    ans*=n-1;
    return ans;
}
void split(ll x)
{
    cc=0;
    for(ll i=2;i*i<=x;i++)
    {
        if(x%i==0)
        {
            p[cc]=i;
            int count=0;
            while(x%i==0)
            {
                count++;x/=i;
            }
            cnt[cc]=count;
            cc++;
        }
    }
    if(x>1)
    {
        p[cc]=x;cnt[cc]=1;cc++;
    }
}
void dfs(ll count,ll step)
{
    if(step==cc)
    {
        fac[s++]=count;
        return ;
    }
    ll sum=1;
    for(int i=0;i<cnt[step];i++)
    {
        sum*=p[step];
        dfs(count*sum,step+1);
    }
    dfs(count,step+1);
}
int main()
{
    //freopen("i.txt","r",stdin);
    //freopen("o.txt","w",stdout);
    LL n;
    int t=0;
    while(~scanf("%lld",&n),n)
    {
        LL cnt=1;
        int flag=0;
        t++;
        printf("Case %d: ",t);
        LL q=9*n/gcd(8,n);
        if(gcd(q,10)!=1)
        {
            puts("0");
            continue;
        }
        LL phi=P(q);
        split(phi);
        s=0;
        dfs(1,0);
        sort(fac,fac+s);
        //cout<<ans[0]<<endl;
        for(int i=0;i<s;i++)
        {
            if(pow(10,fac[i],q)==1)
            {
                cout<<fac[i]<<endl;
                break;
            }
        }

    }
}

  

时间: 2024-07-29 11:08:33

poj_3696_The Luckiest number的相关文章

[题解](同余)POJ_3696_The Luckiest Number

还是挺难的吧......勉强看懂调了半天 首先表达式可以写成 8(10^x -1)/9,题意为求一个最小的x使L | 8(10^x -1)/9 设d=gcd(L,8) L | 8(10^x -1)/9 <=>9L | 8(10^x -1) <=>9L/d | 10^x -1 (因为 9L/d 和 8/d 互质了 所以 9L/d 能整除(8/d)*(10^x-1)和 8/d 无关,所以可以去掉) <=>10^x 同余 1(mod 9L/d) 引理: 若a,n互质,则满足1

The Luckiest number(hdu2462)

The Luckiest number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1163    Accepted Submission(s): 363 Problem Description Chinese people think of '8' as the lucky digit. Bob also likes digit '

POJ_3696 The Luckiest number 【欧拉定理+同余式+对取模的理解】

一.题目 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 consi

POJ3696 The Luckiest Number

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

poj The Luckiest number

                                     The Luckiest numbe 题目: 在满足|x| + |y|最小时候,让a*|x| + b*|y|最小.求:|x| + |y|最小值. 算法: 同余式的求解运用. 解体步骤: 1.先用gcd判断是否有解.(c%g == 0有解) 2.对式子求最简式.a' = a/g ; b' = b/g: c' = c/g; 3.运用扩展欧几里得求解x,y的值. 4.判断当x,y分别求得最小正整数解时候的大小. 5.确定最后答案

HDU 2462 The Luckiest number

传送门 题目大意: 定义只含有数字8的数为幸运数 给定正整数L,求L的所有倍数中最小的幸运数 算法思路: 设最终答案为x个8,则x满足(10x-1)*8/9≡0 (mod L) 化简:10x≡1 (mod n),其中n=9L/gcd(9L,8) 这是一个离散对数的问题,求解方法如下: 若gcd(10,n)>1,无解 若gcd(10,n)=1,由欧拉定理:10?(n)≡1 (mod n).可以证明,x为?(n)的约数,从小到大枚举约数即可 10l≡1 (mod n) 则成立的最小 l 是? (n)

[POJ3696]The Luckiest number(数论)

题目:http://poj.org/problem?id=3696 题意:给你一个数字L,你要求出一个数N,使得N是L的倍数,且N的每位数都必须是8,输出N的位数(如果不存在输出0) 分析: 首先我们假设N是x个8组成的 那么88888...888=kL 提个8出来:8*111..1111=kL ① 因为题目只要求x的值,所以要弄出关于x的方程 11...111可以写成(10^k-1)/9 于是①变成了8(10^x-1)=9kL ② 再来回顾下题目,②式中x和k是变量,且都属于正整数,要根据②式

The Luckiest number(hdu 2462)

给定一个数,判断是否存在一个全由8组成的数为这个数的倍数 若存在则输出这个数的长度,否则输出0 /* 个人感觉很神的一道题目. 如果有解的话,会有一个p满足:(10^x-1)/9*8=L*p => 10^x-1=9*L*p/8 设m=9*L/gcd(L,8) 则存在p1使得 10^x-1=m*p1 => 10^x=1(mod m) 根据欧拉定理 10^φ(m)=1(mod m) 所以x一定是φ(m)的因数(这好像是某个定理). */ #include<iostream> #incl

poj 3696 The Luckiest number 欧拉函数在解a^x=1modm的应用

题意: 给一个L,求长度最小的全8数满足该数是L的倍数. 分析: 转化为求方程a^x==1modm.之后就是各种数学论证了. 代码: //poj 3696 //sep9 #include <iostream> #include <algorithm> using namespace std; typedef long long ll; ll L; ll factor[65536]; ll mul(ll x,ll y,ll p) { ll ret=0; while(y){ if(y&