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;
}
目录
相关文章
codeforces 285C - Building Permutation
题目大意是有一个含n个数的数组,你可以通过+1或者-1的操作使得其中的数是1--n中的数,且没有重复的数。 既然是这样的题意,那么我就应该把原数组中的数尽量往他最接近1--n中的位置放,然后求差绝对值之和,但有多个数,怎么使他们和最小,这样就要对其进行排序了,直接按大小给它们安排好位置,然后计算。
34 0
|
6月前
Knight Moves(POJ2243)
Knight Moves(POJ2243)
|
图形学
hdu1086 You can Solve a Geometry Problem too(判断线段相交)
hdu1086 You can Solve a Geometry Problem too(判断线段相交)
71 0
AtCoder Beginner Contest 176 D - Wizard in Maze(01BFS)
AtCoder Beginner Contest 176 D - Wizard in Maze(01BFS)
115 0
AtCoder Beginner Contest 214 F - Substrings(subsequence DP)
AtCoder Beginner Contest 214 F - Substrings(subsequence DP)
96 0
|
机器学习/深度学习 人工智能 Java
AtCoder Beginner Contest 215 D - Coprime 2 (质因子分解 gcd)
AtCoder Beginner Contest 215 D - Coprime 2 (质因子分解 gcd)
108 0
|
人工智能
atcoder AtCoder Beginner Contest 210 D - National Railway(dp)
atcoder AtCoder Beginner Contest 210 D - National Railway(dp)
116 0
ZOJ - Summer 2018 - Contest 1 by SBconscious - Problems - 1001: Saber
ZOJ - Summer 2018 - Contest 1 by SBconscious - Problems - 1001: Saber
94 0