Ultraviolet A 10692-Huge Mod (number theory)

Source: Internet
Author: User

Link to the question: Ultraviolet A 10692-Huge mod

Give the power of a number, and then it modulo the M value.

Solution: based on the nature of the residual series, the last line must be cyclical, so there will be AB = abmod (phi [M]) + phi [M] (phi [M] is an Euler's function of M), which can be solved based on recursion.

#include 
  
   #include 
   
    #include 
    
     const int maxn = 15;int A[maxn], k;int pow_mod (int a, int n, int M) {    int ans = 1;    while (n) {        if (n&1)            ans = ans * a % M;        a = a * a % M;        n /= 2;    }    return ans;}int euler_phi(int n) {    int m = (int)sqrt(n+0.5);    int ans = n;    for (int i = 2; i <= m; i++) {        if (n % i == 0) {            ans = ans / i * (i-1);            while (n%i==0)                n /= i;        }    }    if (n > 1)        ans = ans / n * (n - 1);    return ans;}int solve (int d, int M) {    if (d == k - 1)        return A[d]%M;    int phi = euler_phi(M);        int c = solve (d+1, phi) + phi;    return pow_mod(A[d], c, M);}int main () {    int cas = 1;    char str[maxn];    while (scanf("%s", str) == 1 && strcmp(str, "#")) {        int M;        sscanf(str, "%d", &M);        scanf("%d", &k);        for (int i = 0; i < k; i++)            scanf("%d", &A[i]);        printf("Case #%d: %d\n", cas++, solve(0, M));    }    return 0;}
    
   
  

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.