//给出四个非负整数L,R,X,Y,请计算有多少个非负整数N满足以下四个条件//        1.N的二进制表示中1的个数不小于L//        2.N的二进制表示中1的个数不大于R//        3.N和X的按位与为X//        4.N和Y的按位或为Yimport java.util.Scanner;public class Main {    public static void main(String[] args) {        Scanner scan = new Scanner(System.in);        int T=scan.nextInt();        for (int i = 0; i < T; i++) {            int L= scan.nextInt();            int R= scan.nextInt();            int X= scan.nextInt();            int Y= scan.nextInt();            String s=Integer.toBinaryString(X);            String s1=Integer.toBinaryString(Y);            int count=0,consume=0;  //count表示可0可1的位数,consume表示必须为1的位数            int end1=s.length()-1,end2=s1.length()-1;            while (end1>=0||end2>=0){                if(end1<0){                    if(s1.charAt(end2)=='1'){                        count++;                    }                }else if(end2<0){                    if(s.charAt(end1)=='0'){                        count++;                    }else {                        consume++;                    }                } else{                    if(s.charAt(end1)=='0'&&s1.charAt(end2)=='1'){                        count++;                    }else if(s.charAt(end1)=='1'){                        consume++;                    }                }                end1--;end2--;            }            int res=0;            L-=consume;R-=consume;  //减去必须为1的位数            if(L<0){  //如果左边界小于0,把左边界置为1,并把count个位置全为0的这种情况算上                L=1;res++;            }            if(count<L||count>R){  //考试时没加'||count>R'过了18%                System.out.println(0);                System.exit(0);            }            if(count<R){                R=count;            }            //在count个位置上放置L—R个1            for (int j = L; j <= R; j++) {                res+=func(j,count);            }            System.out.println(res);        }    }    public static int func(int x,int y){  //用来计算CXY(x为上标,y为下标)        double res=1.0;        while (x>0){            res=res*y/x;            x--;y--;        }        return (int) res;    }}
点赞 3
评论 1
全部评论

相关推荐

愤怒的潜伏者在开会:你不攻击他,我可攻击你了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务