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个

接下来用快速幂即可。

代码:

#include<stdio.h>
#define ll long long
const ll M=1e9+;
ll t,n;
int main(){
scanf("%lld",&t);
while(t--){
scanf("%lld",&n);
ll k=,ans=;
n--;
while(n){
if(n&)ans=(ans*k)%M;
k=(k*k)%M;
n>>=;
}
printf("%lld\n",ans-);
}
}

【HDU 5363】Key Set的更多相关文章

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

    题 Description soda has a set $S$ with $n$ integers $\{1, 2, \dots, n\}$. A set is called key set if ...

  2. 【HDU 5647】DZY Loves Connecting(树DP)

    pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...

  3. 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题

    [HDU  3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...

  4. -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】

    [把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...

  5. 【HDU 2196】 Computer(树的直径)

    [HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...

  6. 【HDU 2196】 Computer (树形DP)

    [HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...

  7. 【HDU 5145】 NPY and girls(组合+莫队)

    pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Other ...

  8. 【hdu 1112】The Proper Key

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s) ...

  9. 【hdu 1043】Eight

    [题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=1043 [题意] 会给你很多组数据; 让你输出这组数据到目标状态的具体步骤; [题解] 从12345 ...

随机推荐

  1. 二分图最大匹配算法-Hopcroft-Karp模板

    时间复杂度:O((√V)*E) #include<stdio.h> #include<string.h> ,M=,INF=0x3f3f3f3f; int dx[N],dy[M] ...

  2. [转] 国外程序员整理的 C++ 资源大全

    关于 C++ 框架.库和资源的一些汇总列表,由 fffaraz 发起和维护. 内容包括:标准库.Web应用框架.人工智能.数据库.图片处理.机器学习.日志.代码分析等. 标准库 C++标准库,包括了S ...

  3. Vernam密码

    Vernam加密法也称一次一密(One-Time-Pad),用随机的非重复的字符集合作为输出密文.这里最重要的是,一旦使用了变换的输入密文,就不再在任何其他消息中使用这个输入密文(因此是一次性的).输 ...

  4. Android View坐标getLeft, getRight, getTop, getBottom

    1  引起疑惑 分析视图invalidate流程的过程中发现view的left, right, top, bottom跟自己理解的不一样,现在想分析一下这几个值具体的含义. 2  理解坐标,位置概念 ...

  5. JavaScript---闭包和作用域链

    作用域和作用域链: 参考文章 :http://www.cnblogs.com/malinlin/p/6028842.html  http://www.cnblogs.com/lhb25/archive ...

  6. action中result没有值

    action中result没有值,访问action会输出action中的所有数据,输出类型为.action类型 .

  7. Node.js开发环境搭建

    1.安装express npm install express -g 2.express33.6以后把express-generator分离出来了,所以还需安装express-generator,否则 ...

  8. 【MySQL】Linux MySQL学习记录

    1.查看日志存放路径 show variables like 'general_log_file'; 2.查看日志是否开启 show global variables like 'log_bin%'; ...

  9. py变量

        1, python以数据为主 x=2,是给数据2开辟了个空间, X指向了2 y=x ,即y指向了2 x=5 ,x重新赋值 但是y依旧是原来的

  10. H5实现拍照并上传

    <!DOCTYPE HTML><html><head>    <meta charset="UTF-8">    <meta ...