Quick power: quickpow, power: quickpow

Source: Internet
Author: User

Quick power: quickpow, power: quickpow

 

As we all know, quick power is the most common method to optimize the power operation of the logarithm. When learning the idea of "quick power", the idea of division and governance is easy for everyone to use simple recursion.

But in fact, in addition to recursion, a better method will be a simple WHILE loop. Below is the code:

# Include <iostream> # include <cstdio> # include <algorithm> # include <cmath> # include <vector> using namespace std; int quickpow (int x, int y) {int n = 1; while (y! = 0) {if (y & 1) n * = x; x = x * x; y = y> 1;} return n ;}int main () {int a, B; scanf ("% d", & a, & B); printf ("% d \ n", quickpow (a, B ));}

* Remember to use bitwise operations to optimize QWQ.

 

PS: This is my first BLOG post. Here, I set up the FLAG to carefully refresh the questions, update the BLOG, and cheer up to become an ambitious QAQ.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.