ZOJ - Summer 2018 - Contest 2 by Astolfo - Problems - 1002: Hazard and The Triangle

简介: ZOJ - Summer 2018 - Contest 2 by Astolfo - Problems - 1002: Hazard and The Triangle

Hazard and The Triangle


Time Limit: 1 Second      Memory Limit: 262144 KB      Score: 1


I'm Eden Hazard,the so-called "superb passer". In a match in the group stage,three players surrounding me tried to block my advance,which,of course,failed. However,at that moment,they formed a triangle,which reminds me of an interesting problem about Pascal's triangle.


The first element and the last element of each row in a 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.Here's an example.


image.png


I wonder how many elements in the -th row of a Pascal's Triangle are odd numbers.


Input


There are several test cases (no more than ).

There is an integer  in each case.


Output


For each test case, output the number of odd numbers in the -th line.


Input


3
4
5

Output

2
4
2

Output


解题思路:杨辉三角第 n 行的奇数个数,2^( n-1 中二进制 1 的个数)。

AC 代码


#include<bits/stdc++.h>
#include<cmath>
#define mem(a,b) memset(a,b,sizeof a);
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ll numOf1(ll n)
{
    ll cnt = 0;
    while(n){
        cnt++;  // 只要 n 不为 0,则其至少有一个 1
        n = n & (n - 1);
    }
    return cnt;
}
int main()
{
    ll n;
    while(~scanf("%lld",&n))
    {
        if(n<=0) puts("0");
        else cout<<(1LL<<NumberOf1(n-1))<<endl;
    }
    return 0;
}
目录
相关文章
ZOJ - Summer 2018 - Contest 1 by SBconscious - Problems - 1001: Saber
ZOJ - Summer 2018 - Contest 1 by SBconscious - Problems - 1001: Saber
107 0
LeetCode 407. Trapping Rain Water II
我们把最外面的四边当成四面墙,想象海水面不断的升高,首先会浸过墙面最低的格子,如果墙面最低格子的四周(出了在墙面的格子)有比它矮的格子,那么这就可以形成一个蓄水池,蓄水池的最高高度就是墙面最低的格子,于是我们计算这个蓄水池可以获得的蓄水量;然后这个蓄水池被添加到墙面中;继续在墙面中找最低的格子;
126 0
LeetCode 407. Trapping Rain Water II
|
Java C++ Python
[LeetCode] Trapping Rain Water
Note: The following idea is in fact from the last answer in this link, which leads to a clean code. I just reorganize it and add some explanations.
1073 0
|
索引
LeetCode 42 Trapping Rain Water
给定n个非负整数,每个非负整数表示一个宽度为1的柱子,计算下雨后能够捕获多少水.
83 0
LeetCode 42 Trapping Rain Water
LeetCode - 42. Trapping Rain Water
42. Trapping Rain Water  Problem's Link  ---------------------------------------------------------------------------- Mean:  在坐标上给你一些竖直放置的条形积木,问你这个积木能够容纳多少液体.
908 0
|
机器学习/深度学习
1257:Knight Moves 2021-01-09
1257:Knight Moves 2021-01-09
|
机器学习/深度学习 算法
917:Knight Moves
题目链接:http://noi.openjudge.cn/ch0205/917/ 原题应该是hdu 1372 总时间限制: 1000ms  内存限制: 65536kB 描述 BackgroundMr Somurolov, fabulous chess-gamer indeed, asserts ...
1060 0