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>
#include<cstdio>
#define lon long long
#define N 400010
#ifdef unix
#define LL "%lld"
#else
#define LL "%I64d"
#endif
using namespace std;
int prime[N],f[N],num,qlen;
lon q[N];
void get_prime(){
    for(int i=2;i<N;i++){
        if(!f[i]) prime[++num]=i;
        for(int j=1;j<=num;j++){
            if(i*prime[j]>=N) break;
            f[i*prime[j]]=1;
            if(i%prime[j]==0) break;
        }
    }
}
lon gcd(lon a,lon b){
    if(!b) return a;
    return gcd(b,a%b);
}
lon euler(lon x){
    lon res=x;
    for(lon i=2;i*i<=x;i++)
        if(x%i==0){
            res-=res/i;
            while(x%i==0) x/=i;
        }
    if(x>1) res-=res/x;
    return res;
}
void get_q(lon n){
    qlen=0;
    for(int i=1;i<=num&&n>1;i++){
        while(n%(lon)prime[i]==0){
            n/=prime[i];
            q[++qlen]=prime[i];
        }
    }
    if(n>1) q[++qlen]=n;
}
lon mul(lon a,lon b,lon mod){
    lon base=a,r=0;
    while(b){
        if(b&1) r+=base;r%=mod;
        base+=base;base%=mod;
        b>>=1;
    }
    return r;
}
lon poww(lon a,lon b,lon mod){
    lon base=a,r=1;
    while(b){
        if(b&1) r=mul(r,base,mod);
        base=mul(base,base,mod);
        b>>=1;
    }
    return r;
}
int main(){
    int cas=0;lon L;get_prime();
    while(scanf(LL,&L)&&L){
        lon m=9*L/gcd(L,8);
        if(gcd(m,10)>1){
            printf("Case %d: 0\n",++cas);
            continue;
        }
        lon x=euler(m);get_q(x);
        for(int i=1;i<=qlen;i++){
            if(poww(10,x/q[i],m)==1){
                x/=q[i];
            }
        }
        printf("Case %d: ",++cas);
        printf(LL,x);printf("\n");
    }
    return 0;
}
时间: 2024-10-19 07:06:57

The Luckiest number(hdu 2462)的相关文章

HDU 4937 (杭电多校 #7 1003题)Lucky Number(瞎搞)

题目地址:HDU 4937 多校的题以后得重视起来...每道题都错好多次...很考察细节.比如这道....WA了无数次.... 这题的思路自己真心想不到...这题是将进制后的数分别是1位,2位,3位和更多位的分开来计算. 当是1位的时候,显然只有3到6,此时只能是-1 当是2位的时候,可以转换成一元一次方程求解 当是3位的时候,可以转换成一元二次方程求解 当是4位的时候,此时最多也只有7000个数,7000^3接近1e12.所以剩下的直接枚举进制数来判断即可. 代码如下: #include <i

hdu 1394 Minimum Inversion Number(线段树)

Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10853    Accepted Submission(s): 6676 Problem Description The inversion number of a given number sequence a1, a2, ..., a

hdu 2665 Kth number(划分树)

Kth number Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4602 Accepted Submission(s): 1468 Problem Description Give you a sequence and ask you the kth big number of a inteval. Input The first l

hdu 3709 Balanced Number (数位dp)

Balanced Number Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 1871    Accepted Submission(s): 836 Problem Description A balanced number is a non-negative integer that can be balanced if a pi

HDU 1394Minimum Inversion Number(线段树)

题目大意是说给你一个数组(N个),没戏可以将其首部的k(k<N)个元素移动至尾部,这样总共会形成N个序列 现在要求这n个序列中逆序对数最少的那一个序列有多少个逆序对 最初的确是没太多思路,就算知道线段书可以球某一个序列的逆序对数,但是这里要求n次,就没有太多把握了 而最后的方法其实的确是只用求一次的,由于给出的n个数字是0-n-1的一个排列,所以考虑吧a[0]放到最后一个位置时,那以它作为起点的逆序对数相应的会减少a[0]个(这是因为塔处在地一个位置,所有比它晓得数都会在其后方), 然后考虑a[

BestCoder Round #70 Jam&#39;s math problem(hdu 5615)

Problem Description Jam has a math problem. He just learned factorization. He is trying to factorize ax^2+bx+cax?2??+bx+c into the form of pqx^2+(qk+mp)x+km=(px+k)(qx+m)pqx?2??+(qk+mp)x+km=(px+k)(qx+m). He could only solve the problem in which p,q,m,

BestCoder Round #29 1003 (hdu 5172) GTY&#39;s gay friends [线段树 判不同 预处理 好题]

传送门 GTY's gay friends Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 264    Accepted Submission(s): 57 Problem Description GTY has n gay friends. To manage them conveniently, every morning he o

字典树 Trie (HDU 1671)

Problem Description Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let's say the phone catalogue listed these numbers: 1. Emergency 911 2. Alice 97 625 999 3. Bob 91 12 54 26 In this

Valentine&#39;s Day Round 1001.Ferries Wheel(hdu 5174)解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5174 题目意思:给出 n 个人坐的缆车值,假设有 k 个缆车,缆车值 A[i] 需要满足:A[i−1]<A[i]<A[i+1](1<i<K).现在要求的是,有多少人满足,(他坐的缆车的值 + 他左边缆车的值) % INT_MAX == 他右边缆车的值. 首先好感谢出题者的样例三,否则真的会坑下不少人.即同一部缆车可以坐多个人.由于缆车的值是唯一的,所以可以通过排序先排出缆车的位置.求出