HDU 4602 Partition (矩阵乘法)
Partition
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 797 Accepted Submission(s): 322
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.
Each test case contains two integers n and k(1≤n,k≤109).
4 2
5 5
1
思路:
列出了 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 (矩阵乘法)的更多相关文章
- hdu 4602 Partition 矩阵快速幂
Partition Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Proble ...
- hdu 4602 Partition(矩阵快速幂乘法)
Problem Description Define f(n) , we have =+++ =++ =++ =++ =+ =+ =+ = totally ways. Actually, we wil ...
- 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 ...
- hdu 4602 Partition 数学(组合-隔板法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4602 我们可以特判出n<= k的情况. 对于1<= k<n,我们可以等效为n个点排成 ...
- 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 (概率方法)
Partition Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- hdu 4602 递推关系矩阵快速幂模
Partition Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- HDU 5895 Mathematician QSC(矩阵乘法+循环节降幂+除法取模小技巧+快速幂)
传送门:HDU 5895 Mathematician QSC 这是一篇很好的题解,我想讲的他基本都讲了http://blog.csdn.net/queuelovestack/article/detai ...
- HDU 5607 graph(DP+矩阵乘法)
[题目链接] http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=663&pid=1002 [题意] 给定一个有向 ...
随机推荐
- 数据科学家Docker历险记(1):windows下环境搭建
原文:http://www.xueqing.tv/cms/article/247 Docker是最近炒得很火热的一门技术,在网上可以找到关于它的介绍文章,比如<Docker到底是什么?为什么它这 ...
- Sublime Es6教程2-基本语法
2.基本语法 let, const, forEach,for of class, extends, super arrow functions, template string, destructur ...
- 一个小栗子聊聊JAVA泛型基础
背景 周五本该是愉快的,可是今天花了一个早上查问题,为什么要花一个早上?我把原因总结为两点: 日志信息严重丢失,茫茫代码毫无头绪. 对泛型的认识不够,导致代码出现了BUG. 第一个原因可以通过以后编码 ...
- destoon源码解读
一.module module值:表示模块的id ID1.核心: ID2.会员: ID3.扩展: 当ID>3时,为购买.公司等模块. dt:为各种变量,相当于整站的配置,如:关键词.描述.积分等 ...
- SDE注册版本失败,仅支持一个空间列
如果直接编辑SDE要素类与要素可以不需要版本,使用默认版本,如果要让用户通过界面编辑,即使用开启编辑.保存编辑和停止编辑,就需要注册为版本,而在注册版本弹出如下错误: 正如错误所说,一个要素类或shp ...
- iOS 8 swift 键盘不出来 ios 8 uitextfield keyboard not appearing
ios 8 uitextfield keyboard not appearing //发现在iphone 6 and iPhone plus 上面键盘不出来.后来查了下原来要在模拟器里设置下 var ...
- bash if 表达式含义
[ -a FILE ] 如果 FILE 存在则为真. [ -b FILE ] 如果 FILE 存在且是一个块特殊文件则为真. [ -c FILE ] 如果 FILE 存在且是一个字特殊文件则为真. [ ...
- [置顶] 在Visual Studio 2008上调试C语言程序
C语言的地位和重要性就不用说了,但,很多人学习C语言,还在使用Visual C++ 6.0,甚至还有人使用Turbo C,很无语,只说一句吧:“OUT了". 让我们体验一下华丽的Visual ...
- string format 格式化小数位
String具体的格式化数据的方法 int a = 12345678;格式为sring输出Label1.Text = string.Format("asdfadsf{0}adsfasdf&q ...
- MySQL登陆小问题
root用户创建用户 CREATE USER gechong IDENTIFIED BY 'gechong'; 登录数据库时提示错误: C:\Users\gechong>mysql -u gec ...