The Luckiest number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 980    Accepted Submission(s): 301

Problem 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
做这个题的前提有两个公式:1.(a/b)%mod = a%(b*mod)/b%mod  2.待证明的公式: ax%b = 0 => a%(b/gcd(x,b)) 详情参见我的上一篇博客
这个题我们可以化成 (8*(10^k-1)/9)%L = 0 ---->  8*(10^k-1)%(9*L) 求出 d = gcd(9*L)
然后根据2化成 (10^k-1) % (9*L/d) => (10^k)%(9*L) = 1 又可以根据欧拉定理来求解了。。和我的上一题差不多。。但是这个题更坑,pow_mod是满足不了精度的,总之
参考了n多博客和资料才弄出这题。。高精度快速幂模模板get
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;
typedef long long LL;
LL e[][];
LL phi(LL x)
{
LL ans=x;
for(LL i=; i*i<=x; i++)
if(x%i==)
{
ans=ans/i*(i-);
while(x%i==) x/=i;
}
if(x>)
ans=ans/x*(x-);
return ans;
}
LL gcd(LL a,LL b)
{
return b==?a:gcd(b,a%b);
}
void devide(LL ans,int &id)
{
for(LL i=; i*i<=ans; i++) ///分解质因数
{
if(ans%i==)
{
e[id][]=i;
e[id][]=;
while(ans%i==) ans/=i,e[id][]++;
id++;
}
}
if(ans>)
{
e[id][]=ans;
e[id++][]=;
}
}
LL modular_multi(LL a, LL b, LL c) {/// a * b % c
LL res, temp;
res = , temp = a % c;
while (b) {
if (b & ) {
res += temp;
if (res >= c) {
res -= c;
}
}
temp <<= ;
if (temp >= c) {
temp -= c;
}
b >>= ;
}
return res;
}
LL modular_exp(LL a, LL b, LL c) { ///a ^ b % c 改成mod_pow就不行,中间发生了溢出,还是这个模板靠谱
LL res, temp;
res = % c, temp = a % c;
while (b) {
if (b & ) {
res = modular_multi(res, temp, c);
}
temp = modular_multi(temp, temp, c);
b >>= ;
}
return res;
}
int main()
{
LL l;
int t= ;
while(~scanf("%lld",&l),l)
{
printf("Case %d: ",t++);
LL d = gcd(,*l);
LL a = *l/d;
if(gcd(a,)!=){
printf("0\n");
}else{
LL ans = phi(a);
int id = ;
devide(ans,id);
for(int i=;i<id;i++){
for(int j=;j<e[i][];j++){
if(modular_exp(,ans/e[i][],a)==) ans/=e[i][];
}
}
printf("%lld\n",ans);
}
}
return ;
}
 

hdu 2462(欧拉定理+高精度快速幂模)的更多相关文章

  1. HDU 4704 Sum (高精度+快速幂+费马小定理+二项式定理)

    Sum Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u Submit Status  ...

  2. hdu 4602 递推关系矩阵快速幂模

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

  3. HDU 1061 Rightmost Digit --- 快速幂取模

    HDU 1061 题目大意:给定数字n(1<=n<=1,000,000,000),求n^n%10的结果 解题思路:首先n可以很大,直接累积n^n再求模肯定是不可取的, 因为会超出数据范围, ...

  4. URAL 1141. RSA Attack(欧拉定理+扩展欧几里得+快速幂模)

    题目链接 题意 : 给你n,e,c,并且知道me ≡ c (mod n),而且n = p*q,pq都为素数. 思路 : 这道题的确与题目名字很相符,是个RSA算法,目前地球上最重要的加密算法.RSA算 ...

  5. hdu 5187(高精度快速幂)

    zhx's contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  6. HDU.2640 Queuing (矩阵快速幂)

    HDU.2640 Queuing (矩阵快速幂) 题意分析 不妨令f为1,m为0,那么题目的意思为,求长度为n的01序列,求其中不含111或者101这样串的个数对M取模的值. 用F(n)表示串长为n的 ...

  7. codeforces magic five --快速幂模

    题目链接:http://codeforces.com/contest/327/problem/C 首先先算出一个周期里面的值,保存在ans里面,就是平常的快速幂模m做法. 然后要计算一个公式,比如有k ...

  8. 快速幂模n运算

    模运算里的求幂运算,比如 5^596 mod 1234, 当然,直接使用暴力循环也未尝不可,在书上看到一个快速模幂算法 大概思路是,a^b mod n ,先将b转换成二进制,然后从最高位开始(最高位一 ...

  9. HDU 5667 构造矩阵快速幂

    HDU 5667 构造矩阵快速幂 题目描述 解析 我们根据递推公式 设 则可得到Q的指数关系式 求Q构造矩阵 同时有公式 其中φ为欧拉函数,且当p为质数时有 代码 #include <cstdi ...

随机推荐

  1. 洛谷 1571 眼红的Medusa

    洛谷 1571 眼红的Medusa 虽说这道题标签里写明了二分,然而我还是首先想到了map......毕竟map真的是简单好写. map做法 #include<bits/stdc++.h> ...

  2. 消息队列之 Kafka

    转 https://www.jianshu.com/p/2c4caed49343 消息队列之 Kafka 预流 2018.01.15 16:27* 字数 3533 阅读 1114评论 0喜欢 12 K ...

  3. Golang ioutil读写文件测试

    运用 ioutil.ReadFile .ioutil.WriteFile package main import ( "io/ioutil" "log" &qu ...

  4. 小程序电脑调试没有问题,真机预览报错fail hand shake error

    今天在做小程序的过程中使用HTTPS请求数据时,遇到安卓机型无法获取到数据,通过一系列的排查,发现是因为ssl证书的问题,后来通过https://www.myssl.cn/tools/check-se ...

  5. ACM 广度优化搜索算法总结

    广度优化搜索算法的本质:要求每个状态不能重复,这就需要我们:第一次先走一步可以到达的状态,如果还没有找到答案,就需要我们走到两步可以到达的状态.依次下去 核心算法:队列 基本步骤:          ...

  6. 新游发布:《Don't touch the color》

    这是笨猫工作室最后一个Scratch 2.0游戏,经过笨猫工作室成员的不懈努力,游戏终于可以稳定运行.此次更新添加了最高分数显示,优化了系统流畅度.快来试玩吧!!! 卡搭蓝链:https://kada ...

  7. hdu 1011 Starship Troopers(树形背包)

    Starship Troopers Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  8. Python选修课第二届Turtle绘图大赛~~画猫猫

    (a)20161401167 夏思敏 20161401179 段梦格 (b)代码执行视频链接 点击查看:Python使用turtle库画猫猫 (c)程序源码 import turtle turtle. ...

  9. 2018天梯赛第一次训练题解和ac代码

    随着评讲的进行代码和题解会逐步放上来 2018天梯赛第一次训练 1001 : 进制转换 Time Limit(Common/Java):1000MS/10000MS     Memory Limit: ...

  10. [小技巧]使用set对列表去重,并保持列表原来顺序