-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
71d0beb
commit 8be25c7
Showing
4 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
//{ Driver Code Starts | ||
//Initial Template for Java | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
class GFG { | ||
public static void main(String args[]) throws IOException { | ||
BufferedReader read = | ||
new BufferedReader(new InputStreamReader(System.in)); | ||
int t = Integer.parseInt(read.readLine()); | ||
while (t-- > 0) { | ||
String S[] = read.readLine().split(" "); | ||
|
||
long a = Long.parseLong(S[0]); | ||
long b = Long.parseLong(S[1]); | ||
long c = Long.parseLong(S[2]); | ||
long n = Long.parseLong(S[3]); | ||
long m = Long.parseLong(S[4]); | ||
|
||
Solution ob = new Solution(); | ||
System.out.println(ob.genFibNum(a,b,c,n,m)); | ||
} | ||
} | ||
} | ||
// } Driver Code Ends | ||
|
||
|
||
//User function Template for Java | ||
|
||
class Solution { | ||
static long genFibNum(Long a, Long b, Long c, long n, long m) | ||
{ | ||
// code here | ||
if (n <= 2) | ||
{ | ||
return 1 % m; | ||
} | ||
|
||
long[][] mat = | ||
{ | ||
{a, b, 1}, | ||
{1, 0, 0}, | ||
{0, 0, 1} | ||
}; | ||
|
||
long[][] res = | ||
{ | ||
{1, 0, 0}, | ||
{0, 1, 0}, | ||
{0, 0, 1} | ||
}; | ||
|
||
n -= 2; | ||
while (n > 0) | ||
{ | ||
if ((n & 1) == 1) | ||
{ | ||
res = multiply(res, mat, m); | ||
} | ||
|
||
mat = multiply(mat, mat, m); | ||
n >>= 1; | ||
} | ||
|
||
return (res[0][0] + res[0][1] + c * res[0][2]) % m; | ||
} | ||
|
||
|
||
static long[][] multiply(long[][] A, long[][] B, long m) | ||
{ | ||
int size = A.length; | ||
long[][] result = new long[size][size]; | ||
for (int i = 0; i < size; ++i) | ||
{ | ||
for (int j = 0; j < size; ++j) | ||
{ | ||
long sum = 0; | ||
for (int k = 0; k < size; ++k) | ||
{ | ||
sum += A[i][k] * B[k][j]; | ||
sum %= m; | ||
} | ||
|
||
result[i][j] = sum; | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(logn) | ||
Space complexity - O(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* public class ListNode { | ||
* int val; | ||
* ListNode next; | ||
* ListNode() {} | ||
* ListNode(int val) { this.val = val; } | ||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; } | ||
* } | ||
*/ | ||
class Solution | ||
{ | ||
public ListNode removeZeroSumSublists(ListNode head) | ||
{ | ||
ListNode dummy = new ListNode(0, head); | ||
int prefix = 0; | ||
Map<Integer, ListNode> prefixToNode = new HashMap<>(); | ||
prefixToNode.put(0, dummy); | ||
|
||
for (; head != null; head = head.next) | ||
{ | ||
prefix += head.val; | ||
prefixToNode.put(prefix, head); | ||
} | ||
|
||
prefix = 0; | ||
|
||
for (head = dummy; head != null; head = head.next) | ||
{ | ||
prefix += head.val; | ||
head.next = prefixToNode.get(prefix).next; | ||
} | ||
|
||
return dummy.next; | ||
} | ||
} |