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. SQLite C/C++ 教程

    目录 1安装 2 C/C++ Interface APIs 3连接到数据库 4创建表 5插入操作 6更新操作 7删除操作 安装 在我们开始使用SQLite在C / C++程序,我们需要确保SQLite ...

  2. UVA116 Unidirectional TSP 单向TSP

    分阶段的DAG,注意字典序的处理和路径的保存. 定义状态d[i][j]为从i,j 出发到最后一列的最小花费,转移的时候只有三种,向上,向下,或平移. #include<bits/stdc++.h ...

  3. 并查集+思维——X-Plosives

    一.问题描述(题目链接) 有n种化合物,每种化合物由两种元素组成.当几种的化合物数量等于他们所含不同元素的数量时,就会发生爆炸.现在依次给出化合物的组成,当新的化合物与之前的化合物放在一起会发生爆炸时 ...

  4. Use-After-Free

    0x00 UAF利用原理 uaf漏洞产生的主要原因是释放了一个堆块后,并没有将该指针置为NULL,这样导致该指针处于悬空的状态(这个指针可以称为恶性迷途指针),同样被释放的内存如果被恶意构造数据,就有 ...

  5. PHP 腾讯云cos使用之我见

    因为某些人的原因,本文从新改名发布一遍. 原名称:tp5 -- 腾讯云cos简单使用 原文链接:https://www.cnblogs.com/YFYQ/p/10840050.html 因项目需要,本 ...

  6. Web服务器☞Apache VS Nginx

    Web服务器☞Apache VS Nginx LZ最近公司有一个项目在Web服务器选型上,在Apache和Nginx之间引起了一些讨论.这两者目前都是业内优秀的web服务器,都实现了HTTP1.1协议 ...

  7. ios runloop学习

    今天突然才之间才意识到NSTimer这样的运行方式,是在多线程中实现的循环还是在主线程中去实现的呢.当然不可能是在主线程中的while那么简单,那样什么都干不了,简单看了下NSTimer是以同步方式运 ...

  8. django-ckeditor添加代码功能(codesnippet)

    最近做了一个博客,使用python3+django2.1开发的,后台编辑器和前端显示用的Django-ckeditor富文本编辑器,由于发现没有代码块功能,写上去的代码在前端展示有点乱,于是一顿问度娘 ...

  9. 并查集--poj 2492

    Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...

  10. 【转】VS2017的VSIX插件开发

    最近从头开发了一遍一个VSIX的插件,用于调测的一个工具: 特此把相关的过程经验记录下来: 第一步:建立工程 1.      首先是安装上: 需要安装Visual Studio SDK,这个在安装VS ...