博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【高精度&想法题】Count the Even Integers @ICPC2017HongKong/upcexam5563#Java
阅读量:5055 次
发布时间:2019-06-12

本文共 4382 字,大约阅读时间需要 14 分钟。

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

转载于:https://www.cnblogs.com/NeilThang/p/9356619.html

你可能感兴趣的文章
memo用法总结
查看>>
AngularJs $watch监听模型变化
查看>>
1005 继续(3n+1)猜想 (25 分)
查看>>
多例设计模式
查看>>
c# await 到底等待的是什么?
查看>>
电子科大 数据结构专题
查看>>
数组的定义和初始化
查看>>
php获取post内容方式
查看>>
ansi 控制码表及颜色代码
查看>>
FMSC 使用理解
查看>>
用PHP向数据库中添加数据
查看>>
使用spring集成hibernate
查看>>
@ 与 ^ 运算符
查看>>
MYSQL函数、高级应用
查看>>
LeetCode 刷题顺序表
查看>>
013-linux系统管理——系统资源查看
查看>>
css弹窗动画效果
查看>>
vs自己主动生成的WebService配置文件在部署到IIs6后,服务调用失败的解决方法
查看>>
CentOS 7 执行级别的切换
查看>>
Spring AOP
查看>>