题意

Language:Default
The Luckiest number
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7083 Accepted: 1886

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

Source

分析

\[\because L | \frac{8(10^x-1)}{9} \\
\therefore 9L | 8(10^x-1) \\
\therefore \frac{9L}{\gcd(L,8)} | 10^x-1 \\
\therefore 10^x \equiv 1 (\bmod \frac{9L}{\gcd(L,8)}) \\
\because a^x \equiv 1 (\bmod n) \rightarrow \gcd(a,n)=1,x|\varphi(n) \\
\therefore x|\varphi(\frac{9L}{\gcd(L,8)})
\]

所以枚举即可。时间复杂度\(O(\sqrt{L} \log L)\)

代码

#include<iostream>
#include<cmath>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
rg T data=0,w=1;
rg char ch=getchar();
while(!isdigit(ch)){
if(ch=='-') w=-1;
ch=getchar();
}
while(isdigit(ch))
data=data*10+ch-'0',ch=getchar();
return data*w;
}
template<class T>il T read(rg T&x){
return x=read<T>();
}
typedef long long ll; ll L,d,k,p,s,i,num=0;
ll gcd(ll x,ll y){
return y?gcd(y,x%y):x;
}
ll ksc(ll a,ll b,ll c){
ll ans=0;
while(b){
if(b&1) ans=(ans+a)%c;
a=a*2%c;
b>>=1;
}
return ans;
}
ll ksm(ll a,ll b,ll c){
ll ans=1%c;
a%=c;
while(b){
if(b&1) ans=ksc(ans,a,c);
a=ksc(a,a,c);
b>>=1;
}
return ans;
}
ll phi(ll n){
ll ans=n;
for(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*(n-1);
return ans;
}
ll number(){
d=gcd(L,8);
k=9*L/d;
if(gcd(k,10)!=1) return 0;
p=phi(k);
s=sqrt((double)p);
for(i=1;i<=s;++i)
if(p%i==0&&ksm(10,i,k)==1)
return i;
for(i=s-1;i;--i)
if(p%i==0&&ksm(10,p/i,k)==1)
return p/i;
return 0;
}
int main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
while(read(L)) printf("Case %lld: %lld\n",++num,number());
return 0;
}

POJ3696 The Luckiest number的更多相关文章

  1. POJ3696:The Luckiest number(欧拉函数||求某数最小的满足题意的因子)

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

  2. [POJ3696]The Luckiest number(数论)

    题目:http://poj.org/problem?id=3696 题意:给你一个数字L,你要求出一个数N,使得N是L的倍数,且N的每位数都必须是8,输出N的位数(如果不存在输出0) 分析: 首先我们 ...

  3. POJ3696 The Luckiest Number 欧拉定理

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

  4. poj 3696 The Luckiest Number

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

  5. POJ_3696 The Luckiest number 【欧拉定理+同余式+对取模的理解】

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

  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. 「POJ3696」The Luckiest number【数论,欧拉函数】

    # 题解 一道数论欧拉函数和欧拉定理的入门好题. 虽然我提交的时候POJ炸掉了,但是在hdu里面A掉了,应该是一样的吧. 首先我们需要求的这个数一定可以表示成\(\frac{(10^x-1)}{9}\ ...

随机推荐

  1. java 的异常处理

    一.异常的概念: java 中的异常通常指的是在运行期出现的错误,这样的错误也是比较难以调试的,解决问题的时候注意观察出现错误的名字和行号最重要 下面看这个例子: import java.io.*; ...

  2. Java加密代码 转换成Net版

    java版本自己封装base64 package com.qhong; import java.io.UnsupportedEncodingException; import org.apache.c ...

  3. luogu P1162 填涂颜色

    https://www.luogu.org/problem/show?pid=1162 //其实很简单的吧 //就是最外圈加一圈0 ,然后把外圈里面的0都遍历了 //剩下的0 就变成2 就行了 #in ...

  4. SpringBoot使用Redis数据库

    (1)pom.xml文件引入jar包,如下: <dependency> <groupId>org.springframework.boot</groupId> &l ...

  5. Python 个人笔记(一)

    csv文件读取 使用csv标准库模块对csv文件进行读写 如下,读取名为filename的csv文件. 其中第一行为表头的列名,从第二行开始为数据内容(假设有两列). import csv with ...

  6. callee 与 caller

    arguments.callee 在函数内部指向函数本身 1.函数调用 function sum (num){ if(num <= 1){ return 1; }else{ return num ...

  7. 解决 android.support.v7.widget.GridLayout 使用 xmlns:app 出现 error 的问题

    GridLayout 是在 Android API Level 14 加进来的 它可用来取代 TableLayout 也提供了自由度较大且实用的排版功能 为了兼容 4.0 以下的较低版本 Androi ...

  8. 各种排序算法思想复杂度及其java程序实现

    一.冒泡排序(BubbleSort)1. 基本思想: 设排序表长为n,从后向前或者从前向后两两比较相邻元素的值,如果两者的相对次序不对(A[i-1] > A[i]),则交换它们, 其结果是将最小 ...

  9. windows下的IO模型之异步选择(WSAAsyncSelect)模型

    异步选择(WSAAsyncSelect)模型是一个有用的异步I/O 模型.其核心函数是WSAAsyncSelect,该函数是非阻塞的 (关于异步io的理解详情可以看:http://www.cnblog ...

  10. UVA-1572 Self-Assembly (图+拓扑排序)

    题目大意:每条边上都有标号的正方形,两个正方形能通过相匹配的边连接起来,每种正方形都有无限多个.问能否无限延展下去. 题目分析:将边视为点,正方形视为边,建立无向图,利用拓扑排序判断是图否为DAG. ...