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 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.
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
题意:求杨辉三角第i行的奇数个数。
思路:求lucus定理,知道C(N,M)为奇数,当且当N&M=M时。 所以我们求出N的二进制下1的个数num,答案就是2^num.
#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
int main()
{
int T; ll ans,N;
scanf("%d",&T);
while(T--){
cin>>N; ans=;
if(N==1LL){
cout<<<<endl; continue;
} N--;
int i;
rep(j,,) if((N&(1LL<<j))) ans=ans*;
cout<<ans<<endl;
}
return ;
}
ZOJ - 4081: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 ...
- [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 ...
- LeetCode - Pascal's Triangle II
题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return ...
- 【leetcode】Pascal's Triangle I & II (middle)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
随机推荐
- nodejs sequelize 对应数据库操作符的定义
const Op = Sequelize.Op [Op.and]: {a: } // 且 (a = 5) [Op.or]: [{a: }, {a: }] // (a = 5 或 a = 6) [Op. ...
- JS时钟--星期 年 月 日 时 分
var clock = function(clockName){ var mydate = new Date(); var hours = mydate.getHours(); var minutes ...
- Astah Professional安装
asish安装 1● 文件下载 2● 安装图解 3● 破解 replace 4● 测试 success
- Regex Password Validation
You need to write regex that will validate a password to make sure it meets the follwing criteria: A ...
- 一款c语言实现的赛车游戏
博主学习c语言已经有一段时间了,出于对自己学习检验的目的,自制了一款c语言赛车游戏. 由于本质是检验和尝试,所以并没有注重游戏的界面.下文是开发文档,在博主的github网页可以下载源码,注意本项目使 ...
- day03 字符串
今日学习 1.python的基本数据回顾 2.int--数字类型 3.bool值 取值只有True False bool值没有操作 4.字符串 1.python的基本数据回顾 1)int =>整 ...
- VCL界面控件DevExpress VCL发布v18.2.2|附下载
DevExpress VCL Controls是 Devexpress公司旗下最老牌的用户界面套包.所包含的控件有:数据录入,图表,数据分析,导航,布局,网格,日程管理,样式,打印和工作流等,让您快速 ...
- freemarker中的null异常处理以及!与??的使用(转)
原文链接: https://blog.csdn.net/mexican_jacky/article/details/50638062 阅读数:6304 如工程包含: 在user中我们有个角色,那么我们 ...
- 201621123001 《java程序设计》第2周学习总结
1. 本周学习总结 学会String 类的一些用法,比如用subString()截取字符串,频繁对字符串进行修改应使用StringBuilder()等. 学会Array 类的一些用法,比如sort() ...
- Session和Cookie,以及用户登录验证问题。
参考 :https://blog.csdn.net/u010002184/article/details/79416199 https://www.bbsmax.com/A/RnJW72YJqY/ 首 ...