2017CCPC 秦皇岛 G

给一个数n,要把这个数分成m个数,这m个数的和n,且要求或起来的答案最小。

因为要或,所以易知m个数中最高位最小且尽量均分即可。

均分的时候先确定一个最高位,然后从最高位向低位填数字,如果一位能填则尽量填满即可。

chawa大数模拟一哈就好了。

 

 

 

 

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

public class G{
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int t;BigInteger one,two,zero;
        one = BigInteger.valueOf(1);two = BigInteger.valueOf(2);zero = BigInteger.valueOf(0);
        t=cin.nextInt();
        //System.out.println(t);
        for(int p=1;p<=t;p++){
            BigInteger n,m,ans;
            ans=BigInteger.valueOf(0);
            n=cin.nextBigInteger();m=cin.nextBigInteger();
            //System.out.println(n);
            BigInteger tmp = new BigInteger("1");
            while((tmp.subtract(one)).multiply(m).compareTo(n)<0){
                tmp=tmp.multiply(two);
            }
            while(n.compareTo(zero)>0){
                while((tmp.subtract(one)).multiply(m).compareTo(n)>=0){
                    tmp=tmp.divide(two);
                }
                ans=ans.add(tmp);
                if(tmp.multiply(m).compareTo(n)<0){
                    n=n.subtract(tmp.multiply(m));
                }
                else{
                    n=n.mod(tmp);
                }
            }
            System.out.println(ans);
        }
    }
}
View Code

 

posted @ 2018-09-12 13:35  LMissher  阅读(219)  评论(0)    收藏  举报