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

来源:互联网 发布:用户生命周期算法 编辑:程序博客网 时间:2024/05/19 23:58

题目链接: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 1000000007LL 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;}


0 0
原创粉丝点击