🌐 AI搜索 & 代理 主页
Skip to content

Commit eaa95f2

Browse files
committed
Learning DP
1 parent 36fa923 commit eaa95f2

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

src/Coin_Change.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//LEET CODE
2+
3+
import java.io.*;
4+
import java.util.Arrays;
5+
6+
public class Coin_Change {
7+
public static void main(String[] args)throws IOException {
8+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9+
10+
int t = Integer.parseInt(br.readLine());
11+
while(t-- > 0){
12+
String[] s = br.readLine().split(" ");
13+
14+
int len = s.length;
15+
int[] a = new int[len];
16+
for(int i=0; i<len; i++){a[i] = Integer.parseInt(s[i]);}
17+
18+
int sum = Integer.parseInt(br.readLine());
19+
20+
int ans = coinChange(a, sum);
21+
System.out.println(ans);
22+
}
23+
}
24+
25+
private static int coinChange(int[] a, int sum){
26+
int[] dp = new int[sum+1];
27+
Arrays.fill(dp,sum+1);
28+
dp[0] = 0;
29+
30+
for(int i=0; i<=sum; i++){
31+
for(int j:a){
32+
if(j <= i)
33+
dp[i] = Math.min(dp[i - j]+1,dp[i]);}
34+
35+
}
36+
37+
return dp[sum] > sum ? -1 : dp[sum] ;
38+
}
39+
}

src/FIBONACCI_logN.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//Codechef//Easy Tiling
2+
import java.io.*;
3+
4+
public class FIBONACCI_logN {
5+
6+
static long MOD = 1000000007;
7+
public static void main(String[] args)throws IOException {
8+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9+
10+
int t = Integer.parseInt(br.readLine());
11+
while(t-- > 0) {
12+
long n = Long.parseLong(br.readLine());
13+
14+
if(n == 1) System.out.println(1);
15+
else if(n == 2) System.out.println(2);
16+
else{
17+
n--;
18+
long[][] M = {{1,1},{1,0}};
19+
long[][] Identity = {{1,0},{0,1}};
20+
long[] f = {1,1};
21+
while(n > 0){
22+
if((n & 1) == 1){
23+
Identity = multiply(Identity,M);
24+
}
25+
M = multiply(M,M);
26+
n = n >> 1;
27+
}
28+
29+
//HERE F(0) = 1; F(1) = 1 THATS WHY F(2) = 2 AND SO ON..
30+
long ans = (Identity[0][0] + Identity[0][1])%MOD; // Fibonacci(N)
31+
System.out.println(ans);
32+
/*
33+
long ans = (Identity[1][0] + Identity[1][1])%MOD; // Fibonacci(N - 1)
34+
System.out.println(ans);*/
35+
36+
//FOR F(0) = 0; F(1) = 1;
37+
/*long ans = (Identity[0][0])%MOD; // Fibonacci(N)
38+
System.out.println(ans);
39+
40+
long ans = (Identity[1][0])%MOD; // Fibonacci(N - 1)
41+
System.out.println(ans);*/
42+
}
43+
44+
}
45+
}
46+
47+
private static long[][] multiply(long[][] A,long[][] B){
48+
long a = (A[0][0]*B[0][0] + A[0][1]*B[1][0])%MOD;
49+
long b = (A[0][0]*B[0][1] + A[0][1]*B[1][1])%MOD;
50+
long c = (A[1][0]*B[0][0] + A[1][1]*B[1][0])%MOD;
51+
long d = (A[1][0]*B[0][1] + A[1][1]*B[1][1])%MOD;
52+
53+
A[0][0] = a;
54+
A[0][1] = b;
55+
A[1][0] = c;
56+
A[1][1] = d;
57+
58+
return A;
59+
}
60+
}

0 commit comments

Comments
 (0)