zoukankan      html  css  js  c++  java
  • 【HDU 5363】Key Set(和为偶数的子集个数)

    Description

    soda has a set $S$ with $n$ integers ${1, 2, dots, n}$. A set is called key set if the sum of integers in the set is an even number. He wants to know how many nonempty subsets of $S$ are key set.
     

    Input

    There are multiple test cases. The first line of input contains an integer $T$ $(1 le T le 10^5)$, indicating the number of test cases. For each test case: 

    The first line contains an integer $n$ $(1 le n le 10^9)$, the number of integers in the set.
     

    Output

    For each test case, output the number of key sets modulo 1000000007.
     

    Sample Input

    4
    1
    2
    3
    4
     

    Sample Output

    0
    1
    3
    7

    题意:

    求1 2 3 ... n 的 所有子集中和为偶数的子集个数,mod 1000000007

    分析:

    数学归纳法证明和为偶数的子集有2n-1-1个:

    1. 当n=1时,有a1=0个
    2. 假设n=k时,有ak=2k-1-1个子集和为偶数,
    • 若k+1为偶数,则ak个子集加上这个偶数,和还是偶数,这个偶数单独一个集合,和就是这个偶数,ak+1=ak*2+1=2k-1
    •  若k+1为奇数,前k个数共有2k个子集,其中一个空集和为0,和为奇数的子集有2k-1-ak=2k-1个,和为奇数的子集加上k+1这个数,和变成了偶数,因此ak+1=ak+2k-1=2k-1

    综合1,2得系列1 2 ... n 和为偶数的子集有2n-1-1个

    接下来用快速幂即可。

    代码:

    #include<stdio.h>
    #define ll long long
    const ll M=1e9+7;
    ll t,n;
    int main(){
        scanf("%lld",&t);
        while(t--){
            scanf("%lld",&n);
            ll k=2,ans=1;
            n--;
            while(n){
                if(n&1)ans=(ans*k)%M;
                k=(k*k)%M;
                n>>=1;
            }
            printf("%lld
    ",ans-1);
        }
    }
  • 相关阅读:
    less css用法思维导图
    javascript模块化编程规范
    行高:line-height图文解析
    CSS细节
    Emmet
    常见的浏览器兼容问题
    一条在没有水的环境下坚持了四年生存下来的鱼
    纪念:一高那些年
    水墨青花
    float浮动
  • 原文地址:https://www.cnblogs.com/flipped/p/5183794.html
Copyright © 2011-2022 走看看