Huge Mod

Input: standard input

Output: standard output

Time Limit: 1 second

The operator for exponentiation is different from the addition, subtraction, multiplication or division operators in the sense that the default associativity for exponentiation goes right to left instead of left to right. So unless we mess it up by placing parenthesis,  should mean  not . This leads to the obvious fact that if we take the levels of exponents higher (i.e., 2^3^4^5^3), the numbers can become quite big. But let's not make life miserable. We being the good guys would force the ultimate value to be no more than 10000.

Given a1, a2, a3, ... , aN and m(=10000) you only need to compute a1^a2^a3^...^aN mod m.

Input

There can be multiple (not more than 100) test cases. Each test case will be presented in a single line. The first line of each test case would contain the value for M(2<=M<=10000). The next number of that line would be N(1<=N<=10). Then N numbers - the values for a1, a2, a3, ... , aNwould follow. You can safely assume that 1<=ai<=1000. The end of input is marked by a line containing a single hash ('#') mark.

Output

For each of the test cases, print the test case number followed by the value of a1^a2^a3^...^aNmod m on one line. The sample output shows the exact format for printing the test case number.

Sample Input

Sample Output

10 4 2 3 4 5
100 2 5 2
53 3 2 3 2
#
Case #1: 2
Case #2: 25
Case #3: 35

题目大意:求一个数((((a^b)^c)^d)^e)..... Mod m的值

幂太huge了,上界是1000^1000^1000^1000^1000^1000^1000^1000^1000,暴力快速幂模肯定行不通,因为幂是多少都难的计算。有公式a^x=a^(x%phi(c)+phi(c)) (mod c),所以可以用递归方法求解。

AC代码:

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
int phi[];
int f[],n;
string m; void init()
{
int i;
for(i=;i<=;i++) phi[i]=;
phi[]=;
for(i=;i<=;i++)
if(!phi[i])
for(int j=i;j<=;j+=i)
{
if(!phi[j]) phi[j]=j;
phi[j]=phi[j]/i*(i-);
}
} int montgomery(int a,int b,int c)
{
int t=;
while(b)
{
if(b%)
t=t*a%c;
b/=;
a=a*a%c;
}
return t;
} int dfs(int now,int mod)
{
if(now==n-)
{
return f[now]%mod;
}
int t=dfs(now+,phi[mod]);
int ans=montgomery(f[now],t+phi[mod],mod);
return ans;
}
int main()
{
init();
int i,ret,kase=;
while(cin>>m,m!="#")
{
ret=;
for(i=;i<m.size();i++)
ret=ret*+m[i]-'';
cin>>n;
for(int i=;i<n;i++)
scanf("%d",f+i);
cout<<"Case #"<<kase++<<": ";
printf("%d\n",dfs(,ret));
}
return ;
}

uva 10692 高次幂取模的更多相关文章

  1. HDU1452Happy 2004(高次幂取模+积性函数+逆元)

    题目意思:2004^x的所有正因数的和(S)对29求余:输出结果: 原题链接 题目解析:解析参照来源:点击打开链接 因子和 6的因子是1,2,3,6; 6的因子和是s(6)=1+2+3+6=12; 2 ...

  2. UVa 11582 (快速幂取模) Colossal Fibonacci Numbers!

    题意: 斐波那契数列f(0) = 0, f(1) = 1, f(n+2) = f(n+1) + f(n) (n ≥ 0) 输入a.b.n,求f(ab)%n 分析: 构造一个新数列F(i) = f(i) ...

  3. uva 10710 快速幂取模

    //题目大意:输入一个n值问洗牌n-1次后是不是会变成初始状态(Jimmy-number),从案例可看出牌1的位置变化为2^i%n,所以最终判断2^(n-1)=1(mod n)是否成立#include ...

  4. UVa 11582 Colossal Fibonacci Numbers! 【大数幂取模】

    题目链接:Uva 11582 [vjudge] watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fil ...

  5. UVa 11582 巨大的斐波那契数!(幂取模)

    https://vjudge.net/problem/UVA-11582 题意: 输入两个非负整数a.b和正整数n,你的任务是计算f(a^b)除以n的余数.f[0]=0,f[1]=1,f[i+2]=f ...

  6. UVA 11609 - Teams 组合、快速幂取模

    看题传送门 题目大意: 有n个人,选一个或者多个人参加比赛,其中一名当队长,如果参赛者相同,队长不同,也算一种方案.求一共有多少种方案. 思路: 排列组合问题. 先选队长有C(n , 1)种 然后从n ...

  7. 组合数取模Lucas定理及快速幂取模

    组合数取模就是求的值,根据,和的取值范围不同,采取的方法也不一样. 下面,我们来看常见的两种取值情况(m.n在64位整数型范围内) (1)  , 此时较简单,在O(n2)可承受的情况下组合数的计算可以 ...

  8. 【转】C语言快速幂取模算法小结

    (转自:http://www.jb51.net/article/54947.htm) 本文实例汇总了C语言实现的快速幂取模算法,是比较常见的算法.分享给大家供大家参考之用.具体如下: 首先,所谓的快速 ...

  9. HDU 1061 Rightmost Digit --- 快速幂取模

    HDU 1061 题目大意:给定数字n(1<=n<=1,000,000,000),求n^n%10的结果 解题思路:首先n可以很大,直接累积n^n再求模肯定是不可取的, 因为会超出数据范围, ...

随机推荐

  1. jquery 不支持$.browser

    if (!$.browser) { $.browser = { mozilla : /firefox/.test(navigator.userAgent.toLowerCase()), webkit ...

  2. ssh复制remote

    rsync rsync localdirectory username@10.211.55.4:/home/username/Downloads/localdirectory -r

  3. WPF知识点全攻略04- XAML页面布局

    名称 说明 Canvas 使用固定坐标绝对定位元素 StackPanel 在水平或竖直方向放置元素 DockPanel 根据外部容器边界,自动调整元素 WrapPanel 在可换行的行中放置元素 Gr ...

  4. webpack 使用总结

    参考:http://www.ferecord.com/webpack-summary.html#base64 写的比较详细了

  5. 经典的7种排序算法 原理C++实现

    排序是编程过程中经常遇到的操作,它在很大程度上影响了程序的执行效率. 7种常见的排序算法大致可以分为两类:第一类是低级排序算法,有选择排序.冒泡排序.插入排序:第二类是高级排序算法,有堆排序.排序树. ...

  6. DP入门练习

    T1 题目:codevs4815江哥的dp题a codevs4815 一个简单的DP,注意开long long(不然会全WA),以及初始条件(这题有负数,所以要把f设成极小值.还要保证转移正确). # ...

  7. 如何用纯 CSS 创作一个菱形 loader 动画

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/eKzjqK 可交互视频教 ...

  8. vscode and python

    http://robotkang.cc/2017/04/VS-Code-%E9%85%8D%E7%BD%AEPython/

  9. PHP 接入支付宝即时到账功能

    首先请到支付宝那边申请一个及时到账的接口账户,需要提交相关材料申请.然后根据即时到账的API文档进行接入.API文档提供了各种语言版本的demo,我这里是下的php版demo,然后再进行相关修改操作. ...

  10. SPOJ DIVSUM - Divisor Summation

    DIVSUM - Divisor Summation #number-theory Given a natural number n (1 <= n <= 500000), please ...