背景
curimit最近心情特別不好,尤其是看到偶數。
描述
某天,curimit在玩耍時,發現了一個紙頭上畫着一個楊輝三角形,心中感覺特不爽。
他認爲偶數是導致他心中不爽的根本原因。並且有多少個偶數,他的不爽程度就爲多少。
下面,請你算算curimit究竟有多不爽。
對楊輝三角形的說明:
1 1
1 2 1
1 3 3 1
1 4 6 4 1
。。。。。。。。。。。
形如上圖的三角形稱爲楊輝三角形,而curimit看到的是這個三角形的前n行。
格式
輸入格式
一行,一個n 。我們保證n小於50位。
輸出格式
一行,curimit的不爽程度。
限制
1秒。。。。
提示
對樣例的說明:
樣例給出的三角形是:
1 1
1 2 1
1 3 3 1
1 4 6 4 1
這個4行的楊輝三角形中一共有4個偶數。
本題有很大難度,請認真思考。
對前5個點:每個點2分,n均小於100
對第6-10個點:每個點4分,n均小於10000
對第11-16個點:每個點5分,n均小於maxlongint
對第17-20個點:每個點10分,n均小於10^50
來源
curimit原創。
感覺 要被這個題搞瘋了
首先打表看看,前行有多少偶數,那麼可以看出一些規律,,,,,
——————其實我並沒有看出來什麼規律,搜的oeis
然而那個公式把我搞瘋了,,,,,,原文這樣寫的a(0)=a(1)=0, a(2n) = 3a(n)+n(n-1)/2, a(2n+1) = 2a(n)+a(n+1)+n(n+1)/2.
然而序列是這樣的:
0, 0, 1, 1, 4, 6, 9, 9, 16, 22, 29, 33, 42, 48, 55, 55, 70, 84, 99, 111, |
然後你會發現,a(2) = 0;
所以其實那個序列是錯開一位的
也就是a(1)=a(2)=0, a(2n) = 3a(n)+n(n-1)/2, a(2n+1) = 2a(n)+a(n+1)+n(n+1)/2
十分尷尬,,,,,,,,
so
代碼是這樣的(有人用Python寫着好簡單,我java好複雜,,,,,):
import java.math.BigInteger;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
public static HashMap map = new HashMap();
public static BigInteger temp;
public static BigInteger two = BigInteger.valueOf(2);
public static BigInteger three = BigInteger.valueOf(3);
public static BigInteger fu = BigInteger.valueOf(-1);
public static void Solve(BigInteger n) {
if(map.get(n) != null) return;
if(n.mod(two).compareTo(BigInteger.ZERO) == 0) {
Solve(n.divide(two));
n = n.divide(two);
temp = n.multiply(n.subtract(BigInteger.ONE)).divide(two);
temp = map.get(n).multiply(three).add(temp);
map.put(n.multiply(two), temp);
}else {
n = n.divide(two);
Solve(n);
Solve(n.add(BigInteger.ONE));
temp = n.multiply(n.add(BigInteger.ONE)).divide(two);
temp = map.get(n).multiply(two).add(map.get(n.add(BigInteger.ONE))).add(temp);
map.put(n.multiply(two).add(BigInteger.ONE), temp);
}
}
public static void main(String[] args) {
Scanner Cin = new Scanner(System.in);
while(Cin.hasNext()) {
BigInteger n = Cin.nextBigInteger();
n = n.add(BigInteger.ONE);
BigInteger m = n;
map.put(BigInteger.ONE, BigInteger.ZERO);
map.put(BigInteger.ZERO,BigInteger.ZERO);
Solve(m);
System.out.println(map.get(n));
}
}
}
0, 0, 1, 1, 4, 6, 9, 9, 16, 22, 29, 33, 42, 48, 55, 55, 70, 84, 99, 111, |