hdu 5363 Key Set (2015多校第六场第11题)找规律推公式

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5363

题意:给你一个有1到n的n个数的集合,求这个集合的非空子集的子集所有元素的和为偶数的子集个数

思路:因为和为偶数,所以一定是由2*x个奇数+y个偶数组成,从中就可以推出公式为2^(n-1)-1

代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>

using namespace std;
#define LL __int64
#define mod 1000000007

LL quick_pow(LL n)
{
    LL ans=1;
    LL a=2;
    while(n)
    {
        if(n&1)
            ans=ans*a%mod;
        a=a*a%mod;
        n>>=1;
    }
    return ans;
}

int main()
{
    int T,n;
    while(scanf("%d",&T)==1)
    {
        while(T--)
        {
            scanf("%d",&n);
            printf("%I64d\n",quick_pow(n-1)-1);
        }
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章