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 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

Problem setter: Monirul Hasan, Member of Elite Problemsetters‘ Panel
Special thanks: Mohammad Sajjad Hossain

题意:输入正整数a1,a2,a3..an和模m,求a1^a2^...^an mod m

思路:需要用到一个公式:,递归处理

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
typedef long long ll;
using namespace std;
const int maxn = 1010;

int a[maxn], vis[maxn], n;
char str[maxn];

int euler_phi(int m) {
	int tmp = (int) sqrt(m+0.5);
	int ans = m;
	for (int i = 2; i <= tmp; i++) if (m % i == 0) {
		ans = ans / i * (i-1);
		while (m % i == 0)
			m /= i;
	}
	if (m > 1)
		ans = ans / m * (m-1);
	return ans;
}

int pow_mod(int a, int m, int mod) {
	int tmp = 1;
	while (m)  {
		if (m & 1)
			tmp = tmp * a % mod;
		m >>= 1;
		a = a * a % mod;
	}
	return tmp;
}

int solve(int cur, int m) {
	if (cur == n-1)
		return a[cur] % m;
	int phi_m = euler_phi(m);
	int tmp = solve(cur+1, phi_m);
	return pow_mod(a[cur], tmp + phi_m, m);
}

int main() {
	int cas = 1, mod;
	while (scanf("%s", str) != EOF && str[0] != '#') {
		mod = 0;
		for (int i = 0; str[i]; i++)
			mod = mod*10 + str[i] - '0';
		scanf("%d", &n);
		for (int i = 0; i < n; i++)
			scanf("%d", &a[i]);
		printf("Case #%d: %d\n", cas++, solve(0, mod));
	}
	return 0;
}
时间: 2024-12-09 02:53:34

UVA - 10692 Huge Mods (欧拉函数)的相关文章

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 11317 - GCD+LCM(欧拉函数+log)

题目链接:uva 11317 - GCD+LCM 题目大意:给定n,求出1~n里面两两的最大公约的积GCD和最小公倍数的积LCM,在10100进制下的位数. 解题思路:在n的情况下,对于最大公约数为i的情况又phi[n/i]次.求LCM就用两两乘积除以GCD即可. #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; ty

UVA 11440 - Help Tomisu(欧拉函数)

UVA 11440 - Help Tomisu 题目链接 题意:给定n和m,求[2,n!]中,所有质因子个数都大于m的个数 思路:?(m!)表示小于m!并与m!互质的个数,而与m!互质的个数,他的质因子肯定不包含1-m,因此就是满足条件的.然后对于这题而言,则是要求n!中,不与m!互质的个数,答案取模100000007 那么先看一个证明: 求kn中与n互质的个数,答案为k?(n). ?(n)表示1-n中与n互质的个数,那么由此考虑[n + 1, 2n], [2n + 1, 3n]...这每个区间

uva 10692 Huge Mods 超大数取模

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

Help Tomisu UVA - 11440 难推导+欧拉函数,给定正整数N和M, 统计2和N!之间有多少个整数x满足,x的所有素因子都大于M (2&lt;=N&lt;=1e7, 1&lt;=M&lt;=N, N-M&lt;=1E5) 输出答案除以1e8+7的余数。

/** 题目:Help Tomisu UVA - 11440 链接:https://vjudge.net/problem/UVA-11440 题意:给定正整数N和M, 统计2和N!之间有多少个整数x满足,x的所有素因子都大于M (2<=N<=1e7, 1<=M<=N, N-M<=1E5) 输出答案除以1e8+7的余数. 思路: lrjP338 由于x的所有素因子都>M:那么x与M!互质. 根据最大公约数的性质,对于x>y,x与y互质,那么x%y与y也互质. 由于N

POJ 2407 Relatives &amp;&amp; UVA 10299 Relatives(欧拉函数)

[题目链接]:click here~~ [题目大意]:欧拉函数:求少于或等于n的数中与n互素的数的个数:n <= 1,000,000,000. [思路]:裸欧拉函数,注意特判n==1的情况,n==1的情况下,应该输出0,poj依然判断1也可以过,但是老牌ojUVA必须是0才过,注意一下. 代码: #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm>

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

UVA 10837 - A Research Problem(欧拉函数)

UVA 10837 - A Research Problem 题目链接 题意:给定phi(n),求最小满足的最小的n 思路:phi(n)=pk11(p1?1)?pk22(p2?1)?pk33(p3?1)....(p为质数),因此对于给定phi(n),先把满足条件phi(n)%(p?1)=0的素数全找出来,在这些素数基础上进行暴力搜索,枚举哪些素数用与不用,求出最小值.这样做看似时间复杂度很高,但是实际上,由于每次多选一个素数之后对于值是呈指数上升的,所以实际组合出来的情况并不会太多,因此是可行的