UVA 10692 Huge Mod

Problem X


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,
... , aN would
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^...^aN mod
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

题意:求出 a0^a1^a2......a^n%m的值。

sl: 以前做过一个a^B mod 1e9+7 的题,那个很显然是费马小定理。碰见这个题目傻逼了。

百度学习一翻知:A^x=A^(x%phi[[m]+phi[m]) (phi[m]<=x)   很显然一个递归的式子。

哎,但是当时每次都是对x%phi[MOD] 傻叉了。应该递归求解。

坑了我4个点真吭。。。

时间: 2024-10-06 02:50:09

UVA 10692 Huge Mod的相关文章

UVA 10692 - Huge Mods(数论)

UVA 10692 - Huge Mods 题目链接 题意:求a0a1a2...mod m 思路:直接算肯定不行,利用欧拉定理ab=a(b mod phi(m) + phi(m))(b>=phi(m)),对指数进行降值处理,然后就可以利用快速幂去计算了,计算过程利用递归求解. 代码: #include <stdio.h> #include <string.h> const int N = 1005; int phi[N * 10], vis[N * 10], m, n, a[

UVA - 10692 Huge Mods (欧拉函数)

Problem X 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 ex

uva 10692 Huge Mods 超大数取模

vjudge上题目链接:Huge Mods 附上截图: 题意不难理解,因为指数的范围太大,所以我就想是不是需要用求幂大法: AB % C = AB % phi(C) + phi(C) % C ( B > phi(C) ) 呢?后来发现确实需要用到,而且因为它有很多重指数,所以需要 dfs,深搜到最后一层后才返回,每次向上一层返回用求幂公式处理好的指数,然后本层用同样的原理去处理好当前层取模的值,并向上一层返回.欧拉函数预处理即可,这题的结束也有点卡人,我是用输入挂来处理的. 1 #include

UVa 10692 Huge Mods

方法:数论 其实不是很明白,为什么这个公式可行 a^b % m = a^(b%phi[m] + phi[m]) % m code: 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <string> 6 #include <vector> 7 #include <stack> 8

UVA 10692 Huge Mods(指数循环节)

指数循环节,由于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&l

E - Huge Mods (UVA - 10692)

- 题目大意 如题目中所说一样,求计算a1^a2^a3^a4......^an模m的值. - 解题思路 利用欧拉降幂公式,和欧拉函数即可解决. - 代码 #include<iostream> #include<cstring> using namespace std; long long b; long long num[10000]; long long powMod(long long a, long long n,long long p) { long long ans =

Uva 10629 Huge Mods (指数循环节)

题意:求 a1?a2?a3?. . .?aN mod m 思路:利用 和递归求解 代码: #include <iostream> #include <string.h> #include <stdio.h> using namespace std; const int N=15; typedef long long ll; int MOD; int A[N],k; int phi(int n) { int rea = n; for(int i=2; i*i<=n;

数论小结1.

从白书上面的习题开始版切....但是发现时间不够了.... 今天粗略的做了不少题目.....但是有些题目感觉实在是比较诡异.......感觉考的话也不太可能...不过还是写下来吧. 1.Uva 10673 已知x, k, 求 x = p * floor(x / k) + q * ceil(x / k).的解. 思路: 其实分类讨论就 ok 了. 当 x % k == 0 时, 取 p = x * k, q = 0; 否则, floor 与 ceil 相差 1. 怎么做? 你懂得. 2. Uva

[ACM] SDUT 2883 Hearthstone II (第二类Stiring数)

Hearthstone II Time Limit: 2000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 The new season has begun, you have n competitions and m well prepared decks during the new season. Each competition you could use any deck you want, but each of the decks must