P1010 幂次方(DFS)

题目描述

任何一个正整数都可以用 222 的幂次方表示。例如

137=27+23+20137=2^7+2^3+2^0 137=27+23+20

同时约定方次用括号来表示,即 aba^bab 可表示为 a(b)a(b)a(b) 。

由此可知, 137137137 可表示为:

2(7)+2(3)+2(0)2(7)+2(3)+2(0)2(7)+2(3)+2(0)

进一步:

7=22+2+207= 2^2+2+2^07=22+2+20 (2^1用2表示),并且

3=2+203=2+2^03=2+20

所以最后 137137137 可表示为:

2(2(2)+2+2(0))+2(2+2(0))+2(0)2(2(2)+2+2(0))+2(2+2(0))+2(0)2(2(2)+2+2(0))+2(2+2(0))+2(0)

又如:

1315=210+28+25+2+11315=2^{10} +2^8 +2^5 +2+11315=210+28+25+2+1

所以 131513151315 最后可表示为:

2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0)2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0)2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0)

输入输出格式

输入格式:

一个正整数 n(n≤20000)n(n≤20000)n(n≤20000) 。

输出格式:

符合约定的 nnn 的 0,20,20,2 表示(在表示中不能有空格)

输入输出样例

输入样例#1: 复制

1315

输出样例#1: 复制

2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0)

思路:DFS一直拆,其实就是递归,拆到2^1和2^0停止,开始还想复杂了...水题一个


//神兽勿删

// ━━━━━━神兽出没━━━━━━
//    ┏┓     ┏┓
//   ┏┛┻━━┛┻┓
//   ┃       ┃
//   ┃ ━     ┃
//   ┃┳┛┗┳    ┃
//   ┃       ┃
//   ┃ ┻     ┃
//   ┃       ┃
//   ┗━┓   ┏┛ Code is far away from bug with the animal protecting
//      ┃   ┃    神兽保佑,代码无bug
//       ┃   ┃
//       ┃   ┗━━━┓
//         ┃        ┣┓
//      ┃        ┏┛
//     ┗┓┓┏━┳┓┏┛
//       ┃┫┫  ┃┫┫
//       ┗┻┛  ┗┻┛
//
// ━━━━━━感觉萌萌哒━━━━━━
//          ┏┓  ┏┓
//        ┏┛┻━━┛┻┓
//        ┃      ┃  
//        ┃   ━  ┃
//        ┃ >  < ┃
//        ┃      ┃
//        ┃... ⌒ ...  ┃
//        ┃      ┃
//        ┗━┓   ┏┛
//           ┃   ┃ Code is far away from bug with the animal protecting          
//           ┃   ┃   神兽保佑,代码无bug
//           ┃   ┃           
//           ┃   ┃        
//           ┃   ┃
//           ┃   ┃           
//           ┃   ┗━━━┓
//           ┃       ┣┓
//           ┃       ┏┛
//           ┗┓┓┏━┳┓┏┛
//            ┃┫┫ ┃┫┫
//            ┗┻┛ ┗┻┛
#include<bits/stdc++.h>
using namespace std;
#define maxn 20005
typedef long long ll;
ll n,A[maxn],num;
void dfs(ll x)
{
    string S;
    ll ans = 0;
    while(x)
    {
        S += x%2+'0';
        x /= 2;
        ans++;
    }
    ans--;
    reverse(S.begin(),S.end());
    ll t=S.size()-1;

    while(S[t] == '0')
        t--;
    //cout << S <<" "<<ans<<" "<<t<<endl;
    for(ll i = 0; i <=t; i++)
    {

        if(S[i] == '1')
        {
            ll flag = 0;
            //cout<<"啦啦啦"<<endl;
            if(ans == 1)
            {
                cout << 2;
            }
            else if(ans==0)
            {
                cout << 2 <<"(" << ans << ")";
            }
            else
            {
                for(ll j = 0; j <=num; j++)
                    if(A[j]==ans)
                    {
                        flag = 1;
                        break;
                    }
                if(flag)
                {
                    cout << 2 << "(" << ans << ")";
                }
                else
                {
                    cout << 2 << "(";
                    dfs(ans);
                    cout << ")";
                }
            }
            if(i!=t)
                cout << "+";

        }
        ans--;

    }
    return ;
}
void s()   //不用开数组 这里其实多余了,只用在DFS里特判一下就好
{
    A[0]=1;
    A[1]=2;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin >> n;
    s();
    dfs(n);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Whyckck/article/details/81412680
今日推荐