Ultraviolet A-10692 huge Mod (Euler's function)

Source: Internet
Author: User
Problem xhuge 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, shoshould 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 wocould 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.

Inputthere 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 wowould contain the value for M (2 <= m <= 10000 ). the next number of that line wocould be n (1 <= n <= 10 ). then n numbers-the values for A1, A2, A3 ,..., an wocould follow. you can safely assume that 1 <= AI <= 1000. the end of input is marked by a line containing a single Hash ('#') Mark.

Outputfor 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 5100 2 5 253 3 2 3 2#
Case #1: 2Case #2: 25Case #3: 35

Problem setter: monirul Hasan, member of elite problemsetters 'panelspecial thanks: Hammad Sajjad Hossain

Enter the positive integers A1, A2, A3. an, and modulo m to calculate A1 ^ A2 ^... ^ An mod m.

Idea: A formula is required: recursive processing.

#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;}


Ultraviolet A-10692 huge Mod (Euler's function)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.