Computer Science, asked by mpriyankadcegmailcom, 8 months ago

Gaint sequence. His friend give a Akshay give a
task that, he will give a number(N). Ganesh want
to sum the sequence until that range. A Giant
sequence is a sequence which follows this
formula
P(n) = P(n-2) + P(n-3)
P(0) = P(1) = P(2) = 1
For Ex: N = 5
p(0)=P(1)=P(2)=1
p(3)=P(1)+p(0)=1+1=2
p(4)=P(2)+p(1)=1+1=2
p(5) =P(3)+p(2)=1+2 = 3
Output :3
Input format
Input the Number(N)
Output format
Display the sum of Giant sequence until that range
Number(N).
Code constraints
0 <N< 1000
(only positive numbers are allowed)
Sample testcases
unut 1​

Answers

Answered by TrishaNikhilJaiswal
2

Answer:

Table of Contents[hide]

TWO INITIAL ALGORITHMS

INITIAL THOUGHTS

COMBINING THE BASES

ANOTHER APPROACH

CHOOSE YOUR BOUNDARY CASES CAREFULLY

BACK TO THE GEOMETRIC SERIES

BUT WHAT ABOUT ALL THE OTHER POSSIBLE VALUES OF M THAT AREN'T PRIME POWERS?

AN ALTERNATIVE APPROACH

Warning - mathematical content ahead!

If you've participated in programming contests before, you'll know that most of the time, the simpler the problem statement, the tougher the problem seems to be. This one looks like it follows the trend, but as long as you are grounded with the correct mathematical knowledge, each step in the solution to this problem was reasonably straightforward - the trick was not getting sidetracked into alternate methods that were never going to work.

Let's start by looking at a couple of key algorithms in problems like these.

Two Initial Algorithms

Calculating powers

Calculating a large power of a number in modular arithmetic is a common problem, and can be done very quickly. To calculate ab mod m, we calculate a, a2, a4, a8, a16 ... by repeated squaring the previous value. We then multiply together the right terms to reach whatever power of b we want.

We can combine both these steps into one function as follows:

[blockcode] result = 1;

power = a;

while (b>0) {

if (b%2==1) result = (result*power)%m;

power = (power*power)%m;

b/=2;

} [/blockcode]

Make sure you understand how this works - we are basically multiplying in the correct power of a every time we see a '1' bit in binary in b.

Calculating inverses

If we are working mod m and want to divide two numbers (ie find a/b), we can do so when b and m share no common factors.

Similar questions