HDOJ 5363 Key Set 【快速幂】

HDOJ 5363 Key Set 【快速幂】

题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5363


题目就是要求从1~n中所有数组成的各子集中,子集元素和为偶数的个数
1、两个奇数可以组成一个偶数
2、集合中元素不存在重复
Solution
Let a be the number of even integers and b be the number of old integers. The answer is:

(-1是指去掉空集的情况)
得出结果为2^(n-1)-1
所以输入后直接快速幂即可

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
int T, n;
const int mod = 1000000007;
typedef long long ll;

ll quickPower (int a, int b, int mod) { // a ^ b % mod
    ll ans = 1; // answer
    ll aBin = a; // a^1, a^2, a^4, a^8 ...
//    while(b > 0) {
//        if(b & 1) ans = ans * aBin % mod; // only calculate when the value is 1
//        b >>= 1; // right shift
//        aBin = aBin * aBin %mod; // square
//    }
    for( ; b; b >>= 1){
        if(b & 1) ans = ans * aBin % mod; // only calculate when the value is 1
        aBin = aBin * aBin %mod; // square
    }
    return ans;
}

int main(){
    scanf("%d", &T);
    while(T--){
        scanf("%d", &n);
        printf("%d\n", quickPower(2, n-1, mod)-1);
    }

    return 0;
}

标程

#include <bits/stdc++.h>
typedef long long LL;
using namespace std;

int main() {
  int T; scanf("%d", &T);
  for (int _ = 0; _ < T; ++ _) {
    int n; scanf("%d", &n); -- n;
    LL r(1), a(2), mod = 1e9 + 7;
    for (; n; n >>= 1) {
      if (n & 1) r = r * a % mod;
      a = a * a % mod;
    }
    r = (r + mod - 1) % mod;
    printf("%lld\n", r);
  }
  return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值