一、题目

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

二、题意分析

1.首先要根据题意写出一个公式

有了这个公式,我们就可以进行下一步

这样,得出,其中P其实可以发现是可以变为任意大小的整数的,所以直接不用管

这个公式再联系同余式

变形

行吧,不服不行,这里我们也应该非常清楚,该同余方程有解的充分必要条件是gcd(10^x,M) = 1.根据大整数的素数分解,10的素因子只有2,5,所以进一步推出有解的条件为gcd(10,M)=1.

那么我们其实已经分析出了没有x的条件就是gcd(10,M)!=1.

此处结合

欧拉定理:对任何两个互质的正整数a,m(m≥2)有a^φ(m)≡1(mod m).

那么我们也可以得出,当gcd(10,M)=1时,有

推到这里挺不容易的,但更加不幸的是,这并不意味着我们就成功了 - -!

我们要找的是最小的x。这里我们需要知道指数的mod运算是有循环节的。我们假设上面这个欧拉定理公式的循环节长度是r。那么可以推出

再结合一个常识式子

OK,再开动我们的小脑筋,这不就是让我们求满足

算你狠~

接下来就是在M的欧拉函数值的所有因子F中,找到满足上面10^F≡1(mod M)的最小因子F,你就成功了!

对于代码写法,我是先线性打sqrt(MAX)的素数表,然后再算欧拉函数值。时间250ms(我绝对不是二百五..)。

三、AC代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int MAXN = 1e5+3;
bool isPrime[MAXN];
int Prime[MAXN], nPrime;
LL Factor[MAXN], Cnt; LL Multi(LL a, LL b, LL mod)
{
LL ans = 0;
while(b)
{
if(b&1)
{
ans = (ans + a)%mod;
}
b>>=1;
a = (a+a)%mod;
}
return ans;
} LL Pow(LL a, LL n, LL mod)
{
LL ans = 1;
while(n)
{
if(n&1)
{
ans = Multi(ans, a, mod);
}
n>>=1;
a = Multi(a, a, mod);
}
return ans;
} LL gcd(LL a, LL b)
{
return b==0?a:gcd(b, a%b);
} void getPrime() //线筛素数
{
memset(isPrime, 1, sizeof(isPrime));
isPrime[0] = isPrime[1] = 0;
nPrime = 0;
for(int i = 2; i < MAXN; i++)
{
if(isPrime[i])
{
Prime[nPrime++] = i;
}
for(int j = 0; j < nPrime && (LL)i*Prime[j] < MAXN ; j++)
{
isPrime[ i*Prime[j] ] = 0;
if(i%Prime[j])
break;
}
}
} LL Euler(LL N)
{
LL Phi = N;
for(int i = 0; Prime[i]*Prime[i] <= N; i++)
{
if(N%Prime[i] == 0)
{
Phi = Phi - Phi/Prime[i];
do
{
N/=Prime[i];
}while(N%Prime[i] == 0);
}
}
if(N>1)
Phi = Phi - Phi/N;
return Phi;
} LL solve(LL N)
{
LL M = N/gcd(N, 8)*9;
if(gcd(10, M) != 1)
{
return 0;
}
LL Phi = Euler(M);
Cnt = 0;
for(LL i = 1; i*i < Phi; i++)
{
if(Phi%i == 0)
{
Factor[Cnt++] = i;
Factor[Cnt++] = Phi/i;
}
}
sort(Factor, Factor+Cnt);
for(LL i = 0; i < Cnt; i++)
{
if(Pow(10, Factor[i], M) == 1)
return Factor[i];
}
return 0;
} int main()
{
LL N;
int cnt = 0;
getPrime();
while(scanf("%I64d", &N) && N)
{
cnt++;
printf("Case %d: %I64d\n", cnt, solve(N));
}
return 0;
}

  

