zoukankan      html  css  js  c++  java
  • ZOJ

    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.

    “math”/

    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
    

    题意:求杨辉三角第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=2000010;
    int main()
    {
        int T; ll ans,N;
        scanf("%d",&T);
        while(T--){
            cin>>N; ans=1;
            if(N==1LL){
                cout<<1<<endl; continue;
            } N--;
            int i;
            rep(j,0,60) if((N&(1LL<<j))) ans=ans*2;
            cout<<ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    c#函数重载
    (1).net c# 一步一步自己写三层代码生成器(主界面及连接数据库界面)
    C#给datagridview某行赋值(转)
    好的软件人员必看的六十本书
    C# 语言规范
    spring 排除指定的类或者包扫描
    java web spring 发送邮件
    spring-data-redis和jedis版本对应收集总结
    python 多线程和多进程基本写法
    python 调用nmap
  • 原文地址:https://www.cnblogs.com/hua-dong/p/10293364.html
Copyright © 2011-2022 走看看