uva 10692 - Huge Mods(數論)

來源:互聯網
上載者:User

標籤:style   http   color   width   os   for   

題目連結:uva 10692 - Huge Mods

題目大意:給出一個數的次方形式,就它模掉M的值。

解題思路:根據剩餘系的性質,最後一定是行成周期的,所以就有ab=abmod(phi[M])+phi[M](phi[M]為M的歐拉函數),這樣就可以根據遞迴去求解。

#include <cstdio>#include <cstring>#include <cmath>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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.