清北暑假模拟day1 艳阳天

/*
注意P有可能不是质数,不要用欧拉函数那一套,正解可以倍增,就是等比数列和的性质,注意n是否为奇数
*/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <memory.h>
#ifdef WIN32
#define LL "%I64d"
#else
#define LL "%lld"
#endif
using namespace std;
int n,m,k;
const int MAXN = 10010;

long long qpow(long long a,long long b,long long MOD) {
    if (b == 0) return 1;
    if (b == 1) return a % MOD;
    long long res = qpow(a, b / 2,MOD);
    res = (res * res) % MOD;
    res = (res * qpow(a,b % 2,MOD)) % MOD;
    return res;
}
long long query(int x,int t,int MOD) {
    if (x == 1) return 1;
    long long res = query(x / 2, t, MOD);
    res = (res + qpow(t, x / 2 , MOD) * res) % MOD;
    if (x % 2) res = (res + qpow(t,x - 1,MOD)) %  MOD;
    return res;
}
int main() {
    freopen("sunshine.in","r",stdin);
    freopen("sunshine.out","w",stdout);

    scanf("%d%d%d",&n,&m,&k);
    printf(LL "\n",query(n,m,k));

    return 0;
}

 

posted @ 2016-10-21 09:20  ACforever  阅读(173)  评论(0编辑  收藏  举报