hdu 4602 Partition

Partition

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2156    Accepted Submission(s): 866


Problem Description
Define 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.
 

Input
The 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≤10 9).
 

Output
Output the required answer modulo 10 9+7 for each test case, one per line.
 

Sample Input
   
   
   
   
2 4 2 5 5
 

Sample Output
   
   
   
   
5 1
 

Source
2013 Multi-University Training Contest 1


思路:
1.k>n、k=n、k=n-1特判.
2.k<n-1时,将n写成n个1的形式,有   1  1  1  1  1  1   1  1  1  1  1  1,
取其中连续的k个合并 1  1  1  (1  1  1)   1  1  1  1  1  1 ,有 1  1  1  k  1  1  1  1  1  1 ,
设左边有a(a>0&a<n-k)个1,合成方案有2^(a-1)种,则右边有n-a-k个1,合成方案有2^(n-a-k-1)种,两式相乘,有
2^(n-k-2)种,与a无关,所以考虑整体,有n-k-1次这样的情况,再加两个边界情况2*2^(n-k-1),就是答案了。

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 105
#define MAXN 100005
#define mod 1000000007
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
typedef long long ll;
using namespace std;

ll n,m,k,ans,cnt,flag;

ll pow_mod(ll a,ll i,ll nn)
{
    if(i==0) return 1%nn;
    ll temp=pow_mod(a,i>>1,nn);
    temp=temp*temp%nn;
    if(i&1) temp=temp*a%nn;
    return temp ;
}
int main()
{
    ll i,j,t;
    scanf("%I64d",&t);
    while(t--)
    {
        scanf("%I64d%I64d",&n,&k);
        if(k>n) printf("0\n");
        else if(k==n) printf("1\n");
        else if(k==n-1) printf("2\n");
        else
        {
            ans=pow_mod(2,n-k-2,mod);
            ans=((n-k+3)*ans)%mod;
            printf("%I64d\n",ans);
        }
    }
    return 0;
}




 

你可能感兴趣的:(hdu 4602 Partition)