前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【HDU 5363】Key Set(和为偶数的子集个数)

【HDU 5363】Key Set(和为偶数的子集个数)

作者头像
饶文津
发布2020-06-02 11:37:35
4420
发布2020-06-02 11:37:35
举报
文章被收录于专栏:饶文津的专栏饶文津的专栏

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个

接下来用快速幂即可。

代码:

代码语言:javascript
复制
#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\n",ans-1);
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-02-06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Description
  • Input
  • Output
  • Sample Input
  • Sample Output
  • 题意:
  • 分析:
  • 代码:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档