-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFRSKT.cpp
More file actions
51 lines (51 loc) · 1.2 KB
/
FRSKT.cpp
File metadata and controls
51 lines (51 loc) · 1.2 KB
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
/*
===================================================
Name :- Nishant Raj
Email :- raj.nishant360@gmail.com
College :- Indian School of Mines
Branch :- Computer Science and Engineering
Time :- 02 October 2015 (Friday) 19:07
===================================================*/
#include <bits/stdc++.h>
using namespace std;
#define ULL unsigned long long
void mul(ULL a[][2] , ULL b[][2] , ULL MOD)
{
ULL res[2][2];
memset(res , 0 , sizeof res);
for(int i = 0 ; i < 2 ; i ++)
for(int j = 0 ; j < 2 ; j++)
for(int k = 0 ; k < 2 ; k++)
res[i][j] = (res[i][j] + a[i][k]*b[k][j]) % MOD;
for(int i = 0 ; i < 2 ; i++)
for(int j = 0 ; j < 2 ; j++)
a[i][j] = res[i][j];
}
ULL power( ULL n , ULL MOD)
{
ULL fib[2][2] = { {1 , 1} , { 1 , 0}},temp[2][2] = { {1 , 0 } , { 0 , 1}};
while(n)
{
if(n&1)
mul(temp , fib , MOD);
mul(fib , fib , MOD);
n>>=1;
}
return temp[0][1];
}
int main()
{
int t;
cin>>t;
while(t--)
{
int k , n , MOD;
cin>>k>>n>>MOD;
ULL ans = n;
while(k--) {
ans = power(ans , MOD);
}
cout<<ans<<endl;
}
return 0;
}