POJ3696 The Luckiest Number 欧拉定理

昨天终于把欧拉定理的证明看明白了。。。于是兴冲冲地写了2道题,发现自己啥都不会qwq



题意:给定一个正整数L<=2E+9,求至少多少个8连在一起组成正整数是L的倍数。

这很有意思么。。。

首先,连续的8可表示为:8*(10^x-1)/9;

那么就是L|8*(10^x-1)*9 => 9*L|8*(10^x-1) ,求最小的x;

我们设d=gcd(L,8)

则9*L/d | 8/d*(10^x-1),因为此时9*L/d 和 8/d 互质,所以9*L/d | 10^x-1,所以 10^x ≡ 1 (mod 9*L/d);

然后要证明一个结论:a^x ≡ 1 (mod n)的最小正整数解x0能整除φ(n)

证明:

反证:设不能整除,则设φ(n)=q*x0+r;(1<=r<x0)

因为a^x0 ≡ 1 (mod n),所以a^(q*x0) ≡ 1(mod n)

又因为a^φ(n) ≡ 1 (mod n),所以a^r ≡ 1 (mod n)

因为r<x0,所以假设不成立;

证毕。

所以我们枚举每个φ(9*L/d) 的约数,用快速幂判断就好了。

#include<cstdio>
#include<iostream>
#include<algorithm>
#define ll long long
#define R register ll
using namespace std;
inline ll g() {
    R ret=0,fix=1; register char ch; while(!isdigit(ch=getchar())) fix=ch==‘-‘?-1:fix;
    do ret=ret*10+(ch^48); while(isdigit(ch=getchar())); return ret*fix;
}
ll a[500010],n,ans;
inline ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
inline ll phi(ll n) {
    R m=n; for(R i=2;i*i<=n;++i) if(n%i==0) {
        m=m/i*(i-1);
        while(n%i==0) n/=i;
    } if(n>1) m=m/n*(n-1); return m;
}
inline ll mul(ll a,ll b) {
    a%=n,b%=n; R c=(long double)a*b/n;
    R ans=a*b-c*n; if(ans<0) ans+=n;
    if(ans>=n) ans-=n; return ans;
}
inline ll qpow(ll a,ll p) { R ret=1;
    for(;p;p>>=1,a=mul(a,a)) if(p&1) ret=mul(ret,a); return ret;
}
signed main() { R t=0;
    while(n=g(),n!=0) { //cout<<n<<endl;
        n=9*n/gcd(8,n);
        printf("Case %d: ",++t);
        if(gcd(10,n)==1) {
            R p=phi(n),cnt=0; //cout<<"PHI: "<<p<<endl;
            for(R i=1;i*i<=p;++i) if(p%i==0) {
                a[++cnt]=i;
                if(i*i!=p) a[++cnt]=p/i;
            } sort(a+1,a+cnt+1); R i;
            for(i=1;i<=cnt;++i) if(qpow(10,a[i])==1) break;
            printf("%lld\n",a[i]);
        } else printf("0\n");
    }
}


2019.05.11

原文地址:https://www.cnblogs.com/Jackpei/p/10848288.html

时间: 2024-08-29 20:12:49

POJ3696 The Luckiest Number 欧拉定理的相关文章

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-3696 The Luckiest number

题意: 给出一个数L,求一个最小的x,使长度为x的888...8这个数整除L: 无解输出0,L<=2*10^9: 题解: 即求满足下式的最小x值: 8/9*(10^x-1)==k*L         (k为正整数) 8*(10^x-1)==k*9*L 为继续化简,求出r=gcd(L,8): 8/r *(10^x-1)==k*9*L/r 因为8/r与9*L/r互质,9*L这个因式必在(10^x-1)中,所以原式即为: 10^x-1≡0(mod 9*L/r) 10^x≡1(mod 9*L/r) 设q

[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是变量,且都属于正整数,要根据②式

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

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 consist of

「POJ3696」The Luckiest number【数论,欧拉函数】

# 题解 一道数论欧拉函数和欧拉定理的入门好题. 虽然我提交的时候POJ炸掉了,但是在hdu里面A掉了,应该是一样的吧. 首先我们需要求的这个数一定可以表示成\(\frac{(10^x-1)}{9}\times 8\). 那么可以列出一个下面的方程 \[\frac{(10^x-1)}{9}\times 8=L\times k\] 设\(d=gcd(9L,8)=gcd(L,8)\) \[\frac89(10^x-1)=Lk\] \[\frac{8(10^x-1)}d=\frac{9Lk}{d}\]

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)

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