• B - Leonel and the powers of two


    Leonel is solving exponentiation problems, he has become really good to calculate powers of two. Several times Leonel has impressed his friends and teacher showing his skills answering what is the value for the kk-th power of two (2^k2k), with really large values for kk.

    To make things more challenging to Leonel, his teacher has requested to him, instead of calculating the value for 2^k2k, to write the result in the following notation:

    • 2 if k = 1k=1
    • (2*2^{k-1})(2∗2k−1) if kk is an odd number
    • (2^{\frac{k}{2}})^2(22k​)2 if kk is an even number

    Leonel will have to repeat the process until no more changes can be done to the current notation. For example if k = 5k=5, then the first time Leonel does the notation he will write (2*2^4)(2∗24). Leonel can change the notation replacing 2^424 with (2^{\frac{4}{2}})^2 = (2^2)^2(224​)2=(22)2, resulting in : (2*(2^2)^2)(2∗(22)2), Leonel can change this notation replacing 2^222 with (2^{\frac{2}{2}})^2(222​)2, after this change the notation will be: (2*((2^1)^2)^2)(2∗((21)2)2), one last change can be done to the notation changing 2^121 with 22, which will result in (2*((2)^2)^2)(2∗((2)2)2), since no more changes can be done, Leonel has found the answer for k = 5k=5.

    Leonel is certain it will take more time for him to write in this notation than calculating the power, that is why he decided to ask for your help to write a computer program so he can copy the notation from the result of your program to his notebook. Given the value of kk write a program that prints 2^k2k in the notation described by Leonel's teacher.

    Input

    The first line of input contains a single integer TT (1 \leq T \leq 10^41≤T≤104), the number of test cases. Each of the next TT lines contains a single integer kk (1 \leq k \leq 10^{18}1≤k≤1018), representing the power of two Leonel has to write in his teacher's notation.

    Output

    For each test case in the input print the result of the final notation Leonel will get.

    Sample 1

    InputcopyOutputcopy
    5
    1
    2
    3
    4
    5
    
    2
    (2)^2
    (2*(2)^2)
    ((2)^2)^2
    (2*((2)^2)^2)

     分析,运用递归,使用string定义的函数,如果k=1返回1,奇数返回(2*"+solve(k-1)+")偶数返回("+ solve(k/2)+")^2

    1. #include <iostream>
    2. using namespace std;
    3. string solve(long long k){
    4. if(k==1){
    5. return "2";
    6. }
    7. if(k%2==1){
    8. return "(2*"+solve(k-1)+")";
    9. }
    10. else if(k%2==0){
    11. return "("+ solve(k/2)+")^2";
    12. }
    13. }
    14. int main(){
    15. int t;
    16. long long k;
    17. scanf("%d",&t);
    18. while(t--){
    19. scanf("%lld",&k);
    20. cout<<solve(k)<<endl;
    21. }
    22. }

  • 相关阅读:
    CTE(公共表表达式)和视图在查询时的性能影响
    2022年深圳技能大赛—大数据技术应用职业技能竞赛圆满闭幕
    解锁横向招聘:创新您的人才搜索
    C#:Winform界面中英文切换功能
    QT webengine显示HTML简单示例
    C primer plus学习笔记 —— 10、位运算
    CentOS 7 NAT模式 ping不通“ Name or service not known”
    web网页设计期末课程大作业:家乡旅游主题网站设计——河北8页HTML+CSS+JavaScript
    瞅瞅 Opencv:扫描图像
    文件操作 和 IO
  • 原文地址:https://blog.csdn.net/q619718/article/details/127768765