n 为奇数时:
f(1)=(01)B=1 f(2)=(101)B=5 f(3)=(10101)B=21……
f(n) = 20+22+24+…+2n-1 = 40+41+42+…+4(n-1)/2 = (2n+1-1)/3;
n为偶数时:
f(1)=(10)B=2 f(2)=(1010)B=10 f(3)=(101010)B=42……
f(n)=21+23+25+…+2n-1=2(40+41+42+…+4(n-1)/2)=(2n+1-2)/3;
存一个带除法取模的公式orz
a/b%m=a%bm/b
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5 + 8;
const int inf = 0x3f3f3f3f;
ll ff(ll x, ll y, ll mod)
{
ll ans = 1;
while(y)
{
if(y & 1)
ans = ans * x % mod;
x = x * x % mod;
y >>= 1;
}
return ans;
}
int main()
{
ll n, m;
while(~scanf("%lld%lld", &n, &m))
{
if(n & 1)
printf("%lld\n", (ff(2, n + 1, 3 * m) - 1) / 3);
else
printf("%lld\n", (ff(2, n + 1, 3 * m) - 2) / 3);
}
}