ZOJ-Little Sub and Pascal's Triangle(思维规律)
Little Sub is about to take a math exam at school. As he is very confident, he believes there is no need for a review.
Little Sub's father, Mr.Potato, is nervous about Little Sub's attitude, so he gives Little Sub a task to do. To his surprise, Little Sub finishes the task quickly and perfectly and even solves the most difficult problem in the task.
Mr.Potato trys to find any possible mistake on the task paper and suddenly notices an interesting problem. It's a problem related to Pascal's Triangle.
The definition of Pascal's Triangle is given below:
The first element and the last element of each row in Pascal's Triangle is , and the -th element of the -th row equals to the sum of the -th and the -th element of the -th row.
According to the definition, it's not hard to deduce the first few lines of the Pascal's Triangle, which is:
......
In the task, Little Sub is required to calculate the number of odd elements in the 126th row of Pascal's Triangle.
Mr.Potato now comes up with a harder version of this problem. He gives you many queries on this problem, but the row number may be extremely large. For each query, please help Little Sub calculate the number of odd elements in the -th row of Pascal's Triangle.
Input
There are multiple test cases. The first line of the input contains an integer (), indicating the number of test cases. For each test case:
The first and only line contains an integer (), indicating the required row number in Pascal's Triangle.
Output
For each test case, output the number of odd numbers in the -th line.
Sample Input
3
3
4
5
Sample Output
2
4
2
题意:求出杨辉三角第n行的奇数数量
思路:将n先减一,然后求出此时n的二进制中1的数量cnt,2的cnt次方即为答案(注意longlong不要用I64d,要用lld)
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main(){
int T;
cin>>T;
while(T--){
long long int m;
scanf("%lld",&m);
long long int cnt=0;
m-=1;
while(m)
{
cnt++;
m-=m&(-m);
}
long long ans=1ll<<cnt;
printf("%lld\n",ans);
}
return 0;
}
ZOJ-Little Sub and Pascal's Triangle(思维规律)的更多相关文章
- ZOJ 4081 Little Sub and Pascal's Triangle 题解
ZOJ 4081 Little Sub and Pascal's Triangle 题解 题意 求杨辉三角第n行(从1开始计数)有几个奇数. 考察的其实是杨辉--帕斯卡三角的性质,或者说Gould's ...
- ZOJ - 4081:Little Sub and Pascal's Triangle (结论)
Little Sub is about to take a math exam at school. As he is very confident, he believes there is no ...
- 118. Pascal's Triangle杨辉三角形(全部/一行)
[抄题]: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...
- [LeetCode] Pascal's Triangle II 杨辉三角之二
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...
- [LeetCode] Pascal's Triangle 杨辉三角
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- 【leetcode】Pascal's Triangle II
题目简述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Retur ...
- 【leetcode】Pascal's Triangle
题目简述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...
- LeetCode 118 Pascal's Triangle
Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...
- LeetCode 119 Pascal's Triangle II
Problem: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Ret ...
随机推荐
- java获取多个汉字的拼音首字母
本文属于http://java.chinaitlab.com/base/803353.html原创!!! public class PinYin2Abbreviation { // 简体中文的编码范围 ...
- python爬虫--编码问题y
1)中文网站爬取下来的内容中文显示乱码 Python中文乱码是由于Python在解析网页时默认用Unicode去解析,而大多数网站是utf-8格式的,并且解析出来之后,python竟然再以Unicod ...
- Ajax01 什么是ajax、获取ajax对象、ajax对象的属性和方法、编程步骤、缓存问题、乱码问题
目录 1 什么是ajax 2 获取ajax对象 3 ajax对象的属性和方法 4 使用ajax的编程步骤 5 缓存问题 6 乱码问题 1 什么是ajax ajax是一种用来改善用户体验的技术,其本质是 ...
- [转]JQuery 如何选择带有多个class的元素
比如下面代码需要选择同时带有这几个class的元素,怎么写? 1 <div class="modal fade in"></div> A: 1. 依次过滤 ...
- Android简单的monkey测试
Android中的monkey测试是比较常用的工具了,设定好monkey之后,让手机跑一晚上,第二天分析日志,这样能更加有效率的工作. monkey测试的工具比较多,基本的方法都差不多. 抓取日志: ...
- adb 无法调试的问题,ADB server didn't ACK,* failed to start daemon *
The connection to adb is down, and a severe error has occured. You must restart adb and Eclipse. Ple ...
- angular 分页2
http://www.alliedjeep.com/2547.htm AngularJS Code (Users.js) var Users = angular.module('Users', []) ...
- 遍历一个二维数组的简便方法(减少foreach次数)
在一些特定场合可以使用下, 还是有局限性 输出结果 : 另一种场景 : 输出结果 : 更复杂的场景 : 输出结果 :
- Openwrt单独编译某一个模块而不是整个固件
make package/rt2860v2/compile 就是在make menuconfig那个目录下执行此命令就会编译rt2860v2这个模块
- 「ZOJ 1354」Extended Lights Out「高斯消元」
题意:给定一个\(5\times 6\)的棋盘的\(01\)状态,每次操作可以使它自己和周围四个格子状态取反,求如何操作,输出一个\(01\)矩阵 题解:这题可以通过枚举第一行的状态然后剩下递推来做, ...