HDU 5363 element is 1 ~ Number of subset elements in a set of n and the sum of them are even-thinking-(Fast Power modulo), hdu5363

Source: Internet
Author: User

HDU 5363 element is 1 ~ Number of subset elements in a set of n and the sum of them are even-thinking-(Fast Power modulo), hdu5363

Question: A set has element 1 ~ N, ask his subset to satisfy the condition that the sum of all elements in the subset is an even number and how many such subsets are there?

Analysis:

An arrangement and combination problem. If the element is an even number, the odd number must be an even number, and the even number does not matter. Therefore, the even number has a 2 ^ (n/2) method, multiply by the odd number (C (n + 1)/) + C (n + 1 ).....) method of selection, minus one, except for the empty set, note that when the odd number is obtained above, it is (n + 1)/2 (here is the downward Division ), is the combination of n as an even number and n as an odd number.

Number of combinations: C (n, 1) + C (n, 3) + .... = C (n, 2) + C (n, 4) + .... = 1/2 * (C (n, 1) + C (n, 2) + ..... C (n. n) = 1/2*(2 ^ n) = 2 ^ (n-1) (n is an odd and even number)

Merge the formula (division here is the real division, so when n is an odd number, + 1 or-1 is used for Division ):

1. when n is an even number, there are 2 ^ (n/2) methods for obtaining an even number, and 2 ^ (n/N-1)/2 methods for obtaining an odd number) -1

2. when n is an odd number, the even number can be 2 ^ (n-1)/2), and the odd number can be 2 ^ (n + 1)/2, ans = 2 ^ (n-1)-1

The rest is the rapid power modulo. Remember this method.

Code:

#include<cstdio>#include<iostream>#define INF 1000000007using namespace std;int t;long long n;long long ans;long long f(long long a,long long b,long long c){    long long t = 1;    while(b){        if(b&1) t=(t*a)%INF;        b>>=1;        a=(a*a)%INF;    }    return t; }int main(){    scanf("%d",&t);    while(t--){        scanf("%I64d",&n);            ans=f(2,n-1,INF);            ans-=1;            printf("%I64d\n",ans);    }}


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.