zoukankan      html  css  js  c++  java
  • poj3696

    4794: The Luckiest Number

    Time Limit: 1 Sec  Memory Limit: 128 MB
    Submit: 48  Solved: 8
    [Submit][Status][Web Board]

    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 positiv
    e integers that are a multiple of L and consist of only digit '8'.?
    找到一个最小的只含有数字8的十进制正整数,使它为L的倍数,输出其长度

    Input

    The input consists of multiple test cases. Each test case contains exactly one line containing L(1 
    ≤ L ≤ 10^12).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

    由x个n组成的数可以写成n(10^x-1)/9
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    
    typedef long long ll;
    
    ll gcd(ll a,ll b){
       return b?gcd(b,a%b):a;
    }
    
    ll getphi(ll n){
       ll ans=n;
       for (ll i=2;i*i<=n;i++){
        if(n%i==0){
            ans=ans/i*(i-1);
            while(n%i==0) n/=i;
        }
       }
       if(n>1) ans=ans*(n-1)/n;
       return ans;
    }
    
    ll quickmod(ll a,ll b,ll p){
        ll ans=1%p;
        for (;b;b>>=1){
            if(b&1) ans=ans*a%p;
            a=a*a%p;
        }
        return ans;
    }
    
    int main(){
        ll l;
        ll t=0;
        while(scanf("%lld",&l)&&l!=0){
            ++t;
            ll ans=999999999999;
            ll d=gcd(l,8);
            ll k=9*l/d;
            ll phi=getphi(k);
            if(gcd(k*9/gcd(k,8),10)!=1)
            {
                printf("Case %lld: 0
    ",t);
                continue;
            }
            for (ll i=1;i*i<=phi;i++){
                if(phi%i==0){
                if(quickmod(10,i,k)==1%k) ans=min(ans,i);
                else if(quickmod(10,phi/i,k)==1%k) ans=min(ans,phi/i);
                }
            }
            if(ans==999999999999) printf("Case %lld: 0
    ",t);
            else printf("Case %lld: %lld
    ",t,ans);
        }
    return 0;
    }
  • 相关阅读:
    Java StringBuilder、基本类型的包装类
    立个Flag不学好PHP誓不罢休
    LAMP搭建 转
    CentOS使用yum源中自带的rpm包安装LAMP环境
    CentOS RedHat YUM 源扩展补充(32位、64位均有)
    解决phpmyadmin上传文件大小限制的配置方法
    lanmp v2.5一键安装包发布(包括lamp,lnmp,lnamp安装)
    图像处理 jpg png gif svg
    NAT模式/路由模式/全路由模式 (转)
    网页制作中绝对路径和相对路径的区别
  • 原文地址:https://www.cnblogs.com/lmjer/p/9095583.html
Copyright © 2011-2022 走看看