Power (Power) of 2

Source: Internet
Author: User
Tags gcd

Power (Power) of 2 Title Description

Any positive integer can be represented by a power of 2. For example:
137=27+23+20
At the same time, the square is indicated by parentheses, that is, AB can be expressed as a (b).
The 137 can be expressed as:
2 (7) +2 (3) +2 (0)
Further: 7=22+2+20 (21 expressed in 2)
3=2+20
So the last 137 can be expressed as:
2 (2 (2) +2+2 (0)) +2 (+ (0)) +2 (0)
Another example:
1315=210 +28 +25 +2+1
So the last 1315 can be expressed as:
2 (2 (+ + (0)) +2) +2 (2 (+ + (0))) +2 (2 (2) +2 (0)) +2+2 (0)

Input

Input: Positive integer (n≤20000)

Output

Output: 0,2 representation of conforming N (cannot have spaces in the representation)

Sample input
1371315
Sample output
2 (2 (2) +2+2 (0)) +2 (+ + (0)) +2 (0) 2 (2 (+ + (0)) +2) +2 (2 (+ + (0)) +2 (2 (2) +2 (0)) +2+2 (0)
Analysis: Recursion
Code:
#include <iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<algorithm>#include<climits>#include<cstring>#include<string>#include<Set>#include<map>#include<queue>#include<stack>#include<vector>#include<list>#include<ext/rope>#defineRep (I,m,n) for (i=m;i<=n;i++)#defineRSP (It,s) for (Set<int>::iterator It=s.begin (); It!=s.end (); it++)#defineVI vector<int>#definePII pair<int,int>#defineMoD 1000000007#defineINF 0x3f3f3f3f#definePB Push_back#defineMP Make_pair#defineFi first#defineSe Second#definell Long Long#definePi ACOs (-1.0)Const intmaxn=1e5+Ten;Const intdis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};using namespacestd;using namespace__gnu_cxx;ll gcd (ll p,ll q) {returnq==0? P:GCD (q,p%q);} ll Qpow (ll p,ll q) {ll F=1; while(q) {if(q&1) f=f*p;p=p*p;q>>=1;}returnF;}intn,m;voidWorkintNow ) {    if(now==1) {printf ("2 (0)");return;} Else if(now==2) {printf ("2");return;} intp=1, q=0;  while(P<=now) q++,p<<=1; Q--, p>>=1; if(p==Now ) {printf ("2 (");        Work (q); printf (")"); }    Else    {        if(p==2) printf (" the"); Else{printf ("2 (");            Work (q); printf (")+"); } Work ( now-p); }}intMain () {inti,j,k,t;  while(~SCANF ("%d",&N)) {work (n); printf ("\ n"); }    //System ("pause");    return 0;}

Power (Power) of 2

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.