【高精度&想法题】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 1Now, 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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 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));
}
}
}
【高精度&想法题】Count the Even Integers @ICPC2017HongKong/upcexam5563#Java的更多相关文章
- HDU 4972 Bisharp and Charizard 想法题
Bisharp and Charizard Time Limit: 1 Sec Memory Limit: 256 MB Description Dragon is watching NBA. He ...
- uva 748 Exponentiation 浮点数乘方运算 高精度水题
输入的前六位数表示一个小数,然后输入一个数表示几次方.要求用高精度算出结果. 高精度水题,主要注意处理小数点,先在输入时把小数点提取出来并记录位置,用普通乘法计算出结果后由后向前计算位置添加小数点. ...
- CodeForces 111B - Petya and Divisors 统计..想法题
找每个数的约数(暴力就够了...1~x^0.5)....看这约数的倍数最后是哪个数...若距离大于了y..统计++...然后将这个约数的最后倍数赋值为当前位置...好叼的想法题.... Program ...
- HDU - 5806 NanoApe Loves Sequence Ⅱ 想法题
http://acm.hdu.edu.cn/showproblem.php?pid=5806 题意:给你一个n元素序列,求第k大的数大于等于m的子序列的个数. 题解:题目要求很奇怪,很多头绪但写不出, ...
- AIZU 2560 [想法题]
表示敲完多项式乘法&高精度乘法两道FFT模板题后就开始来磕这题了 这题相对而言应该不算模板题 不过神犇们肯定还是一眼看穿 如果原OJ访问速度较慢的话可以考虑戳这里 http://acm.hus ...
- Codeforces 599C Day at the Beach(想法题,排序)
C. Day at the Beach One day Squidward, Spongebob and Patrick decided to go to the beach. Unfortunate ...
- leetcode第37题--Count and Say
题目:(据说是facebook的面试题哦) The count-and-say sequence is the sequence of integers beginning as follows:1, ...
- LeetCode算法题-Count Binary Substrings(Java实现)
这是悦乐书的第293次更新,第311篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第161题(顺位题号是696).给定一个字符串s,计算具有相同数字0和1的非空且连续子串 ...
- LeetCode算法题-Count Primes(Java实现)
这是悦乐书的第190次更新,第193篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第49题(顺位题号是204).计算小于非负数n的素数的数量.例如: 输入:10 输出:4 ...
随机推荐
- 伪分布式hbase2.6.5和hbase1.1.2的配置
1.注意hadoop和hbase的版本兼容问题 目前测试用:hadoop 2.6.5 Hbase 1.1.2 2.创建hadoop用户 Sudo useradd –m hadoop –s /bin/ ...
- BZOJ4566 [Haoi2016]找相同字符 字符串 SAM
原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ4566.html 题目传送门 - BZOJ4566 题意 给定两个字符串 $s1$ 和 $s2$ ,问有 ...
- P1057 传球游戏 dp
题目描述 上体育课的时候,小蛮的老师经常带着同学们一起做游戏.这次,老师带着同学们一起做传球游戏. 游戏规则是这样的:nn个同学站成一个圆圈,其中的一个同学手里拿着一个球,当老师吹哨子时开始传球,每个 ...
- 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。
示例 1: 输入: [1,2,3,4,5,6,7] 和 k = 3 输出: [5,6,7,1,2,3,4] 解释: 向右旋转 1 步: [7,1,2,3,4,5,6] 向右旋转 2 步: [6,7,1 ...
- 044 SimpleDateFormat的线程安全问题与解决方案
这个问题,以前好像写过,不过现在这篇文章,有一个重现的过程,还是值得读一读的. URL:SimpleDateFormat的线程安全问题与解决方案
- mvc 过滤器篇
1,重写类作为特性 2 写一个controller ,让其他controller集成 四种过滤器 1身份验证过滤器 2异常处理过滤器 3行为过滤器 4结果过滤器
- 整合django和bootstrap框架
环境: python版本:2.7.8 django版本:1.7.1 bootstrap版本:3.3.0 首先github上面有两个开源的项目用来整合django和bootstrap. https:// ...
- HDU-1013的解题报告
题目来源:hdu-1013 题意:正整数的数字根是通过求整数的整数而求出的.如果结果值是一个位数,那么这个数字就是数字根.如果结果值包含两个或多个数字,则将这些数字相加并重复该过程.只要需要获得一个数 ...
- startActivity进行Hook
--摘自<android插件化开发指南> 1.Activity的startActivity和Context的startActivity都是在app进程中通知AMS要启动哪个Activity ...
- jOOR
--摘自<android插件化开发指南> 1.jOOR库就一个Reflect.java类很重要 2.Reflect.java包括6个核心方法 1)on:包裹一个类或者对象,表示在这个类或对 ...