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. chrome浏览器 配置开机启动全屏(看板app模式设置)

        chrome浏览器 配置开机启动全屏(看板app模式设置) 1.下载安装chrome浏览器. 2.建立一个chrome浏览器的快键方式,右键打开属性,如下图: 3.将目标选项卡的值修改为:&q ...

  2. Jenkins忘记管理员密码处理

    1.先找到jenkins安装目录打开config.xml文件. 2.然后编辑,删除以下部分: <useSecurity>true</useSecurity> <autho ...

  3. Python 变量作用域 LEGB (上)—— Local,Global,Builtin

    Python 变量作用域的规则是 LEGB LEGB含义解释:L —— Local(function):函数内的名字空间E —— Enclosing function locals:外部嵌套函数的名字 ...

  4. graph-Kruskal-algorithm

    并查集是一种树型的数据结构,用于处理一些不相交集合(Disjoint Sets)的合并及查询问题.主要操作:1. 初始化:每个点所在集合初始化为其自身.2. 查找:查找元素所在的集合,即根节点.3. ...

  5. Java观察者模式(Observer)

    一.定义 观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象.这个主题对象在状态上发生变化时,会通知所有观察者对象,让他们能够自动更新自己.主要应用在java的AWT事件机制 ...

  6. MySQL安装与配置介绍

    MySQl介绍 官方站点:http://www.mysql.com/ MySQL是一个开放源码的小型关联式数据库管理系统.目前MySQL被广泛地应用在Internet上的中小型网站中.由于其体积小.速 ...

  7. Mysql显示所有数据库

    show databases; mysql> show databases; +--------------------+ | Database | +--------------------+ ...

  8. #1 add life to static pages && connect to MySQL

    由于实验室 Project 中需要用到PHP, 之前也没接触过 PHP, 因此把 编程入门 <Head Fist PHP & MySQL >找来花了四五天快速过了一遍. 现在想把书 ...

  9. 下拉列表 Spinner

    在Web开发中,HTML提供了下拉列表的实现,就是使用<select>元素实现一个下拉列表,在其中每个下拉列表项使用<option>表示即可.这是在Web开发中一个必不可少的交 ...

  10. jmeter返回的Unicode转换成utf8

    该问题通过查找资料借鉴前辈门的经验得到了解决,记录下来是为了后面能够用到 最近发现有些接口返回的时unicode类型的,如下图所示: 因此只需要在jmeter中添加后置处理器:BeanShell Po ...