Partition

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2472    Accepted Submission(s): 978

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≤109).
 
Output
Output the required answer modulo 109+7 for each test case, one per line.
 
Sample Input
2
4 2
5 5
 
Sample Output
5
1

对于1 <= k < n,我们能够等效为n个点排成一列,并取出当中的连续k个,这连续的看K个两端断开;

1、若取得是这K个点包含端点(我们仅仅考虑一个端点的情况),还剩下(n-k-1)个间隔,

每一个间隔有断开和闭合两种状态,故有2^(n-k-1),最后乘以2;

2、若取得是这K个点不包含端点,这连续的K个点有(n-k-1)种取法,还剩下(n-k-2)个间隔,

故有2^(n-k-2)*(n-k-1);

总计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 __int64
const 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 (概率方法)的更多相关文章

  1. hdu 4602 Partition 数学(组合-隔板法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4602 我们可以特判出n<= k的情况. 对于1<= k<n,我们可以等效为n个点排成 ...

  2. hdu 4602 Partition

    http://acm.hdu.edu.cn/showproblem.php?pid=4602 输入 n 和 k 首先 f(n)中k的个数 等于 f(n-1) 中 k-1的个数 最终等于 f(n-k+1 ...

  3. HDU 4602 Partition (矩阵乘法)

    Partition Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  4. hdu 4602 Partition 矩阵快速幂

    Partition Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Proble ...

  5. hdu 4602 Partition(快速幂)

    推公式+快速幂 公式有很多形式,可以写矩阵 1.前n-1项和的两倍+2的(n-2)次方,这个写不出啥 2.递推式:f(n)=2*f(n-1)+2的(n-3)次方 3.公式:2的(n-k-2)次方*(n ...

  6. hdu 4602 Partition(矩阵快速幂乘法)

    Problem Description Define f(n) , we have =+++ =++ =++ =++ =+ =+ =+ = totally ways. Actually, we wil ...

  7. 【HDU 4602】Partition

    题意 给你一个数n,把它写成几个正整数相加的形式,即把n拆开成若干段,把所有可能的式子里正整数 k 出现的次数取模是多少. 分析 特判 k>=n 的情况. k<n时:问题相当于n个点排一行 ...

  8. Partition HDU - 4602 (不知道为什么被放在了FFT的题单里)

    题目链接:Vjudge 传送门 相当于把nnn个点分隔为若干块,求所有方案中大小为kkk的块数量 我们把大小为kkk的块,即使在同一种分隔方案中的块 单独考虑,它可能出现的位置是在nnn个点的首.尾. ...

  9. HDU 4050 wolf5x 概率dp 难度:1

    http://acm.hdu.edu.cn/showproblem.php?pid=4050 题意: 现在主角站在0处,需要到达大于n的位置 主角要进入的格子有三种状态: 0. 不能进入 1. 能进入 ...

随机推荐

  1. 【POJ3461】【KMP】Oulipo

    Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without t ...

  2. C#应用程序获取项目路径的方法总结

    一.非Web程序   //基目录,由程序集冲突解决程序用来探测程序集 1.AppDomain.CurrentDomain.BaseDirectory     //当前工作目录的完全限定路径2.Envi ...

  3. jquery 实现的一款超简单的图片切换功能

    <html><head> <meta http-equiv="Content-Type" content="text/html; chars ...

  4. ueditor的过滤、转义、格式丢失问题

    1. 过滤 http://www.cnblogs.com/Olive116/p/3464495.html 2. 转义 http://segmentfault.com/q/101000000048928 ...

  5. css+js自动化开发之第十五天

    一.css上一篇的补充 1.position(页面分层) (1)fiexd将标签固定在页面的某个位置 position属性:top,left,right,bottom (2)relative+abso ...

  6. python的min()函数也可用于比较tuple

      python的min()函数也可用于比较tuple >>> a = (2,'asv','dfg') >>> b = (3,'gsg','weg') >&g ...

  7. MyEclipse右键new菜单项的设置 及 Eclipse中各种文件不能保存中文的问题

    有时候,myeclipse右键new的时候经常出现一些ejb等文件你懂的,很是恶心~~ Window --> Customize Perspective --> Submenus --&g ...

  8. AVR单片机RC触摸

    RC电容触摸感应按键1:RC感应原理 RC采样原理就是通过测量感应极电容的微小变化,来感知人体对电容式感应器(按键.轮键或者滑条)的感应.电极电容(C)通过一个固定的电阻(R)周期性地充放电.(原文件 ...

  9. asp.net 由于代码已经过优化或者本机框架位于调用堆栈之上,无法计算表达式的值

    看MS给的解决方案:(http://support.microsoft.com/kb/312629/ ) 症状:如果使用 Response.End.Response.Redirect 或 Server ...

  10. Matlab计算机视觉/图像处理工具箱推荐

    Matlab计算机视觉/图像处理工具箱推荐 转载http://cvnote.info/matlab-cv-ip-toolbox/ 计算机视觉/图像处理研究中经常要用到Matlab,虽然其自带了图像处理 ...