ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【Codeforces 484A】Bits

2019-02-25 20:37:29  阅读:289  来源: 互联网

标签:temp ans1 Codeforces long 484A temp1 Bits out


【链接】 我是链接,点我呀:)
【题意】


让你求出l~r当中二进制表示1的个数最多的数x

【题解】


最多有64位
我们可以从l开始一直增大到r
怎么增大?
找到l的二进制表示当中0所在的位置
假设i这一位的0经过加法变成了1
那么我们再从低位到高位依次枚举那一位为1就好
然后把这个二进制转换成十进制
看看它是不是比ri来的小
如果是,则尝试更新最小值
a[i]=(int)一个long类型的数字%2
a[i]是Int
会出错..

【代码】

import java.io.*;
import java.util.*;

public class Main {
    
    
    static InputReader in;
    static PrintWriter out;
        
    public static void main(String[] args) throws IOException{
        //InputStream ins = new FileInputStream("E:\\rush.txt");
        InputStream ins = System.in;
        in = new InputReader(ins);
        out = new PrintWriter(System.out);
        //code start from here
        new Task().solve(in, out);
        out.close();
    }
    
    static int N = 50000;
    static class Task{
        int n,len;
        long li,ri;
        long Bits[];
        
        public void solve(InputReader in,PrintWriter out) {
            Bits = new long[100];
            n = in.nextInt();
            for (int ii = 1;ii <= n;ii++) {
                li = in.nextLong();ri = in.nextLong();
                long ans = li;int ans1 = 0;
                for (int j = 63;j >= 1;j--){
                    Bits[j] = li%2;
                    li = li/2;
                    ans1 += Bits[j];
                }
                
                //out.println("ans="+ans);
                //out.println("ans1="+ans);
                long temp = ans;
                int temp1 = ans1;
                long now = 1;
                for (int j = 63;j >= 1;j--) {
                    temp = temp - now*Bits[j];
                    temp1-=Bits[j];
                    if(Bits[j]==0) {
                        temp1 = temp1+1;
                        temp = temp + now;
                        //out.println("temp="+temp+" now = "+now);
                        if (temp<=ri) {
                            if (temp1>ans1) {
                                ans1 = temp1;
                                ans = temp;
                            }else if (temp1==ans1 && temp<ans) {
                                ans = temp;
                            }
                        }
                        long temp2 = 1,cur = 0;
                        for (int k = 1;k <= (63-j);k++) {
                            cur = cur + temp2;
                            if (temp + cur<=ri) {
                                if (temp1 + k > ans1) {
                                    ans1 = temp1 + k;
                                    ans = temp + cur;
                                }else if (temp1+k==ans1 && temp+cur<ans) {
                                    ans = temp + cur;
                                }
                            }else break;
                            temp2 = temp2*2;
                        }
                        temp = temp - now;
                        temp1 = temp1-1;
                    }
                    now = now * 2;
                }
                out.println(ans);
                //out.println(ans1);
            }
            
        }
    }

    

    static class InputReader{
        public BufferedReader br;
        public StringTokenizer tokenizer;
        
        public InputReader(InputStream ins) {
            br = new BufferedReader(new InputStreamReader(ins));
            tokenizer = null;
        }
        
        public String next(){
            while (tokenizer==null || !tokenizer.hasMoreTokens()) {
                try {
                tokenizer = new StringTokenizer(br.readLine());
                }catch(IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
        
        public int nextInt() {
            return Integer.parseInt(next());
        }
        
        public long nextLong() {
            return Long.parseLong(next());
        }
    }
}

标签:temp,ans1,Codeforces,long,484A,temp1,Bits,out
来源: https://www.cnblogs.com/AWCXV/p/10433189.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有