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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:21:51+00:00 2026-06-11T03:21:51+00:00

#include<iostream> #include<cstdio> #define M 1000000007 using namespace std; long long int power(int a,int b)

  • 0
#include<iostream>
#include<cstdio>
#define M 1000000007
using namespace std;

long long int power(int a,int b)
{
    if(b==0)
        return 1;
    else if(b==1)
        return a;
    else if(b%2==0)
        return power((a*a)%M,b/2);
    else
        return (power((a*a)%M,b/2)*a)%M;
}

In this function when I pass a=2, b>31, it always returns 0.for b=31 i got 147483634.can you tell where the problem is?

or can you tell another method for calculating large powers of a number.

  • 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-11T03:21:53+00:00Added an answer on June 11, 2026 at 3:21 am

    In (a*a)%M, a*a probably overflows before computing the remainder. And starting with 2, the overflow producing 0 doesn’t surprise me. You need to work with a type able to represent (M-1)*(M-1), i.e. 1000000012000000036 while int is commonly limited to 2147483647. long long (standard in C since 99 and C++ since 11, common extension elsewhere) is guaranteed to work.

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

Sidebar

Related Questions

#include <ctime> #include <cstdio> #include <sys/time.h> #include <iostream> using namespace std; int main() {
Consider this code #include <iostream> #include <cstdio> using namespace std; class Dummy { public:
#include<iostream> #include<cstdio> using namespace std; int main() { int T; char J[100], S[100]; int
main cpp file: #include <iostream> #include <cstdio> #include table.h using namespace std; int main()
I'm getting compile error in this code #include<iostream> #include<cstdio> #include<string> using namespace std; void
#include <iostream> using namespace std; int main() { int a, b, c, max; cout<<a=;
#include <iostream> #include <string.h> using namespace std; int main() { int e=0; int b=0;
#include <iostream> #include <string> using namespace std; string crash() { } int noCrash() {
#include <iostream> #include <iomanip> using namespace std; int main() { char array[10]; for(int i
#include<iostream> #include<stdlib.h> #include<string.h> #include<stdio.h> using namespace std; union type{ int a; char b; int

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.