hdu1852快速幂取余

开始题目看岔了,以为还挺复杂的,打了半天才发现题目是说对2008^N求约数和再计算,不是对N,于是就很简单了。。。

/*

 * hdu1852/win.cpp

 * Created on: 2012-7-10

 * Author    : ben

 */

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <cmath>

#include <ctime>

#include <iostream>

#include <algorithm>

#include <queue>

#include <set>

#include <map>

#include <stack>

#include <string>

#include <vector>

#include <deque>

#include <list>

#include <functional>

#include <numeric>

#include <cctype>

using namespace std;



typedef long long LL;



int modular_exp(int a, int b, int c) {

    LL res, temp;

    res = 1, temp = a % c;

    while(b) {

        if(b & 1) {

            res = res * temp % c;

        }

        temp = temp * temp % c;

        b >>= 1;

    }

    return (int)res;

}



int main() {

#ifndef ONLINE_JUDGE

    freopen("data.in", "r", stdin);

#endif

    int N, K, M;

    while(scanf("%d%d", &N, &K) == 2) {

        if(N == 0 && K == 0) {

            break;

        }

        int p1 = modular_exp(2, 3 * N + 1, K);

        p1 = (p1 + K - 1) % K;

        int p2 = modular_exp(251, N + 1, K * 250);

        p2 = (p2 + K * 250 - 1) % (K * 250);

        p2 /= 250;

        M = (p1 % K) * (p2 % K) % K;

        printf("%d\n", modular_exp(2008, M, K));

    }

    return 0;

}

你可能感兴趣的:(HDU)