Partition

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

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
 
Source
 
Recommend
liuyiding
 

思路:

列出了 n=5 时 5,4,3,2,1 出现的次数为 1 2 5 12 28
f[n+1]=3*f[n]-f[n-1]-f[n-2]-..f[1]
f[n]=3*f[n-1]-f[n-2]-..f[1]
==> f[n+1]=4*f[n]-4*f[n-1]
#include<iostream>
#include<cstdio>
#include<cstring> using namespace std; const int mod=; struct Matrix{
long long arr[][];
}; Matrix init,unit;
int n,k;
long long num[][]={{,-},{,}}; void Init(){
for(int i=;i<;i++)
for(int j=;j<;j++){
init.arr[i][j]=num[i][j];
unit.arr[i][j]=(i==j)?:;
}
} Matrix Mul(Matrix a,Matrix b){
Matrix c;
for(int i=;i<;i++)
for(int j=;j<;j++){
c.arr[i][j]=;
for(int k=;k<;k++)
c.arr[i][j]=(c.arr[i][j]%mod+a.arr[i][k]*b.arr[k][j]%mod+mod)%mod;
c.arr[i][j]%=mod;
}
return c;
} Matrix Pow(Matrix a,Matrix b,int k){
while(k){
if(k&){
b=Mul(a,b);
}
a=Mul(a,a);
k>>=;
}
return b;
} int main(){ //freopen("input.txt","r",stdin); int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&k);
Init();
if(k>n){
printf("0\n");
continue;
}
int tmp=n-k+;
if(tmp==){
printf("1\n");
continue;
}
if(tmp==){
printf("2\n");
continue;
}
if(tmp==){
printf("5\n");
continue;
}
Matrix res=Pow(init,unit,tmp-);
//long long ans=((res.arr[0][0]%mod*5)%mod+(res.arr[0][1]%mod*2)%mod)%mod;
long long ans=(res.arr[][]*+res.arr[][]*)%mod;
cout<<ans<<endl;
}
return ;
}
												

HDU 4602 Partition (矩阵乘法)的更多相关文章

  1. hdu 4602 Partition 矩阵快速幂

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

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

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

  3. 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 ...

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

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

  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 (概率方法)

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

  7. hdu 4602 递推关系矩阵快速幂模

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

  8. HDU 5895 Mathematician QSC(矩阵乘法+循环节降幂+除法取模小技巧+快速幂)

    传送门:HDU 5895 Mathematician QSC 这是一篇很好的题解,我想讲的他基本都讲了http://blog.csdn.net/queuelovestack/article/detai ...

  9. HDU 5607 graph(DP+矩阵乘法)

    [题目链接] http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=663&pid=1002 [题意] 给定一个有向 ...

随机推荐

  1. 使用 Edit + MASM 5.0 编译器 + Linker 连接器

    其实这种方式是很简单的,只是很麻烦,因为简单而且麻烦, 所以我采用尽可能的将截图传上来,然后稍加注解的方式进行介绍, 软件准备: 需要 MASM 5.0 或者以上的汇编编译器 首先,是要编辑汇编源代码 ...

  2. Pandas DataFrame学习笔记

    对一个DF r1  r2  r3 c1 c2 c3 选行:  df['r1']  df['r2':'r2']  #包含r2  df[df['c1']>5] #按条件选 选列:  df['c1'] ...

  3. mongodb最大连接数、最大连接数修改

        mongodb最大连接数是20000. 所以业界流传一段话,千万级以下的用mysql.千万级以上的用mongodb,亿级以上的用hadoop.   查看mongodb最大连接数 mongodb ...

  4. JS调试必备的5个debug技巧_javascript技巧

    JS调试必备的debug调试javascript技巧 1. debugger; 我以前也说过,你可以在JavaScript代码中加入一句debugger;来手工造成一个断点效果.需要带有条件的断点吗? ...

  5. ECShop模板原理

    模板的原理 类似Smarty/ECShop这类模板的原理如下图所示. 1.首先是编译模板ECShop/Smart是利用PHP引擎,所以编译的结果是一个PHP文件,其编译过程就是 将分隔符{}替换成PH ...

  6. Oracle数据库imp

    创建表空间 create tablespace ICITY datafile 'D:\Oracle\oradata\orcl\ICITY.DBF' size 400M autoextend on ne ...

  7. Jenkins Xcode 证书设置错误 Code Sign error: No matching codesigning identity found: No codesigning identities

    Jenkins 集成Xcode 项目的时候在证书上遇到了问题.实际上如果在本地的话.只要Xcode工程里选择了项目就不需要重新设置证书了.jenkins会自动找到这个证书,只要在build setti ...

  8. ASP.NET绑定学习

    1.直接绑定到页面成员<asp:Repeater ... DataSource='<%#页面方法或属性%>'></asp:Repeater> 2.绑定到数组< ...

  9. asp.net 定时执行任务代码 定时采集数据

    using System; using System.Data; using System.Configuration; using System.Collections; using System. ...

  10. 微信小程序 - 分包加载(独立分包)

    独立分包是小程序中一种特殊类型的分包,可以独立于主包和其他分包运行.从独立分包中页面进入小程序时,不需要下载主包.当用户进入普通分包或主包内页面时,主包才会被下载 将某些具有一定功能独立性的页面配置到 ...