HDU 4602 partition (Probability Method)

Source: Internet
Author: User

Partition Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 2472 accepted submission (s): 978


Problem descriptiondefine F (n) as the number of ways to perform n in format of the sum of some positive integers. For instance, when n = 4, we have
4 = 1 + 1 + 1 + 1
4 = 1 + 1 + 2
4 = 1 + 2 + 1
4 = 2 + 1 + 1
4 = 1 + 3
4 = 2 + 2
4 = 3 + 1
4 = 4
Totally 8 ways. Actually, we will have f (n) = 2 (n-1) after observations.
Given a pair of integers N and K, your task is to figure out how many times that the integer k occurs in such 2 (n-1) ways. in the example above, number 1 occurs for 12 times, while number 4 only occurs once.
 
Inputthe first line contains a single integer T (1 ≤ T ≤ 10000), indicating the number of test cases.
Each test case contains two integers N and K (1 ≤ n, k ≤109 ).
 
Outputoutput the required answer modulo 109 + 7 for each test case, one per line.
Sample Input
24 25 5
 
Sample output
51


For 1 <= k <n, we can arrange the N points into a column and take out K consecutive points. In this case, the K ends are disconnected consecutively;

1. If the K points contain the endpoint (we only consider an endpoint), there are still (n-k-1) intervals,

Each interval has two states: disconnected and closed, so there are 2 ^ (n-k-1), and multiplied by 2;

2, if the K points do not contain the endpoint, the continuous K points have (n-k-1) method, there are (n-k-2) interval,

So there are 2 ^ (n-k-2) * (n-k-1 );

Total 2? 2 ^ (n-k? 1) + 2 ^ (n-k? 2 )? (N-k? 1) = (n-k + 3) * 2 ^ (n-k? 2 ).


#include"stdio.h"#include"string.h"#include"queue"#include"vector"#include"algorithm"using namespace std;#define LL __int64const int mod=1000000007;int main(){    int T,i,n,k;    scanf("%d",&T);    while(T--)    {        scanf("%d%d",&n,&k);        if(n<k)            printf("0\n");        else if(n==k)            printf("1\n");        else        {            LL d=n-k,s1,t;            if(d==1) printf("2\n");            else            {                s1=d+3;                d-=2;                LL aa=2,tmp=1;                while(d)                {                    if(d&1)                        tmp*=aa;                    d/=2;                    aa=(aa*aa)%mod;                    tmp%=mod;                }                printf("%I64d\n",(tmp*s1)%mod);            }        }    }    return 0;}


HDU 4602 partition (Probability Method)

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.