hdu 5363 Key Set 快速幂

Problem Description

soda has a set S with n integers {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 are key set.

Input

There are multiple test cases. The first line of input contains an integer T (1≤T≤105), indicating the number of test cases. For each test case:

The first line contains an integer n (1≤n≤109), 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

题意:给出一个数n,表示一个集合中有n个数1-n;找出这个集合的非空子集的个数,并且这些非空子集中所有元素相加为偶数。

大家都知道n个数的非空子集有2的n次方减1个,那么给原集合中1剔除来,剩下的集合就有2的n-1次方-1个。那么剩下的集合中所有元素相加之和为奇数的加上1就为偶数了,剩下为偶数的就不加1.所以结果就是2的n次方-1。由于这个数据非常大,就要用快速幂。

快速幂的作用:减少计算时间。在计算中就可以取摸。取摸原理假设 m=x*y;那么 m%n=((x%n)*(y%n))%n;

#include<cstdio>
#include<cstring>
using namespace std;
long long t,n,ans,y;
int f()
{
    ans=1;
    y=2;;
    n-=1;
    while (n)
    {
        if (n&1) ans=(ans*y)%1000000007;
        y=(y*y)%1000000007;
        n>>=1;
    }
    return ans-1;
}
int main()
{
    scanf("%lld",&t);
    while (t--)
    {
        scanf("%lld",&n);
        printf("%lld\n",f());
    }
    return 0;
}
时间: 2024-07-31 01:33:26

hdu 5363 Key Set 快速幂的相关文章

HDU 5363 Key Set(快速幂取余)

Key Set Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 362    Accepted Submission(s): 238 Problem Description soda has a set S with n integers {1,2,-,n}. A set is called key set if the sum o

HDU 5363 Key Set(快速幂取模)

Key Set Problem 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 multipl

hdu 3221 Brute-force Algorithm(快速幂取模,矩阵快速幂求fib)

http://acm.hdu.edu.cn/showproblem.php?pid=3221 一晚上搞出来这么一道题..Mark. 给出这么一个程序,问funny函数调用了多少次. 我们定义数组为所求:f[1] = a,f[2] = b, f[3] = f[2]*f[3]......f[n] = f[n-1]*f[n-2].对应的值表示也可为a^1*b^0%p,a^0*b^1%p,a^1*b^1%p,.....a^fib[n-3]*b^fib[n-2]%p.即a,b的指数从n=3以后与fib数列

Hdu 4965(矩阵快速幂)

题目链接 Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 87    Accepted Submission(s): 39 Problem Description One day, Alice and Bob felt bored again, Bob knows Alice is a

HDU 5363 Key Set【快速幂取模】

Key Set Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1886    Accepted Submission(s): 990 Problem Description soda has a set S with n integers {1,2,-,n}. A set is called key set if the sum

hdu 5363 Key Set (快速幂取模)

题意: 给你一个元素为1到n的集合,让你求有多少个非空子集,子集内的元素之和为偶数. 解析: 子集中满足元素之和为偶数那么得满足几何中的奇数必须为偶数个. 那么偶数的情况可以任意取.一个几何中有 n/2 个偶数,有 (n+1)/2个奇数. 那么最终的结果为∑n/2i=1Cin/2?∑(n+1)/2j=1Cj(n+1)/2=2n?1?1 由于结果比较大,所以要用到快速幂取摸. my code #include <cstdio> #include <cstring> #include

HDU 5363(2015多校6)-Key Set(快速幂取模)

题目地址:HDU 5363 题意:给你一个具有n个元素的集合S{1,2,-,n},问集合S的非空子集中元素和为偶数的非空子集有多少个. 思路:解释转自[queuelovestack的专栏]因为集合S中的元素是从1开始的连续的自然数,所以所有元素中奇数个数与偶数个数相同,或比偶数多一个.另外我们要知道偶数+偶数=偶数,奇数+奇数=偶数,假设现在有a个偶数,b个奇数,则 根据二项式展开公式 以及二项式展开式中奇数项系数之和等于偶数项系数之和的定理 可以得到上式 最后的结果还需减去 即空集的情况,因为

hdu 5363 Key Set

http://acm.hdu.edu.cn/showproblem.php?pid=5363 Key Set Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 549    Accepted Submission(s): 338 Problem Description soda has a set S with n integers {

hdu 5667 Sequence 矩阵快速幂

题目链接:hdu 5667 Sequence 思路:因为fn均为a的幂,所以: 这样我们就可以利用快速幂来计算了 注意: 矩阵要定义为long long,不仅仅因为会爆,还会无限超时 要对a%p==0特判,以为可能出现指数%(p-1)==0的情况,那么在快速幂的时候返回的结果就是1而不是0了 /************************************************************** Problem:hdu 5667 User: youmi Language: