Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment-3.2

Student Name: Shivansh Mitra UID: 22BCS90006


Branch: BE - CSE Section/Group: 21BCS_SN-911-A
Semester: 5th Date of Performance: 23-10-23
Subject Name: Advance Programming Lab-1 Subject Code: 21CSP-314

Aim: Implement the problems based on Backtracking.


Objective: The objective of implementing backtracking problems is to develop
algorithms that efficiently explore and search through solution spaces in a systematic
manner, with the aim of finding valid solutions. By practicing backtracking, learners
gain proficiency in solving complex combinatorial problems, such as puzzles and
optimization challenges, by recursively considering and discarding potential
solutions.
Problem 1:
The Fibonacci Sequence : The Fibonacci sequence appears in nature all around us, in the
arrangement of seeds in a sunflower and the spiral of a nautilus for example.The Fibonacci
sequence begins with and as its first and second terms. After these first two elements,
each subsequent element is equal to the sum of the previous two elements.
Programmatically:

Given , return the number in the sequence.


Script / Code:
#include <iostream>
int fibonacci(int n) {
if (n == 0) {
return 0;
} else if (n == 1) {
return 1;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

}
}
int main() {
int n;
std::cin >> n;
int result = fibonacci(n);
std::cout << result ;
return 0;
}

Output:

Problem 2:
You are given an integer P.
Also, you are given Q queries of the following type:
1. . N: Determine the count of distinct arrays of size < N and > 1 such that:
• Each array element is a prime number
• Product of the value of all the array elements is < P
• Array formed is palindromic
Note
. Two arrays are said to be distinct if there exists at least one index where the value of
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

element present in both the arrays is different.


. An array is said to be palindromic if it reads same from the left to right and right to
left direction.
Since the count can be very large, print the output in modulo 109 + 7.
· Assume 1 based indexing.
Input format
. The first line contains an integer P.
. The second line contains an integer Q.
. The next line contains Q space-separated integers that denotes the value of N for
each query.
Output format
Print Q space-separated integers denoting the result for Q queries.
Script / Code:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
define mod 1000000007
#define maxVal 1000007

vector<int> primes;
vector<ll> dp(21, 0);

void sieve() {
vector<int> isPrime(maxVal, 1);
isPrime[0] = isPrime[1] = 0;

for (int p = 2; p * p <= maxVal; p++) {


if (isPrime[p]) {
for (int i = p * p; i <= maxVal; i += p) {
isPrime[i] = 0;
}
}
}

for (int i = 2; i <= maxVal; i++) {


if (isPrime[i]) {
primes.push_back(i);
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

}
}
}
ll countArrays(int numPrimes, int isOdd, int product, int p) {
if (numPrimes == 0) {
return 1;
}
ll cnt = 0;
for (int i = 0; i < primes.size(); i++) {
if (isOdd == 1) {
if (product * primes[i] > p) {
break;
}
cnt += countArrays(numPrimes - 1, 0, product * primes[i], p);
} else {
if (product * primes[i] * primes[i] > p) {
break;
}
cnt += countArrays(numPrimes - 1, 0, product * primes[i] * primes[i], p);
}
}
return cnt;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);

sieve();

int queries, p;
cin >> p >> queries;

for (int i = 1; i < 21; i++) {


dp[i] = countArrays((i + 1) / 2, i % 2, 1, p);
}

for (int i = 2; i < 21; i++) {


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

dp[i] += dp[i - 1];


}
while (queries--) {
int n;
cin >> n;
if (n < 21) {
cout << dp[n] << " ";
} else {
cout << dp[20] << " ";
}
}
return 0;
}

Output:

LEARNING OUTCOMES :

1. Algorithmic Thinking: Backtracking problems require you to think


algorithmically and design solutions that systematically explore and backtrack
in search spaces. You'll enhance your ability to devise efficient algorithms for
complex scenarios.

2. Problem Decomposition: Backtracking encourages breaking down complex


problems into smaller, manageable subproblems. You'll develop the skill of
problem decomposition, making it easier to tackle intricate challenges.

3. Error Handling: Implementing backtracking solutions involves handling


errors, invalid choices, and tracking your progress. You'll improve your error
handling and debugging skills, making your code more robust and reliable.

You might also like