POJ_3696 The Luckiest number 【欧拉定理+同余式+对取模的理解】的更多相关文章

  1. hdu 5109(构造数+对取模的理解程度)

    Alexandra and A*B Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  2. C#取模的理解:为什么当a<b,a%b=a?

    一,取模a%b 1,如果a>b,例如10%7=3,这是什么原因呢?可以根据下面的理解 10 =7*1+3,则模就是3 2,如果a<b,例如7%10 = 7,这时怎么得到的呢?根据下面来理解 ...

  3. POJ3696 The Luckiest Number 欧拉定理

    昨天终于把欧拉定理的证明看明白了...于是兴冲冲地写了2道题,发现自己啥都不会qwq 题意:给定一个正整数L<=2E+9,求至少多少个8连在一起组成正整数是L的倍数. 这很有意思么... 首先, ...

  4. BZOJ 3884 欧拉定理 无穷幂取模

    详见PoPoQQQ的博客.. #include <iostream> #include <cstring> #include <cstdio> #include & ...

  5. poj 3696 The Luckiest Number

    The Luckiest Number 题目大意:给你一个int范围内的正整数n,求这样的最小的x,使得:连续的x个8可以被n整除. 注释:如果无解输出0.poj多组数据,第i组数据前面加上Case ...

  6. poj_3696_The Luckiest number

    Chinese people think of '8' as the lucky digit. Bob also likes digit '8'. Moreover, Bob has his own ...

  7. HDU 2462 The Luckiest number

    The Luckiest number Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Ori ...

  8. The Luckiest number(hdu2462)

    The Luckiest number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  9. HDU 1212 Big Number(C++ 大数取模)(java 大数类运用)

    Big Number 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1212 ——每天在线,欢迎留言谈论. 题目大意: 给你两个数 n1,n2.其中n1 ...

随机推荐

  1. Python基础入门-字符串

    字符串详解 字符串的用法是最多的,很多功能的实现都离不开字符串,而且字符串的使用方法也很多,这里面不能说全部给大家一一介绍,只能说把一些常用的列举出来,方便回忆或者说供大家参考,谢谢!请继续往下看~~ ...

  2. Oracle ERP系统借贷关系表

    Oracle ERP系统借贷关系表 成本核算会计信息归纳 按照事务处理的来源类型归纳. 一. 采购接收入库和退货: 1.接收:   借:材料采购 (订单价格) 贷:应计负债 (订单价格) 2.入库: ...

  3. 转 Delphi中XLSReadWrite控件的使用(2)---基本应用

    unit Main; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, ...

  4. .net 特性 Attribute

    public sealed class RemarkAttribute : Attribute { public string Remark { get; set; } // 构造函数 public ...

  5. Spring中的用到的设计模式

    应该说设计模式是我们在写代码时候的一种被承认的较好的模式.好的设计模式就像是给代码造了一个很好的骨架,在这个骨架里,你可以知道心在哪里,肺在哪里,因为大多数人都认识这样的骨架,就有了很好的传播性.这是 ...

  6. vs 2017局域网内调试

    之前调试代码都是在本地启动服务,以  localhost:端口号   的形式调试,今天发现也是可以用ip地址的形式来调用接口,这种方式可以支持内网内Client端调用接口,实现调试的功能,具体方法如下 ...

  7. 谷歌Google浏览器去广告插件ABP插件安装与使用

    ---恢复内容开始--- 最新版本的 Chrome 浏览器,主版本号为 67,数字签名日期为 2018.05.30.对 Chrome 的扩展(俗称插件)安装策略进行了调整——只允许在 Chrome 应 ...

  8. java 笔记整理

    在19寒假对java基础进行自学,总结的笔记整理出来 ==================================================== 排序查找 冒泡排序法1.一共会比较数组元 ...

  9. 渗透测试工具实战技巧 (转载freebuf)

    最好的 NMAP 扫描策略 # 适用所有大小网络最好的 nmap 扫描策略 # 主机发现,生成存活主机列表 $ nmap -sn -T4 -oG Discovery.gnmap 192.168.56. ...

  10. 51nod1832(二叉树/高精度模板+dfs)

    题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1832 题意: 中文题诶~ 思路: 若二叉树中有 k 个节点只 ...