时间限制: 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 0 0 0 0 1 0 1 0 1 0 1

1 1 1 1 1 1 1 1 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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));
}
}
}

【高精度&想法题】Count the Even Integers @ICPC2017HongKong/upcexam5563#Java的更多相关文章

  1. HDU 4972 Bisharp and Charizard 想法题

    Bisharp and Charizard Time Limit: 1 Sec  Memory Limit: 256 MB Description Dragon is watching NBA. He ...

  2. uva 748 Exponentiation 浮点数乘方运算 高精度水题

    输入的前六位数表示一个小数,然后输入一个数表示几次方.要求用高精度算出结果. 高精度水题,主要注意处理小数点,先在输入时把小数点提取出来并记录位置,用普通乘法计算出结果后由后向前计算位置添加小数点. ...

  3. CodeForces 111B - Petya and Divisors 统计..想法题

    找每个数的约数(暴力就够了...1~x^0.5)....看这约数的倍数最后是哪个数...若距离大于了y..统计++...然后将这个约数的最后倍数赋值为当前位置...好叼的想法题.... Program ...

  4. HDU - 5806 NanoApe Loves Sequence Ⅱ 想法题

    http://acm.hdu.edu.cn/showproblem.php?pid=5806 题意:给你一个n元素序列,求第k大的数大于等于m的子序列的个数. 题解:题目要求很奇怪,很多头绪但写不出, ...

  5. AIZU 2560 [想法题]

    表示敲完多项式乘法&高精度乘法两道FFT模板题后就开始来磕这题了 这题相对而言应该不算模板题 不过神犇们肯定还是一眼看穿 如果原OJ访问速度较慢的话可以考虑戳这里 http://acm.hus ...

  6. Codeforces 599C Day at the Beach(想法题,排序)

    C. Day at the Beach One day Squidward, Spongebob and Patrick decided to go to the beach. Unfortunate ...

  7. leetcode第37题--Count and Say

    题目:(据说是facebook的面试题哦) The count-and-say sequence is the sequence of integers beginning as follows:1, ...

  8. LeetCode算法题-Count Binary Substrings(Java实现)

    这是悦乐书的第293次更新,第311篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第161题(顺位题号是696).给定一个字符串s,计算具有相同数字0和1的非空且连续子串 ...

  9. LeetCode算法题-Count Primes(Java实现)

    这是悦乐书的第190次更新,第193篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第49题(顺位题号是204).计算小于非负数n的素数的数量.例如: 输入:10 输出:4 ...

随机推荐

  1. MediatR

    1.MediatR是什么? 微软官方eshopOnContainer开源项目中使用到了该工具, mediatR 是一种中介工具,解耦了消息处理器和消息之间耦合的类库,支持跨平台 .net Standa ...

  2. file标签 - 图片上传前预览 - FileReader & 网络图片转base64和文件流

    记得以前做网站时,曾经需要实现一个图片上传到服务器前,先预览的功能.当时用html的<input type="file"/>标签一直实现不了,最后舍弃了这个标签,使用了 ...

  3. BZOJ3796 Mushroom追妹纸 字符串 SA KMP

    原文链接https://www.cnblogs.com/zhouzhendong/p/9253173.html 题目传送门 - BZOJ3796 题意 找一个串 $w$ 满足: 1.$w$ 是 $s_ ...

  4. List接口相对于Collection接口的特有方法

    [添加功能] 1 void add(int index,Object element); // 在指定位置添加一个元素. [获取功能] 1 Object get(int index); // 获取指定 ...

  5. 框架MyBatis

    ByBatis MyBatis是Apache的一个开源项目iBatis,iBatis3.x 正式更名为MyBatis ,代码于2013年11月迁移到Github.它是一个基于Java的持久层框架(连数 ...

  6. Python 动态加载并下载"梨视频"短视频

    下载链接:http://www.pearvideo.com/category_1 import requests from lxml import etree import re from urlli ...

  7. day41 mycql 函数

    一些经典的练习题,以及函数的简单用法,內建函数 -- 函数 python函数 def fun1(a1,a2,a3): sum = a1+a2+a3 return sum fun1(1,2,3) jav ...

  8. gradle上传本地文件到远程maven库(nexus服务器)

    自定义aar-upload.gradle文件 artifacts { archives file('./build/outputs/aar/Lib_ads-baidu-debug.aar') } up ...

  9. CLR Via第一 章 知识点整理(4) FCL、CTS、CLI和CLS

    FCL(Framework Class Library) Framework 类库: FCL是 .net Framework 包含的一组DLL程序集的统称,FCL包含了提供了很多功能,关于这一部分没有 ...

  10. spring_AOP_annotation

    例子下载 beans.xml 首先,在配置文件配置好下面的配置 <?xml version="1.0" encoding="UTF-8"?> < ...