- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find (a^b)%m where ‘a’ is very large in C++
In this tutorial, we are going to solve the equation (ab)%m where a is a very large number.
The equation (ab)%m=(a%m)*(a%m)...b_times. We can solve the problem by finding the value of a%m and then multiplying it b times.
Let's see the steps to solve the problem.
Initialize the numbers a, b, and m.
Write a function to find the a%m.
Initialize the number with 0.
Iterate over the number in string format.
Add the last digits to the number.
Update the number with number modulo them.
Get the value of a%m.
Write a loop that iterates b times.
Multiply the a%m and modulo the result with m.
Print the result.
Example
Let's see the code.
#include<bits/stdc++.h> using namespace std; unsigned int aModm(string str, unsigned int mod) { unsigned int number = 0; for (unsigned int i = 0; i < str.length(); i++) { number = number * 10 + (str[i] - '0'); number %= mod; } return number; } unsigned int aPowerBmodM(string &a, unsigned int b, unsigned int m) { unsigned int a_mod_m_result = aModm(a, m); unsigned int final_result = 1; for (unsigned int i = 0; i < b; i++) { final_result = (final_result * a_mod_m_result) % m; } return final_result; } int main() { string a = "123456789012345678901234567890123"; unsigned int b = 3, m = 7; cout << aPowerBmodM(a, b, m) << endl; return 0; }
Output
If you execute the above program, then you will get the following result.
1
Conclusion
If you have any queries in the tutorial, mention them in the comment section.
- Related Articles
- Maximum element in a very large array using pthreads in C++
- If an object is at infinity (very large distance) in front of a concave mirror, where is the image formed?
- Why should the resistance of:a) an ammeter be very small?b) a voltmeter be very large?
- Recursive sum of digit in n^x, where n and x are very large in C++
- The ultrasound waves can penetrate into matter to a large extent because they have:(a) very high speed (b) very high frequency(c) very high wavelength(d) very high amplitude
- Print all 3 digit repeating numbers in a very large number in C++
- Find Last Digit of a^b for Large Numbers in C++
- A large concave mirror has a radius of curvature of 1.5 m. A person stands 10 m in front of the mirror. Where is the person's image?
- The roots of the equation $x^{2} -3x-m( m+3) =0$, where m is a constant, are:$( A) m, m+3$$( B)-m, m+3$$( C)m, -(m+3)$$( D)-m,-(m+3)$
- How to handle very large numbers in Python?
- Snakes are killed in large numbers because:(a) they are very poisonous (b) they kill rats (c) their skin is expensive (d) they damage the crops
- Find the numerical value of ( P: Q ) where ( mathrm{P}=left(frac{x^{m}}{x^{n}}right)^{m+n-l} timesleft(frac{x^{n}}{x^{l}}right)^{n+l-m} timesleft(frac{x^{l}}{x^{m}}right)^{l+m-n} ) and( mathrm{Q}=left(x^{1 /(a-b)}right)^{1 /(a-c)} timesleft(x^{1 /(b-c)}right)^{1 /(b-a)} timesleft(x^{1 /(c-a)}right)^{1 /(c-b)} )where ( a, b, c ) being all different.A. ( 1: 2 )B. ( 2: 1 )C. ( 1: 1 )D. None of these
- A plot is in the form of a rectangle ( A B C D ) having semi-circle on ( B C ) as shown in figure below. If ( A B=60 mathrm{~m} ) and ( B C=28 mathrm{~m} ), find the area of the plot."
- Which is the correct option ?Where does assimilation and absorption of food occurs ?a)small intestine and large intestineb)large intestine and small intestine c) small intestined)large intestine.
- The length of the fence of a trapezium-shaped field ( A B C D ) is ( 130 mathrm{~m} ) and side ( A B ) is perpendicular to each of the parallel sides AD and ( B C ). If ( B C=54 mathrm{~m}, C D=19 mathrm{~m} ) and ( A D=42 mathrm{~m} ), find the area of the field.
