hdu 4602 Partition(矩阵快速幂乘法)
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.
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 the required answer modulo + for each test case, one per line.
题目大意:将一个数 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(矩阵快速幂乘法)的更多相关文章
- hdu 4602 Partition 矩阵快速幂
Partition Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Proble ...
- 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 ...
- hdu 4602 递推关系矩阵快速幂模
Partition Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- HDU.2640 Queuing (矩阵快速幂)
HDU.2640 Queuing (矩阵快速幂) 题意分析 不妨令f为1,m为0,那么题目的意思为,求长度为n的01序列,求其中不含111或者101这样串的个数对M取模的值. 用F(n)表示串长为n的 ...
- HDU 5667 构造矩阵快速幂
HDU 5667 构造矩阵快速幂 题目描述 解析 我们根据递推公式 设 则可得到Q的指数关系式 求Q构造矩阵 同时有公式 其中φ为欧拉函数,且当p为质数时有 代码 #include <cstdi ...
- hdu 2604 Queuing(矩阵快速幂乘法)
Problem Description Queues and Priority Queues are data structures which are known to most computer ...
- HDU 6185 Covering 矩阵快速幂
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6185 题意:用 1 * 2 的小长方形完全覆盖 4 * n的矩形有多少方案. 解法:小范围是一个经典题 ...
- HDU 2157(矩阵快速幂)题解
How many ways?? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 6395 分段矩阵快速幂 HDU 6386 建虚点+dij
http://acm.hdu.edu.cn/showproblem.php?pid=6395 Sequence Time Limit: 4000/2000 MS (Java/Others) Me ...
- HDU 6470 【矩阵快速幂】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6470 写这道题是为了让自己不要忘记矩阵快速幂如何推出矩阵式子的. 注意 代码是TLE的!! #incl ...
随机推荐
- racle undo 解析
racle undo 解析 声明一下:关于oracle的文章基于boobooke小布老师视频,在我学习的过程中,每有体会拿来分享,虽然从理解到整理分享很耗时,但我想这样的学习是扎实的. Undo是干嘛 ...
- Jquery一个slideToggle搞定div的隐藏与显示
Jquery一个slideToggle搞定div的隐藏与显示 <!DOCTYPE html> <html> <head> <script src=" ...
- 一种全新的MEMS开关——高性能、快速、低能耗以及双稳态
这种开关最早由申军教授和研究生阮梅春发明,研究生埃里克·朗格卢瓦在简化结构和缩小尺寸上作了探索,黄志林用相同原理做出了MEMS光学镜子开关,曹志良改变设计.材料和工艺后制作出了能同步开关的矩阵.这种M ...
- Python OpenGL学习(1): 环境配置及错误篇
系统环境是:Ubuntu 14.04 个人首次接触OpenGL,学到哪就写到哪. 1.模块安装: sudo apt-get install python-openglpip install PyOpe ...
- Buns(dp+多重背包)
C. Buns time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...
- linux命令学习笔记
操作文件和文件夹: copy: $ cp file1 file2 $ cp -r dir1 dir2 move: $ mv file .. $ mv file dir/ rename: $ mv fi ...
- ORACLE AUTOMATIC STORAGE MANAGEMENT翻译-第二章 ASM instance(1)
第二章 ASM INSTANCE ASM的类型,例如可以: 10g后ORACLE instance 类型增加了一个ASM种类.参数INSTANCE_TYPE=ASM进行设置. ASM实例启动命令: ...
- 基于HTML5多图片Ajax上传可预览
html5多图控件<input id="fileImage" type="file" size="30" name="fil ...
- 【递推+矩阵快速幂】【HDU2604】【Queuing】
Queuing Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- WebConfig 配置文件详解
<?xml version="1.0"?><!--注意: 除了手动编辑此文件以外,您还可以使用 Web 管理工具来配置应用程序的设置.可以使用 Visual St ...