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. GoogleTest 之路2-Googletest 入门(Primer)

    Why googletest? 为啥要用GoogleTest呢? googletest 是由测试技术Team 开发的带有google 特殊的需求和限制的测试框架. 不管你在什么平台上写C++代码,go ...

  2. 【php】 phpword下载文件问题

    这个问题是在 科锐国际 工作过程中发现的 word文档的名字(有汉字和空格)在windows系统上遍历是查不到文件的,但是在linux系统上市可以的压缩包里面的中文名word文档,如果出现汉字和空格, ...

  3. python之自定义排序函数sorted()

    sorted()也是一个高阶函数,它可以接收一个比较函数来实现自定义排序,比较函数的定义是,传入两个待比较的元素 x, y,如果 x 应该排在 y 的前面,返回 -1,如果 x 应该排在 y 的后面, ...

  4. LeetCode(268) Missing Number

    题目 Given an array containing n distinct numbers taken from 0, 1, 2, -, n, find the one that is missi ...

  5. Linux学习-服务器硬件数据的收集

    以系统内建 dmidecode 解析硬件配备 系统有个名为 dmidecode 的软件,它可以解析 CPU 型号.主板型号与内存相 关的型号等等~ [root@study ~]# dmidecode ...

  6. exe4j+Inno_setup打包java桌面应用

    打开exe4j,这里有个注意点,就是欢迎界面下面的License,如果没有请到网上找一个序列号,否则生成的exe打开之后都会先弹出您未激活exe4j的警告!点击下一步 这里有两个选项,第一个是通常编译 ...

  7. __block 和__weak

    1,在MRC 时代,__block 修饰,可以避免循环引用:ARC时代,__block 修饰,同样会引起循环引用问题: 2,__block不管是ARC还是MRC模式下都可以使用,可以修饰对象,还可以修 ...

  8. ubuntu linux下各种格式软件包的安装卸载

    http://www.cnblogs.com/mo-beifeng/archive/2011/08/14/2137954.html

  9. 大数据学习——akka学习

    架构图 重要类介绍 ActorSystem 在Akka中,ActorSystem是一个重量级的结构,他需要分配多个线程,所以在实际应用中,ActorSystem通常是一个单例对象,我们可以使用这个Ac ...

  10. Educational Codeforces Round 36 (Rated for Div. 2)

    A. Garden time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...