hdu 2462(欧拉定理+高精度快速幂模)
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
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'.
The last test case is followed by a line containing a zero.
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.
11
16
0
Case 2: 2
Case 3: 0
#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(欧拉定理+高精度快速幂模)的更多相关文章
- HDU 4704 Sum (高精度+快速幂+费马小定理+二项式定理)
Sum Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%I64d & %I64u Submit Status ...
- hdu 4602 递推关系矩阵快速幂模
Partition Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- HDU 1061 Rightmost Digit --- 快速幂取模
HDU 1061 题目大意:给定数字n(1<=n<=1,000,000,000),求n^n%10的结果 解题思路:首先n可以很大,直接累积n^n再求模肯定是不可取的, 因为会超出数据范围, ...
- URAL 1141. RSA Attack(欧拉定理+扩展欧几里得+快速幂模)
题目链接 题意 : 给你n,e,c,并且知道me ≡ c (mod n),而且n = p*q,pq都为素数. 思路 : 这道题的确与题目名字很相符,是个RSA算法,目前地球上最重要的加密算法.RSA算 ...
- hdu 5187(高精度快速幂)
zhx's contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- HDU.2640 Queuing (矩阵快速幂)
HDU.2640 Queuing (矩阵快速幂) 题意分析 不妨令f为1,m为0,那么题目的意思为,求长度为n的01序列,求其中不含111或者101这样串的个数对M取模的值. 用F(n)表示串长为n的 ...
- codeforces magic five --快速幂模
题目链接:http://codeforces.com/contest/327/problem/C 首先先算出一个周期里面的值,保存在ans里面,就是平常的快速幂模m做法. 然后要计算一个公式,比如有k ...
- 快速幂模n运算
模运算里的求幂运算,比如 5^596 mod 1234, 当然,直接使用暴力循环也未尝不可,在书上看到一个快速模幂算法 大概思路是,a^b mod n ,先将b转换成二进制,然后从最高位开始(最高位一 ...
- HDU 5667 构造矩阵快速幂
HDU 5667 构造矩阵快速幂 题目描述 解析 我们根据递推公式 设 则可得到Q的指数关系式 求Q构造矩阵 同时有公式 其中φ为欧拉函数,且当p为质数时有 代码 #include <cstdi ...
随机推荐
- 【SAM】loj#6401. 字符串
网上有篇题解写的是线段树合并维护求值? 题目描述 有一个只包含小写字母,长度为 $n$ 的字符串 $S$ .有一些字母是好的,剩下的是坏的. 定义一个子串 $S_{l\ldots r}$是好的,当且仅 ...
- 【动态规划】bzoj1584: [Usaco2009 Mar]Cleaning Up 打扫卫生
思路自然的巧妙dp Description 有N头奶牛,每头那牛都有一个标号Pi,1 <= Pi <= M <= N <= 40000.现在Farmer John要把这些奶牛分 ...
- idea 启动不了
idea 更新了新版本. 破解步骤安排完了之后 , 发现怎么也启动不了. 没有任何提示. 于是为了查看错误信息去idea的安装目录bin下用idea.bat脚本启动发现了如下错误: 在idea6 ...
- LeetCode之Weekly Contest 90
LeetCode第90场周赛记录 第一题:亲密字符串 问题: 给定两个由小写字母构成的字符串 A 和 B ,只要我们可以通过交换 A 中的两个字母得到与 B 相等的结果,就返回 true :否则返回 ...
- Powershell 备忘
如何修改环境变量 [environment]::SetEnvironmentvariable(“path”,"xxx","user") [environment ...
- Linux异常体系之stubs_offset
转自 http://www.xuebuyuan.com/2208550.html 在ARM V4及V4T以后的大部分处理器中,中断向量表的位置可以有两个位置:一个是0x00000000,另一个是0xf ...
- excel日期格式取年份
具体思路:先将日期格式更改为常规格式,再取常规格式的前4位数字 例如:A1==1981/12/22 第一步B1=TEXT(A1,"emd") 第二步C1=LEFT(B1,4) 结束
- html--元素显示优先级
HTML元素的显示优先级 一.HTML元素的显示优先级(显示层次问题,哪个在上哪个在下!总是显示在最前面) 帧元素>HTML元素优先,表单元素总>非表单元素优先 ...
- A+B问题的异常解法
先%XZZ为敬 http://www.cnblogs.com/xzz_233/p/a-plus-b-problem.html 万恶之源:https://www.luogu.org/discuss/sh ...
- 栈的push、pop序列 【微软面试100题 第二十九题】
题目要求: 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1.2.3.4.5是某栈的压栈序列,序列4.5.3.2.1是该压栈 ...