J - Key Set
soda has a set
S
S with
n
n 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 of
S
S are key set.
The first line contains an integer n n (1≤n≤109) (1≤n≤109), the number of integers in the set.
4 1 2 3 4
0 1 3 7
思路:(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 1000000007 typedef 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; }