指数循环节,由于a ^x = a ^(x % m + phi(m)) (mod m)仅在x >= phi(m)时成立,故应注意要判断

//by:Gavin http://www.cnblogs.com/IMGavin/
//指数循环节 递归处理
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
using namespace std;
typedef long long LL;
const int N = 10008, INF = 0x3F3F3F3F;
LL a[N], mod, n;
int phi[N];
void allPhi(int n){
memset(phi,0,sizeof(phi));
phi[1]=1;
for(int i=2;i<=n;i++){
if(!phi[i]){//则i为素数
phi[i]=i;
for(int j=i;j<=n;j+=i){
if(!phi[j]){
phi[j]=j;
}
phi[j]=phi[j]/i*(i-1);
}
}
}
}
LL PowMod(LL a,LL b,LL MOD){
LL ret=1;
while(b){
if(b&1){
ret = ret * a % MOD;
}
a = a * a % MOD;
b>>=1;
}
return ret;
} bool check(LL a, LL n, LL m){
if(a == 1){
return false;
}
LL ans = 1;
for(int i = 0; i < n; i++){
ans *= a;
if(ans >= m){
return true;
}
}
return false;
} LL dfs(LL d, LL m, bool &sym){
if(d == n){
if(a[d] >= m){
sym = 1;
}else{
sym = 0;
}
return a[d] % m;
}
bool flag;
LL p = dfs(d + 1, phi[m], flag);
if(flag){
p += phi[m];
}
sym = check(a[d], p, m);
return PowMod(a[d], p, m);
} int main(){
allPhi(N - 2);
int t = 0;
while(cin >> mod){
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
bool flag;
printf("Case #%d: %lld\n", ++t, dfs(1, mod, flag) % mod);
}
return 0;
}

  

UVA 10692 Huge Mods(指数循环节)的更多相关文章

  1. uva 10692 - Huge Mods(数论)

    题目链接:uva 10692 - Huge Mods 题目大意:给出一个数的次方形式,就它模掉M的值. 解题思路:依据剩余系的性质,最后一定是行成周期的,所以就有ab=abmod(phi[M])+ph ...

  2. uva 10692 Huge Mods 超大数取模

    vjudge上题目链接:Huge Mods 附上截图: 题意不难理解,因为指数的范围太大,所以我就想是不是需要用求幂大法: AB % C = AB % phi(C) + phi(C) % C ( B ...

  3. hdu 2837 Calculation 指数循环节套路题

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

  4. HDU 4335 What is N?(指数循环节)题解

    题意: 询问有多少数\(n\)满足\(n^{n!}\equiv b\mod p \land\ n\in[1,M]\),数据范围:\(M\leq2^{64}-1,p\leq1e5\) 思路: 这题显然要 ...

  5. hdu 5895 Mathematician QSC 指数循环节+矩阵快速幂

    Mathematician QSC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  6. UVA 10692 Huge Mod

    Problem X Huge Mod Input: standard input Output: standard output Time Limit: 1 second The operator f ...

  7. 指数循环节 求A的B次方模C

    phi(c)为欧拉函数, 欧拉定理 : 对于互质的正整数 a 和 n ,有 aφ(n)  ≡ 1 mod n  . A^x = A^(x % Phi(C) + Phi(C)) (mod C) (x & ...

  8. 指数循环节&欧拉降幂

    证明:https://www.cnblogs.com/maijing/p/5046628.html 注意使用条件(B的范围) 例题: FZU1759 HDU2837 ZOJ1674 HDU4335

  9. HDU2837 Calculation(指数循环节)题解

    题意: 已知\(f(0)=1,f(n)=(n\%10)^{f(n/10)}\),求\(f(n)\mod m\) 思路: 由扩展欧拉定理可知:当\(b>=m\)时,\(a^b\equiv a^{b ...

随机推荐

  1. 构造函数忘记new? 看这里看这里

    方法一:自调用构造函数 function Person(name){ if( !(this instanceof Person)){//一定要放在开头.检查this是否为构造函数的一个实例 retur ...

  2. Download Excel file with Angular

    源码连接(编写中) 用Angular下载后台返回的Excel文件,用Blob实现,引用FileSaver.js 后台C#代码: [WebMethod] public static byte[] Cal ...

  3. ruby 基础知识(二)

    ruby  中的动态方法 http://singleant.iteye.com/blog/1680382 Rails 大量使用了符号(symbol).符号看上去很像变量名,不过以冒号作为前缀.符号的例 ...

  4. JavaScript闭包浅谈

    ------------------- 作者:willingtolove: 本文链接:http://www.cnblogs.com/willingtolove/p/4745889.html 1. 变量 ...

  5. Jquery 小技巧

    [每个程序员都会的35个jQuery的小技巧]收集的35个jQuery的小技巧/代码片段,可以帮你快速开发

  6. struts.xml

    <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC    " ...

  7. easyui textbox 添加 onblur 失去焦点事件

    由于textbox不能触发onblur事件,需要换种方式解决问题,方案如下: <input type="text" class="easyui-textbox&qu ...

  8. BugFree 测试管理系统

    测试经理 分配 每个人的账号 新建bug -> bug已修复 -> 已验证 -> closed

  9. 深入理解android:id以及@+id/name和@id/name的区别联系

    今天为了好好研究了下@+id/name和@id/name的区别以及联系,又翻了翻文档/guide/topics/resources/layout-resource.html中关于 android:id ...

  10. jquery_DOM笔记3

    css 操作: css()设置或者返回匹配元素的样式 height() 设置或者返回匹配元素的高度 width() 设置或者返回匹配元素的宽度 offset()设置或者返回匹配元素相对于文档的相对位置 ...