题意:给你一个数n,让你找m个非负整数,使得它们的和为n,并且按位或起来以后的值最小化。输出这个值。

从高位到低位枚举最终结果,假设当前是第i位,如果m*(2^i-1)<n的话,那么说明这一位如果填零,剩下的位不论怎么填,都绝对凑不出n来,所以这一位必须填1.如果m*(2^i-1)>=n,这一位就填1,然后把n变为max(n mod 2^i , n-m*2^i),重复这个过程即可。

要用高精度。

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

public class Main{
	public static void main(String[] argc){
		int T;
		BigInteger n,m;
		BigInteger[] pw=new BigInteger[4005];
		pw[0]=BigInteger.ONE;
		for(int i=1;i<=4001;++i){
			pw[i]=pw[i-1].multiply(BigInteger.valueOf(2l));
		}
		Scanner sc = new Scanner (new BufferedInputStream(System.in));
		T=sc.nextInt();
		for(int zu=1;zu<=T;++zu){
			n=sc.nextBigInteger();
			m=sc.nextBigInteger();
			String s=n.toString();
			int len=s.length();
			BigInteger ans=BigInteger.ZERO;
			for(int i=len*4;i>=0;--i){
				BigInteger tmp=m.multiply(pw[i].subtract(BigInteger.ONE));
				if(tmp.compareTo(n)==-1){
					ans=ans.add(pw[i]);
					BigInteger t=n.subtract(m.multiply(pw[i]));
					BigInteger t2=n.mod(pw[i]);
					if(t.compareTo(t2)==1){
						n=t;
					}
					else{
						n=t2;
					}
				}
			}
			System.out.println(ans);
		}
		sc.close();
    }
}

相关文章:

  • 2022-01-09
  • 2021-11-26
  • 2021-07-20
  • 2021-06-30
  • 2022-02-06
  • 2021-07-28
  • 2021-12-05
  • 2021-10-10
猜你喜欢
  • 2021-12-22
  • 2022-12-23
  • 2022-12-23
  • 2022-02-25
  • 2021-07-21
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案