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 ;
}
												

最新文章

  1. angularJS绑定数据时自动转义html标签
  2. android5.0 aosp编译记录(由于机器硬件原因,改为4.4.2编译通过)
  3. 洛谷P1755 攻击火星
  4. Design Mode 之 创建模式
  5. 【题解】警位安排( 树形 DP)
  6. (4)事件处理——(3)代码执行的顺序(Timing of code execution)
  7. Thinkpad X200 屏幕备案
  8. 【python问题系列--4】ValueError: operands could not be broadcast together with shapes (100,3) (3,1)
  9. 4、安卓数据存储——sqlite
  10. 转深入Java虚拟机 之四:类加载机制
  11. ThinkPHP5上传图片并压缩为缩略图
  12. MySQL之SELECT用法
  13. Linux(Ubuntu 16) 下Java开发环境的配置(三)------Mysql配置
  14. 二叉排序树插入C语言版 递归步骤理解
  15. C#数组,ArrayList,List
  16. Linux运维之系统性能瓶颈工具vmstat分析
  17. URAL 1962 In Chinese Restaurant 数学
  18. Dapper - .Net 环境下一个简单对象映射的框架
  19. TP5 首页导航一级和二级分类
  20. Python之面向对象的组合、多态、菱形问题、子类中重用父类的两种方式

热门文章

  1. [转]shell脚本每行的执行顺序是怎样
  2. Java-JUC(二):Java内存模型可见性、原子性、有序性及volatile具有特性
  3. 转:在centos7上安装memcache
  4. Docker worker nodes shown as “Down” after re-start
  5. You have version null and I want version 8
  6. 微软BI 之SSRS 系列 - 报表中分组聚合中处理不规则层次结构的技巧(没有子元素的时候不展开, 删除+符号)
  7. 关于LINUX在中断(硬软)中不能睡眠的真正原因
  8. kettle 如何将excel文件导入oracle数据库?
  9. LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)
  10. java for语句