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 ...
随机推荐
- Jquery基础之ajax
ajax是Asynchronous JavaScript and XML(异步JavaScript和XML)的简称,ajax并不是单一的技术而是利用一系列交互网页应用相关的技术形成的结合体,ajax揭 ...
- jquery 事件委托三种事件绑定方式.bind(),.live(),.delegate()
http://www.ituring.com.cn/article/467# http://www.cnblogs.com/lvdabao/archive/2013/08/30/3290603.htm ...
- MFC子窗口和父窗口(SetParent,SetOwner)
一.概念和区别 在windows系统中,每个窗口对象都对应有一个数据结构,形成一个list链表.系统的窗口管理器通过这个list来获取窗口信息和管理每个窗口.这个数据结构中有四个数据用来构建list, ...
- getgrent
http://baike.baidu.com/link?url=JNyoNvukL-LP7ayYlNNWLv2gPOzn-bjiwuX1CE_QwUTyrRGCWu4NhDW-JznHQoG4aIfw ...
- Error: Linux下 mysql.sock文件丢失被删除解决方法
在默认情况下,Mysql安装以后会在/tmp目录下生成一个mysql.sock文件,如该文件丢失则Mysql将不能够正常启动,解决方法:使用mysqld_safe 启动即可解决: #basedir:m ...
- js中使用队列发送ajax请求
最近,项目中需要按照先后顺序发送ajax请求,并且在一次请求结束后才能发起下一次,不然就会导致逻辑错误. 解决办法是定义一个数组,保存ajax请求数据. 以下使用extjs4定义一个类 Ext.def ...
- Codeforces 148D Bag of mice 概率dp(水
题目链接:http://codeforces.com/problemset/problem/148/D 题意: 原来袋子里有w仅仅白鼠和b仅仅黑鼠 龙和王妃轮流从袋子里抓老鼠. 谁先抓到白色老师谁就赢 ...
- OpenGL研究2.0 计算圆
OpenGL研究2.0 计算圆 DionysosLai2014-06-18 在游戏中.常常有些地方涉及到一些圆的轨迹计算,例如一些转轴类的游戏,人物一般在角色转轴上面运动.这时,我们就要时刻计算角色的 ...
- 国内外DNS服务器地址列表
DNS(Domain Name System)是域名解析服务器的意思,它在互联网的作用是把域名转换成为网络可以识别的IP地址.目前国内电信运营商通过使用DNS劫持的方法,干扰用户正常上网,使得用户无法 ...
- UVA1600 Patrol Robot
题意: 求机器人走最短路线,而且可以穿越障碍.N代表有N行,M代表最多能一次跨过多少个障碍. 分析: bfs()搜索,把访问状态数组改成了3维的,加了个维是当前能跨过的障碍数. 代码: #includ ...