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 8478331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:42:59+00:00 2026-06-10T18:42:59+00:00

I have been writing a program for the following recurrence relation: An = 5An-1

  • 0

I have been writing a program for the following recurrence relation:

An = 5An-1 - 2An-2  - An-3 + An-4

The output should be the Answer modulus 10^9 + 7..
I wrote a brute force approach for this one as follows…

long long int t1=5, t2=9, t3=11, t4=13, sum;
while(i--)
{
    sum=((5*t4) - 2*t3 - t2 +t1)%MOD;
    t1=t2;
    t2=t3;
    t3=t4;
    t4=sum;
}
printf("%lld\n", sum);

where MOD= 10^9 +7
Every thing seems to be true.. but i am getting negative answer for some values.. and due to this problem, I am unable to find the correct solution…
Plz help about the right place to keep the Modulus

  • 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-10T18:43:00+00:00Added an answer on June 10, 2026 at 6:43 pm

    The thing is that the % operator isn’t the “modulo operator” but the “division remainder” operator with the following equality

    (a/b)*b + a%b == a    (for b!=0)
    

    So, if in case your integer division rounds towards zero (which is mandated since C99 and C++11, I think), -5/4 will be -1 and we have

    (-5/4)*4 + -5%4 == -5
      -1  *4    -1  == -5
    

    In order to get a positive result (for the modulo operation) you need to add the divisor in case the remainder was negative or do something like this:

    long mod(long a, long b)
    { return (a%b+b)%b; }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been writing a program which asks you to input two integers, which
I have been tasked with writing a program which will run a set of
I have tried writing program that plays a sound file but have been unsuccessful
Since I have been writing a big python program for my project. I have
I have been having some inexplicable SFINAE problems in a program I'm writing, so
Recently I have been writing a program in C++ that pings three different websites
I'm writing a web scraping program in C#. So far, I have been able
I have been writing a program that is quite complex compared to what I
I have been writing linear winforms for couple of months and now I am
I have been writing a SOAP client application in C++ on Ubuntu using OpenSSL

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.