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 ...
随机推荐
- Linux下修改Mysql最大并发连接数
输入的命令如下: /usr/local/mysql/bin/mysqladmin -uroot -pyyyyyy variables |grep max_connections nano /etc/m ...
- Swing界面组件的通用属性
----------------siwuxie095 Swing 界面组件(控件)的通用属性: (1)enabled:启用/禁用 ...
- String类型的理解
引用:https://www.cnblogs.com/binyue/p/3862276.html java语言中: 变量除了八大基本数据类型(byte,short,int,long,boolean,f ...
- gearman安装问题总结
解决configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers. yum ...
- C语言-郝斌笔记-001求二次方程的根
求二次方程的根 #include <stdio.h > #include<math.h> int main(void) { //把三个系数保存到计算机中 ; //=不表示相等, ...
- IP地址及子网掩码计算
主机号全0表示网络号,主机号全1表示广播地址 我们都知道,IP是由四段数字组成,在此,我们先来了解一下3类常用的IP A类IP段 0.0.0.0 到127.255.255.255 B类IP段 128. ...
- Siemens3508手机AT指令发送短信的实验
凡夫 最近利用Siemens3508旧手机做了AT指令发送短信的实验.有人可能认为我费那么大劲折腾累不累,告诉你这可是废物再利用,可以利用旧手机里的GSM/GPRS模块做无线远程多点分布数据采集.监控 ...
- url-pattern 的设置与匹配
- Java50道经典习题-程序50 文件IO
题目:有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,将原有的数据和计算出的平均分数存放在磁盘文件"stud"中. impo ...
- Go语言技术教程:Redis介绍安装和使用
Redis介绍 我们日常的开发,数据都需要进行持久化存储,常见的持久化存储有很多种,比如数据库,文件,计算机内存,甚至云服务器等都是持久化存储数据的方式.而就数据库而言,经常又会被人们分为关系型数据库 ...