UVA 10692 Huge Mods (exponential cycle section)

Source: Internet
Author: User

The exponential loop section, because a ^x = a ^ (x% m + phi (m)) (mod m) is only established when X >= Phi (m), it should be noted to determine

By:gavin http://www.cnblogs.com/IMGavin///Index Cyclic section recursive processing #include<cstdio> #include <iostream> #include < cstdlib> #include <cstring> #include <string> #include <algorithm> #include <map> #include <queue> #include <vector> #include <cmath> #include <utility>using namespace Std;typedef long Long Ll;const int N = 10008, INF = 0x3f3f3f3f;    LL A[n], mod, n;int phi[n];void allphi (int N) {memset (phi,0,sizeof (PHI));    Phi[1]=1;            for (int i=2;i<=n;i++) {if (!phi[i]) {//Then I is a prime number phi[i]=i;                    for (int j=i;j<=n;j+=i) {if (!phi[j]) {phi[j]=j;            } phi[j]=phi[j]/i* (i-1); }}}}ll Powmod (ll a,ll b,ll MoD) {ll ret=1;while (b) {if (b&1) {ret = ret * a% mod;} A = A * a% mod;b>>=1;} return ret;}    BOOL Check (ll A, ll N, ll m) {if (a = = 1) {return false;    } LL ans = 1; for (int i = 0; i < n; i++) {ans *=A        if (ans >= m) {return true; }} return false;}        ll Dfs (ll D, ll M, bool &sym) {if (d = = N) {if (A[d] >= m) {sym = 1;        }else{sym = 0;    } return a[d]% m;    } bool Flag;    LL p = DFS (d + 1, phi[m], flag);    if (flag) {p + = phi[m];    } sym = Check (A[d], p, m); Return Powmod (A[d], p, m);}    int main () {Allphi (N-2);    int t = 0;    while (CIN >> MoD) {cin >> n;    for (int i = 1; I <= n; i++) {cin >> a[i];    } bool Flag;    printf ("Case #%d:%lld\n", ++t, DFS (1, MOD, flag)% mod); } return 0;}

  

UVA 10692 Huge Mods (exponential cycle section)

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.