标签:
Description
Input
Output
Sample Input
Sample Output
#include <cstdio> #include <iostream> #include <algorithm> #include <cstdlib> #include <cstring> #include <string> #include <queue> #include <map> #include <set> #define lson l, m, rt << 1 #define rson m + 1, r, rt << 1|1 using namespace std; typedef long long LL; const int N = 100005; LL n, m, nn, mm; LL pow_mod(LL b) { LL res = 1, a = 2; while(b) { if(b & 1) res = res * a % m; a = (a % m) * a % m; b >>= 1; } return res; } LL sum1(LL k) { if(k == 1) return 1; LL tmp, now; if(k & 1) tmp = pow_mod(k - 1); else tmp = pow_mod(k); now = sum1(k >> 1) % m; LL res = (now % m + (now * tmp) % m) % m; if(k & 1) res = res + pow_mod(k * 2 - 2) % m; return res; } LL sum2(LL k) { if(k == 1) return 2; LL tmp, now; if(k & 1) tmp = pow_mod(k - 1); else tmp = pow_mod(k); now = sum2(k >> 1) % m; LL res = (now % m + (now * tmp) % m) % m; if(k & 1) res = res + pow_mod(k * 2 - 1) % m; return res; } int main() { while(~scanf("%lld%lld", &n, &m)) { LL ans = 0; if(n & 1) { nn = (n + 1) >> 1; ans = sum1(nn); printf("%lld\n", ans % m); } else { mm = (n) >> 1; ans = sum2(mm); printf("%lld\n", ans % m); } } }
hdu 4990 Reading comprehension
标签:
原文地址:http://www.cnblogs.com/orchidzjl/p/4694294.html