zoukankan      html  css  js  c++  java
  • hdu-5587 Array(回溯)

    题目链接:

    Array

    Time Limit: 2000/1000 MS (Java/Others)   

     Memory Limit: 131072/131072 K (Java/Others)

    Problem Description
     
    Vicky is a magician who loves math. She has great power in copying and creating.
    One day she gets an array {1}。 After that, every day she copies all the numbers in the arrays she has, and puts them into the tail of the array, with a signle '0' to separat.
    Vicky wants to make difference. So every number which is made today (include the 0) will be plused by one.
    Vicky wonders after 100 days, what is the sum of the first M numbers.
     
    Input
     
    There are multiple test cases.
    First line contains a single integer T, means the number of test cases.(1T2103)
    Next T line contains, each line contains one interger M. (1M1016)
     
    Output
     
    For each test case,output the answer in a line.
     
    Sample Input
     
    3
    1
    3
    5
     
    Sample Output
     
    1
    4
    7
     
    题意:
     
    一开始有一个1,每次操作都把这个数字串复制并且在前边加上一个0,然后再把得到的这个串的每个数字都加1,然后再把这个新得到的数字串加到原来串的末尾,问前m位数的和是多少;
     
    思路:
     
    找规律的题,先初始化,第i次操作一次得到的和为dp[i],长度为L[i],转移方程看代码;然后对一个数m,找到最大的j,L[j]<=m的长度串,ans+=dp[j],ans+=m-L[j];
    m减去这个长度L[j]并去掉前导0,得到一个新的更小的m,这样回溯最后就能得到答案了;
     
    AC代码:
    //#include <bits/stdc++.h>
    #include <vector>
    #include <iostream>
    #include <queue>
    #include <cmath>
    #include <map>
    #include <cstring>
    #include <algorithm>
    #include <cstdio>
    
    using namespace std;
    #define Riep(n) for(int i=1;i<=n;i++)
    #define Riop(n) for(int i=0;i<n;i++)
    #define Rjep(n) for(int j=1;j<=n;j++)
    #define Rjop(n) for(int j=0;j<n;j++)
    #define mst(ss,b) memset(ss,b,sizeof(ss));
    typedef long long LL;
    template<class T> void read(T&num) {
        char CH; bool F=false;
        for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
        for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
        F && (num=-num);
    }
    int stk[70], tp;
    template<class T> inline void print(T p) {
        if(!p) { puts("0"); return; }
        while(p) stk[++ tp] = p%10, p/=10;
        while(tp) putchar(stk[tp--] + '0');
        putchar('
    ');
    }
    
    const LL mod=1e9+7;
    const double PI=acos(-1.0);
    const LL inf=1e14;
    const int N=1e4+15;
    const int maxn=18;
    
    LL dp[70],L[70],ans,sum[70];
    void Init()
    {
        dp[1]=1;
        L[1]=1;
        for(int i=2;i<70;++i)
        {
            dp[i]=2*dp[i-1]+L[i-1]+1;
            L[i]=2*L[i-1]+1;
        }
    }
    
    void dfs(LL x)
    {
        if(x<=0)return;
        for(int i=1;i<70;i++)
        {
            if(L[i]<=x&&L[i+1]>x)
            {
                ans+=dp[i];
                x-=L[i]+1;
                ans+=x+1;
                break;
            }
        }
        dfs(x);
    }
    int main()
    {
        int  t;
        read(t);
        LL m;
        Init();
        while(t--)
        {
            ans=0;
            read(m);
            dfs(m);
            printf("%lld
    ",ans);
        }
    
            return 0;
    }
     
     
  • 相关阅读:
    做一名「技术掮客」去变现自己的技术
    又一流氓推广Microsoft Edge,我勒个去
    拿什么心情来阅读我的代码(程序员的必备心理技能)
    发现与研究的基石
    VBA小技巧
    从明天起,让我们做一名调包侠
    设计模式之“Observer”注疏#01
    mybatis-dynamic-query 3.0 更新
    TiDB入门(四):从入门到“跑路”
    利用typescript生成Swagger文档
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5595351.html
Copyright © 2011-2022 走看看