HDU 4577 X-Boxes (數學+大數除法)

第一次用java,居然過了。 大笑大笑大笑


import java.util.*;
import java.math.*;
public class Main {
	public static void main(String[] args) {
		BigInteger s,n,tmp;int k;
		int cas;
		Scanner cin = new Scanner(System.in);
		cas = cin.nextInt();
		while(cas>0){ cas--;
			n = cin.nextBigInteger();
			k = cin.nextInt();
			s = BigInteger.ZERO;
			int cc = 1;
			for(int i=1;i<k;i++){
				cc*=2;
			}
			while(1==1){
				n = n.divide(BigInteger.valueOf(cc));
				tmp = n.subtract(n.divide(BigInteger.valueOf(2)));
				if(tmp.equals(BigInteger.ZERO)) break;
				s = s.add(tmp);
				n = n.divide(BigInteger.valueOf(2));
			}
			System.out.println(s);
		}
	}
}


發佈了230 篇原創文章 · 獲贊 3 · 訪問量 19萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章