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

题目地址:HDU 5363

题意:给你一个具有n个元素的集合S{1,2,…,n},问集合S的非空子集中元素和为偶数的非空子集有多少个。

思路:解释转自[queuelovestack的专栏]因为集合S中的元素是从1开始的连续的自然数,所以所有元素中奇数个数与偶数个数相同,或比偶数多一个。另外我们要知道偶数+偶数=偶数,奇数+奇数=偶数,假设现在有a个偶数,b个奇数,则

根据二项式展开公式

以及二项式展开式中奇数项系数之和等于偶数项系数之和的定理

可以得到上式

最后的结果还需减去

即空集的情况,因为题目要求非空子集

所以最终结果为

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <map>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const double pi= acos(-1.0);
const double esp=1e-6;
const int mod=1000000007;
LL modxp(LL a,LL b){
    LL res= 1;
    while(b) {
        if(b&1) res=(res*a)%mod;
        a=(a*a)%mod;
        b>>= 1;
    }
    return res;
}

int main() {
    int T,n;
    scanf("%d", &T);
    while(T--) {
        scanf("%d",&n);
        printf("%lld\n",modxp(2,n-1)-1);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-07-31 01:33:31

HDU 5363(2015多校6)-Key Set(快速幂取模)的相关文章

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 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 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 (矩阵快速幂取模----矩阵相乘满足结合律)

http://acm.hdu.edu.cn/showproblem.php?pid=4965 利用相乘的可结合性先算B*A,得到6*6的矩阵,利用矩阵快速幂取模即可水过. 1 #include<iostream> 2 #include<stdio.h> 3 #include<iostream> 4 #include<stdio.h> 5 #define N 1010 6 #define M 1010 7 #define K 6 8 using namespa

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 (快速幂取模)

题意: 给你一个元素为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 元素为1~n的集合有多少个子集的元素和为偶数-思维-(快速幂取模)

题意:一个集合有元素1~n,求他的子集满足这样的条件:子集的所有元素的和是偶数,问有多少个这样的子集 分析: 一个排列组合问题.元素和为偶数,那么奇数肯定要调偶数个,偶数就无所谓了,所以偶数有2^(n/2)种选法,再乘以奇数有(C((n+1)/2,0)+C((n+1)/2,2).....)种选法,再减一,除去空集,注意,上面取奇数的时候用的是(n+1)/2(这里是向下取整的除法),是综合n为偶数和n为奇数两种情况. 组合数性质:C(n,1)+C(n,3)+....=C(n,2)+C(n,4)+.

快速幂取模(2015.7.29)

取余数有%,即 10%3 可以得到1 但是当数比较大时(比如2999999),计算机可能就无法计算 然而,根据数论的结论,我们可以简化一下. 根据a*b mod c = ( (a mod c) * (b mod c) ) mod c 可以得出 an mod b = (a mod b)n mod b 所以,有 an mod b = (a mod b)n mod b = (a mod b)n/22 mod b = (an/2 mod b)2 mod b = (an/2 mod b)2 mod b 如

HDU 4365 正方形格子涂色中心对称轴对称的涂法有多少种-思维-(矩阵坐标关系&amp;快速幂取模)

题意:n*n的格子,涂色,有k种颜料,必须满足旋转任意个90度和翻转之后图片的样子不变,现在已经有m个格子涂过色了,问还有多少种涂法满足上述条件. 分析: 满足上述对称条件,那么涂色的种类问题我们可以放在正方形的一个角来做,因为一个角确定了其他角的颜色也就确定了. 以左上角的下半角为例.共有1+2+....+(n+1)/2个格子,然后记录涂过色的格子对应到这个三角形里的格子数目,用tot来记录,即每输入一个涂过色的格子的坐标我们就在这个三角形里找与之对应的坐标,用vis[][]数组标记是否已经计