The Luckiest number

Time Limit: 1000ms
Memory Limit: 32768KB

This problem will be judged on HDU. Original ID: 2462
64-bit integer IO format: %I64d      Java class name: Main

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

 
解题:

首先,由题意可以得出,(10^x - 1)/ 9 * 8 = L * p(p是一个未知数,但必定是整数)。

然后对上式进行移项处理,得:(10^x - 1) = 9 * L * p / 8。

设m = 9 * L / gcd(L, 8),则有(10^x - 1) = m * p'。p’是必然存在的一个整数。

然后问题就转化成为了 10^x = 1(mod m),观察此式,显然,m和10必定互质。

于是根据欧拉定理,10^(Euler(m)) = 1(mod m) 。由于题目要求最小的解,解必然是Euler(m)的因子。

转自 OK_again

 #include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int maxn = ;
LL mul(LL a, LL b, LL mod) {
LL ret = ;
while(b) {
if(b&) ret = (ret + a) % mod;
a = (a<<)%mod;
b >>= ;
}
return ret;
}
LL quickPow(LL base,LL index,LL mod){
LL ret = ;
while(index){
if(index&) ret = mul(ret,base,mod);
index >>= ;
base = mul(base,base,mod);
}
return ret;
}
bool np[maxn] = {true,true};
int p[maxn],tot;
void init(){
for(int i = ; i < maxn; ++i){
if(!np[i]) p[tot++] = i;
for(int j = ; j < tot && p[j]*i < maxn; ++j){
np[p[j]*i] = true;
if(i%p[j] == ) break;
}
}
}
LL Euler(LL n){
LL ret = n;
for(int i = ; (LL)p[i]*p[i] <= n; ++i){
if(n%p[i] == ){
ret = ret/p[i]*(p[i] - );
while(n%p[i] == ) n /= p[i];
}
}
if(n > ) ret = ret/n*(n-);
return ret;
}
vector<int>F;
void Fn(LL n){
F.clear();
for(int i = ; i < tot && n > ; ++i){
while(n%p[i] == ){
n /= p[i];
F.push_back(p[i]);
}
}
if(n > ) F.push_back(n);
}
int main(){
init();
LL L;
int cs = ;
while(scanf("%I64d",&L),L){
LL m = *L/__gcd(L,8LL);
if(__gcd(m,10LL) != ){
printf("Case %d: 0\n",cs++);
continue;
}
LL x = Euler(m);
Fn(x);
for(auto it:F)
if(quickPow(,x/it,m) == ) x /= it;
printf("Case %d: %I64d\n",cs++,x);
}
return ;
}

HDU 2462 The Luckiest number的更多相关文章

  1. poj 3696 The Luckiest Number

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

  2. POJ3696 The Luckiest number

    题意 Language:Default The Luckiest number Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7 ...

  3. HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)

    HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...

  4. hdu 6216 A Cubic number and A Cubic Number【数学题】

    hdu 6216 A Cubic number and A Cubic Number[数学] 题意:判断一个素数是否是两个立方数之差,就是验差分.. 题解:只有相邻两立方数之差才可能,,因为x^3-y ...

  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 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

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

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

  9. HDU 1394 Minimum Inversion Number(线段树求最小逆序数对)

    HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意:  给一个序列由 ...

随机推荐

  1. org.codehaus.jettison.json.JSONObject使用方法

    public static void main(String[] args) { System.out.println("测试开始"); File file = new File( ...

  2. 506 Relative Ranks 相对名次

    给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌.前三名运动员将会被分别授予 “金牌”,“银牌” 和“ 铜牌”("Gold Medal", "Silve ...

  3. CentOS 6.9:ntpdate[3115]: no server suitable for synchronization found

    在做一个集群实验,克隆的虚拟机,然后使用ntpdate就抛出了错误.机器之间可以互相ping通,selinux和iptables都已经关闭. [root@Server_2 ~]# service nt ...

  4. PHP生成和获取XML格式数据

    在做数据接口时,我们通常要获取第三方数据接口或者给第三方提供数据接口,而这些数据格式通常是以XML或者JSON格式传输,本文将介绍如何使用PHP生成XML格式数据供第三方调用以及如何获取第三方提供的X ...

  5. asp.net MVC中实现调取web api

    public ActionResult Index(string city) { if (string.IsNullOrEmpty(city)) { city = "上海"; } ...

  6. 关于发布WP 8.1应用信息不匹配问题的解决办法

    错误提示:   与此更新关联的程序包标识符与已上传程序包中的标识符不匹配: The package identity associated with this update doesn't match ...

  7. mysql单向自动同步

    mysql自动同步 以下教程均使用mysql自带的自动同步功能 全库单向自动同步 本例把192.168.3.45上名称为ewater_main的数据库自动同步到192.168.3.68的ewater_ ...

  8. IOS应用

    下面是这个类的一些功能: 1.设置icon上的数字图标 //设置主界面icon上的数字图标,在2.0中引进, 缺省为0 [UIApplicationsharedApplication].applica ...

  9. DLL入门浅析【转】

     1.建立DLL动态库 动态链接库(DLL)是从C语言函数库和Pascal库单元的概念发展而来的.所有的C语言标准库函数都存放在某一函数库中.在链接应用程序的过程中,链接器从库文件中拷贝程序调用的函数 ...

  10. 迅为4412开发平台Zigbee模块在物联网智能家居中的应用

      物联网智能家居的发展物联网随着互联网的发展,可以通过互联网实现物和物的互联,就有了物联网的概念.传统家居电器 有了物联网之后,在家居电器范围中,就是通过物联网技术将家中的各种设备连接到一起,家居中 ...