首页 > 学院 > 开发设计 > 正文

HPUOJ---2017寒假作业--专题-1/J-Key Set

2019-11-08 00:56:54
字体:
来源:转载
供稿:网友

J - Key Set

 soda has a set SS with nn integers {1,2,…,n}{1,2,…,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 ofSS are key set.InputThere are multiple test cases. The first line of input contains an integer T(1≤T≤105)(1≤T≤105), indicating the number of test cases. For each test case: The first line contains an integer n(1≤n≤109)(1≤n≤109), the number of integers in the set.OutputFor each test case, output the number of key sets modulo 1000000007.Sample Input
41234Sample Output
0137
思路:(1):集合各子集中,各数字之和为偶数的非空子集个数为2的n-1次方-1;那么,和为奇数的非空子集
             个数为2的n-1次方。
      (2):最后要对一个数求余,用到同余定理,(a+b)%C=(a%c+b%c)%c。
      (3):2的n-1次方对一个数取余,用到快速幂。
#include<cstdio>#define mod 1000000007typedef long long LL;LL Quick_MI(LL a,LL b,LL c){	int s=1;	while(b)	{		a=a%c;		if(b&1)                  //按位与运算,二进制形式,判断b的奇偶		{			s=(s*a)%c;		}		a=(a*a)%c;		b=b/2;	}	return s;                     //快速幂}int main(){	int T;	long long p,m,n;	scanf("%d",&T);	while(T--)	{		scanf("%lld",&n);		p=Quick_MI(2,n-1,mod);		m=(p-1)%mod;          //同余定理		PRintf("%lld/n",m);	}	return 0;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表