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组成的数,是L的倍数。

思路:由一系列证明得到,ans=phi(N)的满足题意的最小因子。

关键:    对于X,求其满足题意的最小因子p|X,可以这样求,枚举素因子prime,如果X/prime满足题意,则X=X/prime。

存疑:我感觉复杂度是根号级别的,但是我看到ppt上说是log级别。

#include<cstdio>
#include<cstdlib>
#include<iostream>
using namespace std;
#define ll long long
ll gcd(ll a,ll b){ if(b==) return a;return gcd(b,a%b);}
ll qmul(ll a,ll x,ll Mod){ll res=; a%=Mod; while(x){if(x&) res=(res+a)%Mod;a=(a+a)%Mod;x>>=;} return res;}
ll qpow(ll a,ll x,ll Mod){ll res=; a%=Mod; while(x){if(x&1LL) res=qmul(res,a,Mod); a=qmul(a,a,Mod); x>>=;} return res;}
ll phi(ll x)
{
ll tx=x,res=x;
for(int i=;i*i<=tx;i++){
if(tx%i==){
res-=res/i;
while(tx%i==) tx/=i;
}
}
if(tx>) res-=res/tx;
return res;
}
ll find(ll Mod,ll py) //得到最小因子满足条件
{
ll n=py; ll tp[][]; int k=;
for(ll i=;i*i<=n;i++) //唯一分解
if(n%i==){
k++; tp[k][]=i; tp[k][]=;
while(n%i==){
n/=i; tp[k][]++;
}
}
if(n>) k++, tp[k][]=n, tp[k][]=; for(int i=;i<=k;i++)
for(int j=;j<=tp[i][];j++)
if(qpow(,py/tp[i][],Mod)==)
py/=tp[i][];
return py;
}
int main()
{
ll N,M,Case=;
while(~scanf("%lld",&N)&&N){
printf("Case %lld: ",++Case);
N=N*/gcd(N,);
if(gcd(N,)!=) printf("0\n");
else printf("%d\n",find(N,phi(N)));
}
return ;
}

POJ3696:The Luckiest number(欧拉函数||求某数最小的满足题意的因子)的更多相关文章

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

    题意: 给一个L,求长度最小的全8数满足该数是L的倍数. 分析: 转化为求方程a^x==1modm. 之后就是各种数学论证了. 代码: //poj 3696 //sep9 #include <i ...

  2. BZOJ2818: Gcd 欧拉函数求前缀和

    给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. 如果两个数的x,y最大公约数是z,那么x/z,y/z一定是互质的 然后找到所有的素数,然后用欧拉函数求一 ...

  3. poj 2773 利用欧拉函数求互质数

    题意:找到与n互质的第 k个数 开始一看n是1e6 敲了个暴力结果tle了,后来发现k达到了 1e8 所以需要用到欧拉函数. 我们设小于n的 ,与n互质的数为  (a1,a2,a3.......a(p ...

  4. UVA 12493 Stars (欧拉函数--求1~n与n互质的个数)

    pid=26358">https://uva.onlinejudge.org/index.phpoption=com_onlinejudge&Itemid=8&cate ...

  5. 【poj 1284】Primitive Roots(数论--欧拉函数 求原根个数){费马小定理、欧拉定理}

    题意:求奇质数 P 的原根个数.若 x 是 P 的原根,那么 x^k (k=1~p-1) 模 P 为1~p-1,且互不相同. (3≤ P<65536) 解法:有费马小定理:若 p 是质数,x^( ...

  6. 欧拉函数求在1-n-1与n互质的个数

    long long phi(long long x) { long long res=x,a=x,i; ;i*i<=a;i++) { ) { res=res/i*(i-); ) a=a/i; } ...

  7. 欧拉函数,打表求欧拉函数poj3090

    欧拉函数 φ(n) 定义:[1,N]中与N互质的数的个数 //互质与欧拉函数 /* 求欧拉函数 按欧拉函数计算公式,只要分解质因数即可 */ int phi(int n){ int ans=n; ;i ...

  8. hdoj 1787 GCD Again【欧拉函数】

    GCD Again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  9. HDU1695-GCD(数论-欧拉函数-容斥)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

随机推荐

  1. http状态码的分类

    状态码分类 转自http://www.cnblogs.com/TankXiao/archive/2013/01/08/2818542.html HTTP状态码被分为五大类, 目前我们使用的HTTP协议 ...

  2. nginx原配置

    #原配置 server { listen ; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main ...

  3. SolidEdge 工程图中如何快速将同一类元素放到同一个图层

    在图层选项卡中新建一个尺寸线图层   点击聪慧选项(把它点凹下去),然后点击任意尺寸线,弹出聪慧选取选项,点击确定,则自动选择了所有尺寸线   点击移动图元,把刚才选中的所有尺寸线都移动到这个图层即可 ...

  4. NVIDIA---CUDA

    http://en.wikipedia.org/wiki/CUDA CUDA From Wikipedia, the free encyclopedia     CUDA Developer(s) N ...

  5. hive:Access denied for user &#39;root&#39;@&#39;%&#39;

    配置hive全分布模式时候,在mysql里面创建用户:create user 'hive' identified by 'hive'; 然后给hive帐号分配全部权限: grant all privi ...

  6. A&DCTF

    ADCTF  WRITEUP 方向:Reverse  解题数:2 题目:Reverse_01 解题过程: 用ida打开反汇编查看代码,看main函数发现 关键部分,字符串比较,竟然是直接比较”is_t ...

  7. JAVA WEB学习笔记(三):简单的基于Tomcat的Web页面

    注意:每次对Tomcat配置文件进行修改后,必须重启Tomcat 在E盘的DATA文件夹中创建TomcatDemo文件夹,并将Tomcat安装路径下的webapps/ROOT中的WEB-INF文件夹复 ...

  8. spi flash 操作

    W25Q16V 是华邦出的一颗 spi flash. 25系列是比较通用的一个系列. 后面的数字 16 跟容量有关, 16 表示 16Mbits, 相当于 2MB. 与此类似的还有 W25Q128V ...

  9. stm32GPIO8种模式

    stm32GPIO工作模式及用途  1.浮空输入GPIO_IN_FLOATING ——浮空输入,可以做KEY识别,RX1       2.带上拉输入GPIO_IPU——IO内部上拉电阻输入       ...

  10. 初始化master节点时,日志内容分析

    root@master:~/code/shell# kubeadm init --image-repository registry.aliyuncs.com/google_containers ++ ...