【高精度&想法题】Count the Even Integers @ICPC2017HongKong/upcexam5563#Java

时间限制: 1 Sec 内存限制: 128 MB
Yang Hui’s Triangle is defined as follow.
In the first layer, there are two numbers A1,1 and A1,2 satisfying A1,1 = A1,2 = 1.
Then for each i > 1, the i-th layer contains i + 1 numbers satisfying Ai,1 = Ai,i+1 = 1 and Ai,j = Ai−1,j−1 + Ai−1,j for 1 < j ≤ i.

1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1

Now, given an integer N , you are asked to count the number of even integers in the first N layers.
输入
The input file contains multiple cases, please handle it to the end of file.
For each case, there is only one line containing an integer N (0 < N ≤ 1050).
输出
For each case, output the number of the even integers in the first N layers of Yang Hui’s Triangle.
样例输入
4
8
12
样例输出
4
16
42

求杨辉三角前n行偶数个数和

先打了个表
1
1 1 //题目中 N=1 从这行开始,但为了便于发现规律,补上了第一行
1 0 1
1 1 1 1
1 0 0 0 1
1 1 0 0 1 1
1 0 1 0 1 0 1
1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 1
1 1 0 0 0 0 0 0 1 1
1 0 1 0 0 0 0 0 1 0 1
1 1 1 1 0 0 0 0 1 1 1 1
1 0 0 0 1 0 0 0 1 0 0 0 1
1 1 0 0 1 1 0 0 1 1 0 0 1 1
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1
1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1
1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1
1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1
1 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 1
1 0 1 0 1 0 1 0 0 0 0 0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 1 0 1
1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1
1 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1
1 0 1 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 1
1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1
1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1
1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

统计奇数的个数,取补集
统计奇数的个数时:
令C = N,base=1,重复如下步骤直到C等于零:
1.找到一个最大的k,使得2k<=C
2.C-=2k
3.答案统计ans+=3k(由表中规律得)*base
4.base*=2(递归分形规律)

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

public class Main {
    static Scanner cin = new Scanner(System.in);
    public static void main(String[] args) {
        while(cin.hasNextBigInteger()){
            BigInteger n = cin.nextBigInteger();
            n = n.add(BigInteger.ONE);
            BigInteger cnt = BigInteger.ONE;
            BigInteger ans = BigInteger.ZERO;
            BigInteger sq = (n.add(BigInteger.ONE).multiply(n).divide(BigInteger.valueOf(2)));
            while (n.compareTo(BigInteger.valueOf(0)) > 0) {
                for (int i = 170; i >= 0; i--) {
                    BigInteger m2 = BigInteger.valueOf(2).pow(i);
                    if (n.compareTo(m2) >= 0) {
                        n = n.subtract(m2);
                        BigInteger m3 = BigInteger.valueOf(3).pow(i);
                        ans = ans.add(m3.multiply(cnt));
                        cnt = cnt.multiply(BigInteger.valueOf(2));
                    }
                }
            }
            System.out.println(sq.subtract(ans));
        }
    }
}
posted @ 2018-04-10 16:07  NeilThang  阅读(130)  评论(0编辑  收藏  举报