• 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. }

  • 相关阅读:
    11-08 周三 图解机器学习之实现逻辑异或,理解输出层误差和隐藏层误差项和动量因子
    代码随想录训练营二刷第三十九天 | 62.不同路径 63. 不同路径 II
    电脑入门:CPU显示100%该如何处理
    antdesign中, 单个页面同时存在多个上传按钮
    java连接mysql8.0.28数据库实例
    机器学习_特征工程_特征数据的评价标准
    左神高级提升班2 约瑟夫环结构
    antd table给某些表头设置样式
    kali更换国内源 2022年更新
    【gl-transitions配置】原项目dockerfile修改,为视频添加转场效果
  • 原文地址:https://blog.csdn.net/q619718/article/details/127768765