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 ...
随机推荐
- Weblogic服务端请求伪造漏洞(SSRF)和反射型跨站请求伪造漏洞(CSS)修复教程
一.服务端请求伪造漏洞 服务端请求伪造(Server-Side Request Forgery),是指Web服务提供从用户指定的URL读取数据并展示功能又未对用户输入的URL进行过滤,导致攻击者可借助 ...
- Windows和Linux创建软链接和硬链接
1.Wondows创建软链接和硬链接 mklink [/d] [/h] link target /d--创建目录软链接:默认为文件软链接:创建目录链接时必须使用该选项不然创出的软链接无效 /h--创建 ...
- PHP配置方法
由于php是一个zip文件(非install版),安装较为简单,解压就行.把解压的 php5.2.1-Win32重命名为 php5.并复制到C盘目录下.即安装路径为 c:\php5 1 找到php目录 ...
- php数组判断值相等时出现的次数,0,1,2这样的
方法1: $arr = array(array( 0=> array('a'=>1), 1=> array('a'=>3), 2=> array('a'=>4), ...
- nyoj-0613-免费馅饼(dp)
nyoj-0613-免费馅饼 G. 免费馅饼 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy的人品实在是太好了,这馅饼别处都不掉,就掉落在 ...
- Uboot代码分析
(1)确定链接脚本文件:uboot根目录下Makefile中的LDSCRIPT宏值,就是指定链接脚本(如:arch/arm/cpu/u-boot.lds)路径用的.(2)从脚本文件找入口: 在链接脚本 ...
- Linux电源管理-Linux regulator framework概述
前言 1. 什么是regulator? regulator翻译为"调节器",分为voltage regulator(电压调节器)和current(电流调节器).一般电源 ...
- Asp.Net 中 HTTP 和 HTTPS 切换
Asp.Net 中 HTTP 和 HTTPS 切换 目的 HTTP,超文本传输协议,明文传输,无状态,服务器默认端口80 HTTPS,具有SSL加密的HTTP,加密传输,需要申请ca证书,服务器默 ...
- Java面向对象的三大特性之一 多态
多态: 子类重写父类方法 1)位置:子类和父类中有同名的方法 2)方法名相同,返回类型和修饰符相同,参数列表相同 方法体不同 多态的优势和应用场合 多态:同一个引用类型,使用不同的实例而执 ...
- struts请求参数注入的三种方式
.请求参数的注入 在Struts2框架中,表单的提交的数据会自动注入到与Action对象相对应的属性.它与Spring框架中的IoC的注入原理相同,通过Action对象为属性提供setter方法注入 ...