-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path10229.cpp
executable file
·128 lines (106 loc) · 2.48 KB
/
10229.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
#include <vector>
using namespace std ;
vector< vector<long long int> > mul( const vector< vector<long long int> > &a, const vector< vector<long long int> > &b, long long int module )
{
if( b.empty() == true ) return vector< vector<long long int> >( 0 ) ;
vector< vector<long long int> > v( a.size(), vector<long long int>( b[ 0 ].size(), 0 ) ) ;
int i = 0 ;
int j = 0 ;
int k = 0 ;
for( i = 0 ; i < v.size() ; i++ )
{
for( j = 0 ; j < v[ i ].size() ; j++ )
{
for( k = 0 ; k < a[ i ].size() ; k++ )
{
v[ i ][ j ] += a[ i ][ k ] * b[ k ][ j ] ;
}
v[ i ][ j ] %= module ;
}
}
return v ;
}
vector< vector<long long int> > pow( const vector< vector<long long int> > &a, long long int p, long long int module )
{
vector< vector<long long int> > v( a.size(), vector<long long int>( a.size(), 0 ) ) ;
int i = 0 ;
for( i = 0 ; i < a.size() ; i++ )
v[ i ][ i ] = 1 % module ;
if( p == 0 )
return v ;
else if( p == 1 )
return a ;
v = pow( a, p / 2, module ) ;
v = mul( v, v, module ) ;
if( p % 2 == 1 )
{
v = mul( v, a, module ) ;
}
return v ;
}
int main()
{
vector< vector<long long int> > v ;
long long int ans ;
int m = 0 ;
int n = 0 ;
int i = 0 ;
while( cin >> n >> m )
{
v = vector< vector<long long int> >( 2, vector<long long int>( 2, 0 ) ) ;
v[ 0 ][ 1 ] = 1 ;
v[ 1 ][ 0 ] = 1 ;
v[ 1 ][ 1 ] = 1 ;
v = pow( v, n, 1L<<m ) ;
ans = v[ 0 ][ 0 ] * 0 + v[ 0 ][ 1 ] * 1 ;
cout << ans << endl ;
}
return 0 ;
}
/* AC but slower version
#include <iostream>
using namespace std ;
int main()
{
int exponent_2[ 20 ] = { 0 } ;
exponent_2[ 0 ] = 1 ;
for( int i = 1 ; i < 20 ; i++ )
{
exponent_2[ i ] = exponent_2[ i - 1 ] * 2 ;
}
int Fabonacci_length[ 20 ] = { 0 } ;
Fabonacci_length[ 0 ] = 1 ;
Fabonacci_length[ 1 ] = 3 ;
for( int i = 2 ; i < 20 ; i++ )
{
Fabonacci_length[ i ] = Fabonacci_length[ i - 1 ] * 2 ;
}
int **Fabonacci = new int *[ 20 ] ;
Fabonacci[ 0 ] = new int [ 1 ] ;
Fabonacci[ 0 ][ 0 ] = 0 ;
for( int i = 1 ; i < 20 ; i++ )
{
Fabonacci[ i ] = new int [ Fabonacci_length[ i ] ] ;
Fabonacci[ i ][ 0 ] = 0 ;
Fabonacci[ i ][ 1 ] = 1 ;
for( int j = 2 ; j < Fabonacci_length[ i ] ; j++ )
{
Fabonacci[ i ][ j ] = ( Fabonacci[ i ][ j - 1 ] + Fabonacci[ i ][ j - 2 ] ) % exponent_2[ i ] ;
}
}
int n , m ;
while( cin >> n >> m )
{
if( m == 0 )
{
cout << "0" << endl ;
}
else
{
cout << Fabonacci[ m ][ n % Fabonacci_length[ m ] ] << endl ;
}
}
return 0 ;
}
*/