#include <cstdio> #include <cstring> #include <algorithm>using namespace Std;int mod;int n;struct node {int s[6][6];} Aa;void init () {memset (aa.s,0,sizeof (AA.S)); Aa.s[0][0]=aa.s[1][0]=aa.s[2][0]=aa.s[3][0]=1; Aa.s[0][1]=1; Aa.s[0][2]=aa.s[4][2]=1; aa.s[0][3]=2; Aa.s[3][3]=1; Aa.s[2][4]=1;} Node Mupi (node A,node b) {node C; memset (c.s,0,sizeof (C.S)); for (int k=0;k<5;k++) {for (int. i=0;i<5;i++) {for (int j=0;j<5;j++) {c.s[i][j]+= (a) S[i][k]%mod*b.s[k][j]%mod)%mod; }}} return C;} void Slove () {init (); Node D; memset (d.s,0,sizeof (D.S)); for (int i=0;i<5;i++) {d.s[i][i]=1; }/* Puts ("D"); for (int i=0;i<5;i++) {for (int j=0;j<5;j++) {printf ("%d", d.s[i][j]); } puts (""); } puts ("AA:"); for (int i=0;i<5;i++) {for (int j=0;j<5;j++) {printf ("%d", aa.s[i][j]); } puts (""); }*/ while (n) {if (n&1) D=mupi (aa,d); Aa=mupi (AA,AA); n/=2; }/* for (int i=0;i<5;i++) {for (int j=0;j<5;j++) {printf ("%d", d.s[i][j]); } puts (""); } */int ans=0; printf ("%d\n", D.s[0][0]%mod);} int main () {while (scanf ("%d%d", &n,&mod) &&n+mod) {slove (); }}
POJ 3420 Quad Tiling