#include <cstdio>
#include <cstring>
#define Clear(A) memset(A,0,sizeof(A))
int Mod,S;
struct Mat{
unsigned int d[ 32 ] [ 32 ] ;
Mat operator * ( const Mat & oth) const {
Mat ret;
Clear( ret.d ) ;
int i,j,k;
for ( i= 0 ; i< S; i++ )
for ( j= 0 ; j< S; j++ )
for ( k= 0 ; k< S; k++ )
ret.d [ i] [ j] + = d[ i] [ k] * oth.d [ k] [ j] ;
for ( i= 0 ; i< S; i++ )
for ( j= 0 ; j< S; j++ )
ret.d [ i] [ j] % = Mod;
return ret;
}
} Stard,A,B,C;
int m;
char str[ 205 ] ;
void dfs( int ps,int depth,int ns) {
if ( depth>= m) {
Stard.d [ ps] [ ns] = 1 ;
return ;
}
if ( depth== 0 || ( ( ns& ( 1 << depth- 1 ) ) ! = ( ps& ( 1 << depth- 1 ) ) )
|| ( ( bool ) ( ps& ( 1 << depth- 1 ) ) ! = ( bool ) ( ps& ( 1 << depth) ) ) ) {
dfs( ps,depth+ 1 ,ns) ;
dfs( ps,depth+ 1 ,ns| ( 1 << depth) ) ;
}
else
dfs( ps,depth+ 1 ,( ns| ( 1 << depth) ) ^ ( ps& ( 1 << depth) ) ) ;
}
void init( ) {
int i;
scanf ( "%s %d %d" ,str,& m,& Mod) ;
if ( strlen ( str) == 1 && str[ 0 ] == '1' ) { printf ( "%d\n " ,( 1 << m) % Mod) ; return ; }
for ( i= 0 ; i< 1 << m; i++ )
dfs( i,0 ,0 ) ;
S= 1 << m;
}
int L;
int Num[ 105 ] ;
bool n[ 405 ] ;
void div ( ) {
int i,tmp= 0 ;
for ( i= L; i>= 1 ; i-- ) {
tmp= tmp* 10 + Num[ i] ;
Num[ i] = tmp/ 2 ;
tmp% = 2 ;
}
while ( L>= 1 && Num[ L] == 0 ) L-- ;
}
void dec( ) {
int i;
Num[ 1 ] -- ;
for ( i= 1 ; Num[ i] < 0 ; i++ ) {
Num[ i] + = 10 ;
Num[ i+ 1 ] -- ;
}
while ( L>= 1 && Num[ L] == 0 ) L-- ;
}
void solve( ) {
memset ( n,0 ,sizeof ( n) ) ;
int i,cnt= 0 ;
L= strlen ( str) ;
for ( i= L- 1 ; i>= 0 ; i-- ) Num[ ++ cnt] = str[ i] - '0' ;
cnt= 0 ;
dec( ) ;
while ( L> 0 ) {
n[ ++ cnt] = Num[ 1 ] & 1 ;
div ( ) ;
}
L= cnt;
for ( i= 0 ; i< 1 << m; i++ ) A.d [ 0 ] [ i] = 1 ;
C= Stard;
for ( i= L- 1 ; i>= 1 ; i-- ) {
C= C* C;
if ( n[ i] ) C= C* Stard;
}
A= A* C;
int ret= 0 ;
for ( i= 0 ; i< S; i++ ) ret+ = A.d [ 0 ] [ i] ;
ret% = Mod;
printf ( "%d\n " ,ret) ;
}
int main( ) {
int Tc;
scanf ( "%d" ,& Tc) ;
for ( int i= 1 ; i<= Tc; i++ ) {
Clear( Stard.d ) ;
Clear( A.d ) ;
Clear( C.d ) ;
init( ) ;
if ( ! ( strlen ( str) == 1 && str[ 0 ] == '1' ) ) solve( ) ;
if ( i! = Tc) puts ( "" ) ;
}
return 0 ;
}
I2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGNzdHJpbmc+CiNkZWZpbmUgQ2xlYXIoQSkgbWVtc2V0KEEsMCxzaXplb2YoQSkpCgppbnQgTW9kLFM7CgpzdHJ1Y3QgTWF0ewogICAgdW5zaWduZWQgaW50IGRbMzJdWzMyXTsKICAgIE1hdCBvcGVyYXRvciAqIChjb25zdCBNYXQgJm90aCkgY29uc3R7CiAgICAgICAgTWF0IHJldDsKICAgICAgICBDbGVhcihyZXQuZCk7CiAgICAgICAgaW50IGksaixrOwogICAgICAgIGZvciAoaT0wO2k8UztpKyspCiAgICAgICAgICBmb3IgKGo9MDtqPFM7aisrKQogICAgICAgICAgICBmb3IgKGs9MDtrPFM7aysrKQogICAgICAgICAgICAgIHJldC5kW2ldW2pdKz1kW2ldW2tdKm90aC5kW2tdW2pdOwogICAgICAgIGZvciAoaT0wO2k8UztpKyspCiAgICAgICAgICBmb3IgKGo9MDtqPFM7aisrKQogICAgICAgICAgICByZXQuZFtpXVtqXSU9TW9kOwogICAgICAgIHJldHVybiByZXQ7CiAgICB9Cn1TdGFyZCxBLEIsQzsKaW50IG07CmNoYXIgc3RyWzIwNV07Cgp2b2lkIGRmcyhpbnQgcHMsaW50IGRlcHRoLGludCBucyl7CiAgICBpZiAoZGVwdGg+PW0pewogICAgICAgIFN0YXJkLmRbcHNdW25zXT0xOwogICAgICAgIHJldHVybjsKICAgIH0KICAgIGlmICggZGVwdGg9PTAgfHwgKCAobnMmKDE8PGRlcHRoLTEpKSAhPSAocHMmKDE8PGRlcHRoLTEpKSApCiAgICB8fCAoIChib29sKShwcyYoMTw8ZGVwdGgtMSkpICE9IChib29sKShwcyYoMTw8ZGVwdGgpKSApICl7CiAgICAgICAgZGZzKHBzLGRlcHRoKzEsbnMpOwogICAgICAgIGRmcyhwcyxkZXB0aCsxLG5zfCgxPDxkZXB0aCkpOwogICAgfQogICAgZWxzZQogICAgICAgIGRmcyhwcyxkZXB0aCsxLChuc3woMTw8ZGVwdGgpKV4ocHMmKDE8PGRlcHRoKSkpOwp9Cgp2b2lkIGluaXQoKXsKICAgIGludCBpOwogICAgc2NhbmYoIiVzICVkICVkIixzdHIsJm0sJk1vZCk7CiAgICBpZiAoc3RybGVuKHN0cik9PTEgJiYgc3RyWzBdPT0nMScpIHsgcHJpbnRmKCIlZFxuIiwoMTw8bSklTW9kKTsgcmV0dXJuOyB9CiAgICBmb3IgKGk9MDtpPDE8PG07aSsrKQogICAgICBkZnMoaSwwLDApOwogICAgUz0xPDxtOwp9CgppbnQgTDsKaW50IE51bVsxMDVdOwpib29sIG5bNDA1XTsKCnZvaWQgZGl2KCl7CiAgICBpbnQgaSx0bXA9MDsKICAgIGZvciAoaT1MO2k+PTE7aS0tKXsKICAgICAgICB0bXA9dG1wKjEwK051bVtpXTsKICAgICAgICBOdW1baV09dG1wLzI7CiAgICAgICAgdG1wJT0yOwogICAgfQogICAgd2hpbGUgKEw+PTEgJiYgTnVtW0xdPT0wKSBMLS07Cn0KCnZvaWQgZGVjKCl7CiAgICBpbnQgaTsKICAgIE51bVsxXS0tOwogICAgZm9yIChpPTE7TnVtW2ldPDA7aSsrKXsKICAgICAgICBOdW1baV0rPTEwOwogICAgICAgIE51bVtpKzFdLS07CiAgICB9CiAgICB3aGlsZSAoTD49MSAmJiBOdW1bTF09PTApIEwtLTsKfQoKdm9pZCBzb2x2ZSgpewogICAgbWVtc2V0KG4sMCxzaXplb2YobikpOwogICAgaW50IGksY250PTA7CiAgICBMPXN0cmxlbihzdHIpOwogICAgZm9yIChpPUwtMTtpPj0wO2ktLSkgTnVtWysrY250XT1zdHJbaV0tJzAnOwogICAgY250PTA7CiAgICBkZWMoKTsKICAgIHdoaWxlIChMPjApewogICAgICAgIG5bKytjbnRdPU51bVsxXSYxOwogICAgICAgIGRpdigpOwogICAgfQogICAgTD1jbnQ7CiAgICBmb3IgKGk9MDtpPDE8PG07aSsrKSBBLmRbMF1baV09MTsKICAgIEM9U3RhcmQ7CiAgICBmb3IgKGk9TC0xO2k+PTE7aS0tKXsKICAgICAgICBDPUMqQzsKICAgICAgICBpZiAobltpXSkgQz1DKlN0YXJkOwogICAgfQogICAgQT1BKkM7CiAgICBpbnQgcmV0PTA7CiAgICBmb3IgKGk9MDtpPFM7aSsrKSByZXQrPUEuZFswXVtpXTsKICAgIHJldCU9TW9kOwogICAgcHJpbnRmKCIlZFxuIixyZXQpOwp9CgppbnQgbWFpbigpewogICAgaW50IFRjOwogICAgc2NhbmYoIiVkIiwmVGMpOwogICAgZm9yIChpbnQgaT0xO2k8PVRjO2krKyl7CiAgICAgICAgQ2xlYXIoU3RhcmQuZCk7CiAgICAgICAgQ2xlYXIoQS5kKTsKICAgICAgICBDbGVhcihDLmQpOwogICAgICAgIGluaXQoKTsKICAgICAgICBpZiAoIShzdHJsZW4oc3RyKT09MSAmJiBzdHJbMF09PScxJykpIHNvbHZlKCk7CiAgICAgICAgaWYgKGkhPVRjKSBwdXRzKCIiKTsKICAgIH0KICAgIHJldHVybiAwOwp9
compilation info
prog.cpp: In function ‘void dfs(int, int, int)’:
prog.cpp:31: warning: suggest parentheses around + or - inside shift
prog.cpp:31: warning: suggest parentheses around + or - inside shift
prog.cpp:32: warning: suggest parentheses around + or - inside shift
prog.cpp: In function ‘void init()’:
prog.cpp:42: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result
prog.cpp: In function ‘int main()’:
prog.cpp:100: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result
stdout