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. [在读]webkit技术内幕

    看了一点点,还不适合目前的我看,搁置.

  2. 搜狐前端css常用命名

  3. 在nginx上部署vue项目(history模式)--demo实列;

    在很早之前,我写了一篇 关于 在nginx上部署vue项目(history模式) 但是讲的都是理论,所以今天做个demo来实战下.有必要让大家更好的理解,我发现搜索这类似的问题还是挺多的,因此在写一篇 ...

  4. SQL 列拼接使用

    一个产品收藏表 Collection , 把该产品被收藏的人拼接在一列中如下: SQL SERVER SELECT ProjectID, UserIDs = ','+(STUFF((SELECT ', ...

  5. JS防止页面被其他网站iframe使用方法

    if(window.top !== window.self){ window.top.location = window.location;} 这句话的意识是说:如果当前窗体不是顶级窗体,就把自己变成 ...

  6. 【经验总结】VS2010下建立MFC程序

    孙鑫的MFC教学视频非常不错,但是由于视频中孙鑫老师采用VC6.0版本,而现在 许多人都转向了使用VS,VS为我们生成了许多不需要的代码,这也导致在这节课的学习编程中总是遇到一些困难.那么,如何去掉这 ...

  7. MyBatis学习(四)

    前言 最近比较松懈,下班回家后也懒得学习了.今晚实在是看不下去了,争取时间学习.社会上有这么多的资源,就看谁能抢的多吧.今天就说说MyBatis的动态SQL吧 正文 动态 SQL 通常要做的事情是有条 ...

  8. 如何修改站点url

    1.config目录下的config_global.php 文件,修改:$_config['cookie']['cookiedomain'] = '.xxxxx.com';2.config目录下的co ...

  9. 作为一个iOS Developer 为什么我不用Swift?

    1.开始 在去年这个时候接手了一个iOS项目,项目主用Swift语言进行开发,对于部分第三方Objective C开源库则使用bridge的方式进行调用 当时项目的规模大概是不超过15个页面,功能也比 ...

  10. nvidia 的一些命令

    直接在命令行使用 NVIDIA-smi会有问题 首先要确保电脑下了cuda. 然后打开cmd,使用cd命令进入: C:\Program Files\NVIDIA Corporation\NVSMI 然 ...