Codeforces Amr and Chemistry (Mathematics + chaos)

Source: Internet
Author: User

Codeforces Amr and Chemistry (Mathematics + chaos)

Question: give n numbers. Each number can be multiplied by two or divided by two (the downward integer is equivalent to the left or right shift). Ask how many operations can make the n numbers disguised.

Idea: first consider the possible values of each number, and express a number in the form of s * 2 ^ k. s is an odd number.

Then all values of this number may be s '* 2 ^ x, (s' = s/2, (s/2)/2 ,.....) and s '* 2 ^ x <= 100000

Because the data range of this question is small and each value may have a few hundred or more values, the number of times each value may be obtained from 1 to 100000 and the total number of operations are recorded, finally, traverse from 1 to 100000 and obtain the smallest ans.

Ps: the individual competition has done countless code sprees in the afternoon ...... the code is optimized at night ..........

 

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        # Include
       # Include
        
          # Include
         
           # Include
          
            # Include
           # Include
            
              # Define eps 1e-6 # define LL long using namespace std; // const int maxn = 100 + 5; const int INF = 100000000; int n, abc; int vol [100010], ans [100010]; // vol indicates the number of times each capacity may be obtained. ans indicates the total number of operations required for obtaining the capacity int main () {// freopen(input.txt, r, stdin ); while (scanf (% d, & n) = 1) {memset (vol, 0, sizeof (vol); memset (ans, 0, sizeof (ans )); for (int I = 0; I <n; I ++) {cin> abc; int t = abc; int cnt = 1, cnt1 = 0; abc <= 1; w Hile (abc <= 100000) {vol [abc] ++; ans [abc] + = cnt; cnt ++; abc <= 1 ;}cnt = 0; while (t % 2 = 0) {vol [t] ++; ans [t] + = cnt; cnt ++; cnt1 ++; t >>=1 ;} vol [t] ++; ans [t] + = cnt; t> = 1; cnt1 ++; while (t! = 0) {int u = t; int cnt2 = cnt1; while (u <= 100000) {vol [u] ++; ans [u] + = cnt2; cnt2 ++; u <= 1 ;}if (t % 2 = 0) {while (t % 2 = 0) {t >>=1; cnt1 ++; vol [t] ++; ans [t] + = cnt1 ;}}t >>= 1; cnt1 ++ ;}} int pans = INF; for (int I = 0; I <= 100000; I ++) if (vol [I] = n) pans = min (pans, ans [I]); // cout <vol [2] <endl <vol [1] <endl <vol [8] <endl; cout <pans <endl ;} return 0 ;}
            
          
         
        
      
     
    
   
  
 

 

 

 

??

 

Related Article

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.