• 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 (1T105), indicating
    the number of test cases. For each test case:
     
    The first line contains an integer n (1n109), 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到n的集合S,问集合S的非空子集中元素和为偶数的非空子集有多少个。
      
    题解:我们知道偶数+偶数=偶数,奇数+奇数=偶数,假设现在有a个偶数,b个奇数。则
     
     
    根据二项式展开公式可得2n-1最后的结果还需减去

    即空集的情况,因为题目要求非空子集,所以最终结果为2n-1-1。

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <iostream>
     4 #include <algorithm>
     5 using namespace std;
     6 const int mod = 1e9 + 7;
     7 long long power(long long n, long long m, long long mod)
     8 {
     9     long long sum = 1;
    10     n %= mod;
    11     while (m){
    12        if (m % 2)
    13         sum = sum * n % mod;
    14        n = n * n % mod;
    15        m /= 2;
    16     }
    17     return sum;
    18 }
    19 int main()
    20 {
    21     int t;
    22     scanf("%d",&t);
    23     while (t--){
    24         long long n;
    25         scanf("%I64d", &n);
    26         long long sum = power(2, n-1, mod) - 1;
    27         printf("%I64d
    ",sum);
    28     }
    29     return 0;
    30 }
    View Code
  • 相关阅读:
    3. 无重复字符的最长子串
    24. 两两交换链表中的节点
    2. 两数相加
    23. 合并K个排序链表
    synergy配置 Ubuntu作Server, Win 7作client
    ros与下位机通信常用的c++ boost串口应用
    tar
    发布里程计传感器信息
    ROS TF——learning tf
    在linux终端下打开pdf文件
  • 原文地址:https://www.cnblogs.com/hgfblog/p/4710928.html
Copyright © 2020-2023  润新知