zoukankan      html  css  js  c++  java
  • 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
    ", 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
    ", r);
      }
      return 0;
    }

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

  • 相关阅读:
    CF1452F Divide Powers 题解
    ZOJ3705Applications
    ZOJ3706Break Standard Weight
    关于技术的感悟与规划
    做自己不愿做的事叫成长,做以前不敢做的事叫突破
    CSS之盒模型
    js数组的sort排序详解
    C#接口
    jQuery UI Dialog:Demo1:入门
    CSS之position解释
  • 原文地址:https://www.cnblogs.com/miaowTracy/p/4836766.html
Copyright © 2011-2022 走看看