HDU5187 zhx & amp; #39; s contest (count problem)

Source: Internet
Author: User

HDU5187 zhx & #39; s contest (count problem)

 

Question:

From 1 ~ N, how many sort

Make a1 ~ Ai can be monotonic or monotonic.

Ai ~ An is monotonically increasing or decreasing.

Obvious combination Problems

Select the number of I from n. The remaining number must meet the monotonic increasing or decreasing law, so the method is unique.

Ans = (C (N, 0) + C (N, 1) +... + C (N, N) = 2 ^ N;

But in this case, the monotonic increasing and monotonic decreasing are calculated twice, so we need to subtract 2.

Ans = 2 ^ n-2;

Note that n = 1, because n is relatively large, pay attention to multiplication Overflow

 

The Code is as follows:

 

#include 
 
  #include 
  
   #include 
   
    #include using namespace std;typedef long long LL;LL multi(LL a,LL b, LL c){    LL ans = 0;    while(b){        if(b&1){            ans= (ans+a)%c;            b--;        }        b>>=1;        a=(a+a)%c;    }    return ans;}LL quick_mod(LL a,LL b,LL c){    LL ans = 1;    while(b){        if(b&1){            ans = multi(ans,a,c);            b--;        }        b>>=1;        a=multi(a,a,c);    }    return ans ;}int main(){    LL n,p;    while(~scanf(%lld%lld,&n,&p)){        if(n==1){            printf(%d,1%p);            continue;        }        LL ans = 2;        ans = quick_mod(ans,n,p);        ans =(ans - 2 + p)%p;        printf(%I64d,ans);    }    return 0;}
   
  
 

 

 

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.