Hdu 5587 Array 2015.11.28 bestcoder 1003

Source: Internet
Author: User

Hdu 5587 Array 2015.11.28 bestcoder 1003

In the beginning, it was a series of 1. Each operation was performed, the series was copied and put to the back, and then 0 was placed between them, then, starting from the value 0, each number after the value 0 is plus 1. 1--112--20171223 ...... Then this series goes through several operations and asks you how many merge M numbers are before, and M is at the maximum of 10 to the power of 16.

Idea: after each operation, we find that their sum is the power of N-1 in the previous state and X 2 + 2, and n represents the number of series. Therefore, A table A can be created with A maximum size of 50 or 60. Then we find that the length of each series is the Npower of 2-1. So I typed a table B to record these lengths. Finally, let's give you an M, and you will be directed to its dfs. In table B, we will use a binary number to find the sequence number smaller than or equal to its first number, then, the corresponding array A contains the sum of the preceding items. Add the sum to the Final Solution. For the rest part of the sequence with the length of L, because the meaning of the question is followed by a total of + 1, we need to + L, then we also say that the center interval is 0 so we add dfs (L-1 ). Hand it back, very fast. I estimate that it is okay to look for B at the location without having to get two points, because table B is also very small, just a few dozen.

 

 

 

Array Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission (s): 139 Accepted Submission (s): 74



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. (1 ≤ T ≤ 2 limit 103)
Next T line contains, each line contains one interger M. (1 ≤ M ≤1016)
Output For each test case, output the answer in a line.

Sample Input
3135

Sample Output
147

Source BestCoder Round #64 (div.2)
#include 
 
  #include 
  
   #include 
   
    #include 
    
     #include 
     
      #include 
      
       #include #include 
       
        #include 
        
         #include 
         
          #include 
          
           #include 
           
            #include
            #include 
             
              using namespace std;typedef long long LL;const int INF=0x7fffffff;const int MAX_N=10000;int T;long long M;long long A[109];long long B[109];long long dfs(long long x){ long long ans=0; long long b=upper_bound(B+1,B+63,x)-upper_bound(B+1,B+63,1)+1; ans+=A[b]; if(x-B[b]==0)return ans; if(x-B[b]==1)return ans+1; ans+=dfs(x-B[b]-1)+x-B[b]; return ans;}int main(){ A[1]=1; long long m=1; for(int i=2;i<=100;i++){ A[i]=A[i-1]*2+2*m; m*=2; } B[1]=1; for(int i=2;i<=100;i++){ B[i]=2*B[i-1]+1; } cin>>T; while(T--){ scanf(%I64d,&M); long long ans=0; printf(%I64d,dfs(M)); } return 0;}
             
           
          
         
        
       
      
     
    
   
  
 



 

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.