HDU 5363 elements for a set of 1~n how many subsets of elements and for even-thinking-(Fast power modulo)

Source: Internet
Author: User

Test instructions: A set has element 1~n that satisfies the condition that all elements of a subset are of an even number, asking how many such subsets

Analysis:

A permutation combination problem. Elements and is even, then the odd number must be adjusted even numbers, even if it does not matter, so even there is 2^ (N/2) Seed selection method, and then multiplied by odd (C ((n+1)/2,0) +c ((n+1)/2,2) ... Seed selection method, minus one, remove the empty set, note that the above is used when the odd number (n+1)/2 (here is the division of the downward rounding), is a combination of n is even and N is an odd two cases.

Combination Number properties: 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 even number that satisfies this property)

Now Bashi the sub-merge (where the division is true division, so N is an odd time to be +1 or 1 to divide):

When 1.N is even, even there are 2^ (N/2), Odd 2^ (N/2)/2 kinds of extraction, integrated ans=2^ (n-1)-1

When 2.N is odd, even there are 2^ ((n-1)/2), Odd 2^ ((n+1)/2), Integrated ans=2^ (N-1) 1

The rest is the quick power to take the mold, this method is very basic to remember it.

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 NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 5363 elements for a set of 1~n how many subsets of elements and for even-thinking-(Fast power modulo)

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.