【数学归纳法】单峰数列 题解

ans = 2^n-2

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define INF 2100000000
#define LL long long
#define clr(x) memset(x,0,sizeof(x))
#define ms(a,x) memset(x,a,sizeof(x))
#ifdef win32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif

using namespace std;

LL m,n,T;

LL mul(LL m, LL n, LL mod) {
    LL ans = 0;
    while(n) {
        if(n&1) ans += m;
        m = (m<<1)%mod;
        m %= mod;
        ans %= mod;
        n >>= 1;
    }
    return ans;
}

LL poww(LL a, LL b, LL mo) {
    LL ans = 1, base = a;
    while(b) {
        if(b&1) ans = mul(ans, base, mo);
        base = mul(base, base, mo);
        b >>= 1;
    }
    return ans;
}

int main() {
    freopen("sequence.in","r",stdin);
    freopen("sequence.out","w",stdout);
    while(cin >> n >> m) {
        LL tot = 2;
             if(n == 1) cout << 1%m << endl;
        else if(m == 1) cout << 0 << endl;
        else if(n == 2) cout << 2%m << endl;
        else cout << (poww(2, n, m)-2+m)%m << endl;
    }
    return 0;
}

你可能感兴趣的:(【数学归纳法】单峰数列 题解)