Partition

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2362    Accepted Submission(s): 937

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
 

递推公式:f(n)=4*f(n-1)-4*f(n-2)

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std; typedef __int64 LL;
const int Mod=; struct Matrix
{
LL a[][];
}; Matrix mult_mod(Matrix A,Matrix B)
{
int i,j,k;
Matrix C;
for(i=;i<=;i++)
{
for(j=;j<=;j++)
{
C.a[i][j]=;
for(k=;k<=;k++)
{
C.a[i][j]=(C.a[i][j]+A.a[i][k]*B.a[k][j]%Mod+Mod)%Mod;
}
}
}
return C;
} Matrix Matrix_pow_mod(Matrix A,int n)
{
struct Matrix ret;
ret.a[][]=ret.a[][]=;
ret.a[][]=ret.a[][]=;
while(n)
{
if(n&) ret=mult_mod(ret,A);
A=mult_mod(A,A);
n>>=;
}
return ret;
} LL deal(int n)
{
struct Matrix A;
A.a[][]=;A.a[][]=-;A.a[][]=;A.a[][]=;
A=Matrix_pow_mod(A,n);
return (A.a[][]*%Mod+A.a[][]*%Mod)%Mod;
} int main()
{
int t,n,k;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&k);
if(k > n) {printf("0\n");continue;}
n=n-k+;
if(n==) {printf("1\n");continue;}
if(n==) {printf("2\n");continue;}
if(n==) {printf("5\n");continue;}
printf("%I64d\n",deal(n-));
}
return ;
}

hdu 4602 递推关系矩阵快速幂模的更多相关文章

  1. hdu 4602 Partition 矩阵快速幂

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

  2. uva 10870 递推关系矩阵快速幂模

    Recurrences Input: standard input Output: standard output Consider recurrent functions of the follow ...

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

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

  4. HDU 5667 构造矩阵快速幂

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

  5. HDU 2604 Queuing( 递推关系 + 矩阵快速幂 )

    链接:传送门 题意:一个队列是由字母 f 和 m 组成的,队列长度为 L,那么这个队列的排列数为 2^L 现在定义一个E-queue,即队列排列中是不含有 fmf or fff ,然后问长度为L的E- ...

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

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

  7. HDU 6185 Covering 矩阵快速幂

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

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

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

  9. Codeforces 185A Plant( 递推关系 + 矩阵快速幂 )

    链接:传送门 题意:输出第 n 年向上小三角形的个数 % 10^9 + 7 思路: 设 Fn 为第 n 年向上小三角形的个数,经过分析可以得到 Fn = 3 * Fn-1 + ( 4^(n-1) - ...

随机推荐

  1. StatementHandler-Mybatis源码系列

    内容更新github地址:我飞 StatementHandler接口 StatementHandler封装了Mybatis连接数据库操作最基础的部分.因为,无论怎么封装,最终我们都是要使用JDBC和数 ...

  2. 通过脚本批量添加AD用户

    1.新建一个csv文件(逗号分隔的一种值文件) 内容为:放在C:\盘根目录下 test300 test300 .com test300 test301 test301 .com test301 tes ...

  3. MySQL对数据库数据进行复制的基本过程详解

    MySQL对数据库数据进行复制的基本过程详解 这篇文章主要介绍了MySQL对数据库数据进行复制的基本过程,解读了Slave的一些相关配置,需要的朋友可以参考下 复制 复制是从一个MySQL服务器(ma ...

  4. LLDB详解

    LLDB的Xcode默认的调试器,它与LLVM编译器一起,带给我们更丰富的流程控制和数据检测的调试功能.平时用Xcode运行程序,实际走的都是LLDB.熟练使用LLDB,可以让你debug事半功倍 L ...

  5. NodeJS基础入门-fs文件系统

    文件I/O是由简单封装的标准POSIX函数提供.通过require('fs') 使用该模块.所有的方法都有异步和同步的形式. 异步方法的最后一个参数都是一个回调函数.传给回调函数的参数取决于具体方法, ...

  6. MyBatis的mapper.xml文件的参数问题:org.apache.ibatis.builder.IncompleteElementException: Could not find parameter map

    配置参数类型有两种选择,即:parameterType和parameterMap 不管参数是否是基本数据类型还是map类型,都是使用parameterType. 版权声明:本文为博主原创文章,未经博主 ...

  7. MySQL创建根据经纬度计算距离的函数

    按照经纬度计算距离 日常开发中,特别是做微信项目时,经常会遇到根据用户地理位置来展示附近商家的功能,通常解决这种问题的思路是,后台设置商家的经纬度,然后再根据前台传的经纬度进行计算,具体经纬度转换以及 ...

  8. SQL语句小练习

    一.创建如下表结构(t_book) Id         主键   自增一 bookName   可变长 20 Price   小数 Author    可变长20 bookTypeId    图书类 ...

  9. POJ - 3660 Cow Contest(传递闭包)

    题意: n个点,m条边. 若A 到 B的边存在,则证明 A 的排名一定在 B 前. 最后求所有点中,排名可以确定的点的个数. n <= 100, m <= 4500 刚开始还在想是不是拓扑 ...

  10. Android CTS - Cannot run program "aapt"/ Fail to run aapt on .../apk installed but AaptParser failed

    今天同事碰到cts的一些问题,跑到某个apk的时候,就提示如下错误: Cannot run program "aapt": error=2. No such file or dir ...