Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8033153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:36:28+00:00 2026-06-05T01:36:28+00:00

I was solving this problem on SPOJ We are to calculate ( (P^N) +

  • 0

I was solving this problem on SPOJ

We are to calculate ( (P^N) + (Q^N) ), we are given
P+Q and P*Q.
Input :
first line will contain an integer T (<=15) denoting the number of test cases
three integers p+q, p*q and n will be given for each test case in a separate line
for every test case output the corresponding
output (p^n)+(q^n) in a separate line

After some time I came up with this recurrence

p^n + q^n = (p^n-1 + q^n-1)(p+q) - pq(p^n-2 + q^n-2)
and in my code i have
a = p + q and b = p.q

Here is my solution

 public Long computeExponential(int n)
  {
    //base cases
    if(n == 0)
    {
      return 1L;
    }
    else if(n == 1)
    {
      return new Long(a);
    }
    else
    {
        return (a * computeExponential(n-1) - b * computeExponential(n-2));
    }

The answers that I get with the given testcases is

2125764
4383653
-3
175099
28160

Is the formula that I have derived wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T01:36:29+00:00Added an answer on June 5, 2026 at 1:36 am

    No, your derived equation is spot-on. Just a wee error in your implementation that I can see:

    If n=0, p^0 + q^0 = 1 + 1 = 2. Your computeExponential for n=0 returns 1.

    [edit]For future reference, I find it quite helpful, for complicated algorithms especially, to write my own test cases, especially for the base cases, simple cases, and outliers, that I have manually calculated the results for and then run these first to check my function is doing what I think it should. Testing your method with n=0, p=2, q=3 (i.e. p+q=5, pq=6) for example would have thrown this error up pretty quickly. Only once it passes my own test cases would I then submit it to other test data which may, or may not, have any meaning to me.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble solving this problem: Create a function that given a character set
I have a very big problem solving this issue I have 2 tables first
I am have difficulties solving this problem: For a positive number n, define C(n)
What´s the most efficient, elegant and pythonic way of solving this problem? Given a
I need some help in solving this problem. We have a large amount of
I am wondering if anyone can offer any advice towards solving this problem. I
I was practising the algorithm based programming problem.I am having difficulty,in solving this problem.I
I've been problem solving this for the past few hours and frustratingly can't find
Consider this way of solving the Subset sum problem: def subset_summing_to_zero (activities): subsets =
This question is more about guidance than actually solving my problem: I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.