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 ...
随机推荐
- JS节点操作(JS原生+JQuery)
JavaScript与JQuery节点操作 节点关系与类型 任何HTML元素,都有nodeType属性.值有1~12,常用的有: 1.元素节点 2.文本节点 8.注释节点 9.document节点 ...
- centos7重启后/etc/resolv.conf 被还原解决办法
每次重启服务器后,/etc/resolv.conf文件就被自动还原了,最后发现是被Network Manager修改了. 查看Network Manager服务状态 systemctl status ...
- 双击内容变input框可编辑,失去焦点后修改的数据异步提交
<html> <head> <meta charset="utf8"> <script src="https://cdn.boo ...
- [译]The Python Tutorial#8. Errors and Exceptions
[译]The Python Tutorial#Errors and Exceptions 到现在为止都没有过多介绍错误信息,但是已经在一些示例中使用过错误信息.Python至少有两种类型的错误:语法错 ...
- BZOJ 2243 染色 树链剖分
题意: 给出一棵树,每个顶点上有个颜色\(c_i\). 有两种操作: C a b c 将\(a \to b\)的路径所有顶点上的颜色变为c Q a b 查询\(a \to b\)的路径上的颜色段数,连 ...
- TextView设置缩略显示
1.代码设置 textview.setSingleLine(); textview.setEllipsiz(TextUtils.TruncateAt.valueOf("END")) ...
- loj2276 「HAOI2017」新型城市化
给出的图是一个二分图(显然--吗),一个图的最大团=其补图的最大独立集,因此二分图的最大独立集就是补图的最大团. 欲使补图最大团变大,则要最大独立集变大.二分图最大独立集=点数-最小点覆盖.最小点覆盖 ...
- ptyhon - 接口自动化测试实战case1
work_20181203_httprequest.py: import requestsclass http_request: def http_get(url,params): res = req ...
- selenium - 常用等待操作
# 4. 等待操作 # 强制等待from time import sleepsleep(10) # 隐性等待# 设置最长等待时间,在这个时间在只要有个时间点加载完成,则执行下一步代码,比sleep智能 ...
- Nginx从入门到放弃-第2章 基础篇
2-1 什么是Nginx 2-2 常见的中间件服务 2-3 Nginx的特性_实现优点1 2-4 Nginx特性_实现优点2 2-5 Nginx特性_实现优点3 2-6 Nginx特性_实现优点4 2 ...