HDU 4927 Series 1

总结出公式发现

当数列长度为 偶数的时候,按照角标为 奇负偶负,长度为 奇数的时候,按照角标为奇正偶负

然后系数为交错的杨辉三角

注意用高精度,会爆long long

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

public class Main {
    
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        while(cin.hasNext()){
            BigInteger x = BigInteger.ONE, one = BigInteger.ONE, two = BigInteger.valueOf(2), sum = BigInteger.ZERO, prevx;
            BigInteger n = cin.nextBigInteger();
            while(x.compareTo(n) <= 0){
                prevx = x;
                x = x.multiply(two);
                BigInteger temp = n.subtract(prevx);
                BigInteger calc = temp.mod(x).add(one);
                BigInteger multiple = temp.divide(x).add(one);
                if(calc.compareTo(prevx)>0 && !calc.equals(x)) calc = x.subtract(calc);
                if(calc.compareTo(prevx)<=0) sum = sum.add(calc.multiply(multiple).multiply(x));
            }
            System.out.println(sum.subtract(n.multiply(two)));
        }
        cin.close();
    }
    
}



你可能感兴趣的:(HDU 4927 Series 1)