Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 8 Dynamic
Programming

Student Name: Aman sehgal UID:20BCS5252


Branch: B.E-CSE Section/Group: 612A
Semester: 5th Date of Performance: 28/10/22
Subject Name: Competitive Programming Subject Code:20CSP-314

• Aim/Overview of the practical:


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
• Steps for experiment/practical/Code:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int mod = 1000000007;

int Inv(int a)

{ int res = 1; int p = mod - 2; while (p)

{ if (p & 1) res = ll(res) * a % mod;

p >>= 1; a = ll(a) * a % mod;

} return

res;

int main() {
int n;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
int k; int x; cin >> n >> k >> x; int res1 = 1, res0 = 0; for

(int i = 1; i < n; i++) { int nres1 = res0; int nres0 =

(ll(res1) * (k - 1) + ll(res0) * (k - 2)) % mod; res1 = nres1;

res0 = nres0;

}
if (x == 1) printf("%d\n", res1); else { int

res = ll(res0) * Inv(k - 1) % mod;

printf("%d\n", res);

}
return 0;

• Result/Output/Writing Summary
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

• Learning outcomes (What I have learnt):


a. Learnt about arrays.

b. Got an overview of the type of questions on hacker-rank.

c. Get to know about crucial test cases.

d. Got an understanding about referencing of arrays.

e. Learnt concept and difference between call-by-value and call-by-function.

Evaluation Grid (To be created as per the SOP and Assessment guidelines by the faculty):

Sr. No. Parameters Marks Obtained Maximum Marks


1.
2.
3.

• Aim/Overview of the practical:


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

• Steps for experiment/practical/Code:

#include <cmath>

#include <cstdio>

#include <vector>

#include <iostream>

#include <algorithm>
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
#define MAX_N 10005

#define INF 1000000000

using namespace std;

int tests,n,answer,s;

int h[MAX_N],minn;

int find_min(int x) {

int res = INF;

int tmp = x / 5;

if ((x - 5 * tmp) % 2 == 0) res = min(res, tmp + (x - 5 * tmp) / 2);

else res = min(res, tmp + (x - 5 * tmp) / 2 + 1);

if (tmp >= 1) {

if ((x - 5 * tmp) % 2 == 1) res = min(res, tmp - 1 + (x + 5 - 5 * tmp) / 2);

else res = min(res, tmp - 1 + (x + 5 - 5 * tmp) / 2 + 1);

return res;

int main() {
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
scanf("%d", &tests);

for (int test = 0 ; test < tests ; test ++) {

scanf("%d", &n);

minn = INF;

for (int i = 0 ; i < n ; i ++) {

scanf("%d", &h[i]);

minn = min(minn, h[i]);

answer = INF;

for (int tmp = 0 ; tmp < 20 ; tmp ++) {

s = 0;

for (int i = 0 ; i < n ; i ++) {

s += find_min(h[i] - minn + tmp);


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
}

answer = min(answer, s);

printf("%d\n", answer);

return 0;

• Result/Output/Writing Summary
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

 Learning outcomes (What I have learnt):


f. Learnt about arrays.

g. Got an overview of the type of questions on hacker-rank.

h. Get to know about crucial test cases.

Sr. No. Parameters Marks Obtained Maximum Marks


1.
2.
3.
i. Got an understanding about referencing of arrays.

j. Learnt concept and difference between call-by-value and call-by-function.

Evaluation Grid (To be created as per the SOP and Assessment guidelines by the faculty):
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

You might also like