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=, we have
=+++
=++
=++
=++
=+
=+
=+
=
totally ways. Actually, we will have f(n)=(n-) 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 (n-) ways. In the example above, number occurs for times, while number only occurs once.
Input
The first line contains a single integer T(≤T≤), indicating the number of test cases.
Each test case contains two integers n and k(≤n,k≤).
 
Output
Output the required answer modulo + for each test case, one per line.
Sample Input

Sample Output

 
Source

题目大意:将一个数 n 拆分,问所有的拆分组合中 K 出现了几次。

思路:

列出了 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]

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 1000000
#define inf 1e12
ll n,k;
struct Matrix{
ll mp[][];
};
Matrix Mul(Matrix a,Matrix b){
Matrix res;
for(ll i=;i<;i++){
for(ll j=;j<;j++){
res.mp[i][j]=;
for(ll k=;k<;k++){
res.mp[i][j]=(res.mp[i][j]+(a.mp[i][k]*b.mp[k][j])%MOD+MOD)%MOD;
}
}
}
return res;
}
Matrix fastm(Matrix a,ll b){
Matrix res;
memset(res.mp,,sizeof(res.mp));
for(ll i=;i<;i++){
res.mp[i][i]=;
}
while(b){
if(b&){
res=Mul(res,a);
}
a=Mul(a,a);
b>>=;
}
return res;
}
int main()
{
ll t;
scanf("%I64d",&t);
while(t--){
scanf("%I64d%I64d",&n,&k);
if(k>n){
printf("0\n");
continue;
}
ll tmp=n-k+;
if(tmp==){
printf("1\n");
continue;
}
if(tmp==){
printf("2\n");
continue;
}
if(tmp==){
printf("5\n");
continue;
} Matrix ttt;
ttt.mp[][]=;
ttt.mp[][]=-;
ttt.mp[][]=;
ttt.mp[][]=; ttt=fastm(ttt,tmp-); Matrix cnt;
cnt.mp[][]=;
cnt.mp[][]=;
ttt=Mul(ttt,cnt);
printf("%I64d\n",ttt.mp[][]); }
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(快速幂)

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

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

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

  4. HDU.2640 Queuing (矩阵快速幂)

    HDU.2640 Queuing (矩阵快速幂) 题意分析 不妨令f为1,m为0,那么题目的意思为,求长度为n的01序列,求其中不含111或者101这样串的个数对M取模的值. 用F(n)表示串长为n的 ...

  5. HDU 5667 构造矩阵快速幂

    HDU 5667 构造矩阵快速幂 题目描述 解析 我们根据递推公式 设 则可得到Q的指数关系式 求Q构造矩阵 同时有公式 其中φ为欧拉函数,且当p为质数时有 代码 #include <cstdi ...

  6. hdu 2604 Queuing(矩阵快速幂乘法)

    Problem Description Queues and Priority Queues are data structures which are known to most computer ...

  7. HDU 6185 Covering 矩阵快速幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6185 题意:用 1 * 2 的小长方形完全覆盖 4 * n的矩形有多少方案. 解法:小范围是一个经典题 ...

  8. HDU 2157(矩阵快速幂)题解

    How many ways?? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. HDU 6395 分段矩阵快速幂 HDU 6386 建虚点+dij

    http://acm.hdu.edu.cn/showproblem.php?pid=6395 Sequence Time Limit: 4000/2000 MS (Java/Others)    Me ...

  10. HDU 6470 【矩阵快速幂】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6470 写这道题是为了让自己不要忘记矩阵快速幂如何推出矩阵式子的. 注意 代码是TLE的!! #incl ...

随机推荐

  1. WINDOWS API 函数(超长,值得学习)

    一.隐藏和显示光标 函数: int ShowCursor ( BOOL bShow );  参数 bshow,为布尔型,bShow的值为False时隐藏光标,为True时显示光标:该函数的返回值为整型 ...

  2. 可以供MFC调用的,QT实现的DLL(qtwinmigrate实现)

    MFC和QT的消息循环机制不同,所以,要让QT写的DLL可以供MFC调用,要做一点特殊的处理 #include <qmfcapp.h> #include <qwinwidget.h& ...

  3. Linux进程间通信——使用信号

    一.什么是信号 用过Windows的我们都知道,当我们无法正常结束一个程序时,可以用任务管理器强制结束这个进程,但这其实是怎么实现的呢?同样的功能在Linux上是通过生成信号和捕获信号来实现的,运行中 ...

  4. 关于 .crash 分析

    这里只给出其中 一种方式. 1. 建议 桌面 建 个文件夹  appxx  ,然后 将那个闪退 对应的 包  xxx.app 放入  appxx文件夹 2. 打开终端cd命令,进入该文件夹 3.在命令 ...

  5. scanf与printf用法详解

    一.scanf家族 1.scanf家族的原型 int scanf(char const *format,...); int fscanf(FILE *stream,char const *format ...

  6. 创建和使用RMAN存储脚本

    创建和使用RMAN存储脚本:1.连接恢复目录(可以不连接到目标库):C:\Users\Administrator>rman target sys/rusky@rusky catalog=rcat ...

  7. SOA架构有基本的要求

    SOA在相对较粗的粒度上对应用服务或业务模块进行封装与重用: 服务间保持松散耦合,基于开放的标准, 服务的接口描述与具体实现无关: 灵活的架构 -服务的实现细节,服务的位置乃至服务请求的底层协议都应该 ...

  8. 搭建MyBatis框架

    一.开发环境 1.JDK 1.6.0_22 2.MyEclipse 10.7.1 3.Oracle_10g_10.2.0.4 注:各软件版本不是必须的,正常任意版本都行,文件较大就不附上下载地址了,推 ...

  9. VMware下桥接设置

    操作环境 主机:Win7 X86 SP1 虚拟机:VMware station 8 虚拟机里的系统:Fedora 15 环境上,不管什么系统,什么版本的虚拟机,使用上都是大同小异的,毕竟核心是不变的. ...

  10. maven将jar包安装到本地仓库的命令

    进入cmd 执行以下命令: mvn install:install-file -Dfile=E:\sqljdbc4.jar -DgroupId=com.microsoft.sqlserver -Dar ...