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. SGU 224.Little Queens

    时间限制:0.75s 空间限制:6M 题意 n*n(n<=10)的棋盘,求出放置m(m<=n*n)个皇后的方案数. Solution: 状态压缩+位运算  搜索. 首先我们从上往下逐行放置 ...

  2. php 备份数据库脚本

    <?php// 备份数据库$host = "localhost";$user = "root"; //数据库账号$password = "123 ...

  3. git config找到下载的路径URL

      有时想在别的机器上下载自己的上传的源代码,发现git不像svn那么方便一眼就能看到下载地址,这两天在网上查了一下,终于找到方法了. 一般本地的github目录下总会有一个.git目录,点击进入,可 ...

  4. 行列的几种命名方式.line-.colume======.row-.col=========.tr-.td

    第一种从line-height的语义来来讲,line表示hang再贴切不过了,colume纵列或者柱子也很形象,缺点太长了 第二种组合模拟rowspan,colspan而来,想必这个col也是colu ...

  5. Python 的“+”和append在添加字符串时候的区别

    对于一个空的Python列表,往后添加内容有很多种,其中两种一个是用“+”直接添加内容,另外一种是Listname.append(x)来添加内容 其中,如果处理字符串 在使用“+”的时候,会将字符串拆 ...

  6. Day11 线程、进程、协程

    创建线程第一种:import threadingdef f1(arg): print(arg) t = threading.Thread(target=f1, args=(123,))#t.start ...

  7. 【Linux】基础配置-修改命令提示符的风格

    1,效果图: [groot]$ 2,设置步骤: 编辑~/.bashrc文件,在最后增加设置行: #显示当面目录的最后一层目录#PS1='\[\e[32m\][\u@\h \W]$\[\e[m\]'#只 ...

  8. 一种计算e的方法

    原文地址:http://hankjin.blog.163.com/blog/static/3373193720108811316123/ 原理:平均e个(0,1)之间的随机数之和会大于1.原因:n个数 ...

  9. Node.js规范化应用

    Node.js运行在一个单线程模式,但它使用一个事件驱动范例来处理并发.它还有助于创建子进程,以充分利用并行处理的多核CPU系统. 子进程总是有三个流child.stdin,child.stdout和 ...

  10. [转]加盐hash保存密码的正确方式

    0x00 背景 大多数的web开发者都会遇到设计用户账号系统的需求.账号系统最重要的一个方面就是如何保护用户的密码.一些大公司的用户数据库泄露事件也时有发生,所以我们必须采取一些措施来保护用户的密码, ...