Partition

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

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
思路:递推式a[n]=2*a[n-1]+2^(n-2);
#include<bits/stdc++.h>
using namespace std;
#define ll __int64
#define esp 0.00000000001
const int N=3e5+,M=1e6+,inf=1e9,mod=1e9+;
struct is
{
ll a[][];
};
is juzhenmul(is a,is b,ll hang ,ll lie)
{
int i,t,j;
is ans;
memset(ans.a,,sizeof(ans.a));
for(i=;i<=hang;i++)
for(t=;t<=lie;t++)
for(j=;j<=lie;j++)
{
ans.a[i][t]+=(a.a[i][j]*b.a[j][t]);
ans.a[i][t]%=mod;
}
return ans;
}
is quickpow(is ans,is a,ll x)
{
while(x)
{
if(x&) ans=juzhenmul(ans,a,,);
a=juzhenmul(a,a,,);
x>>=;
}
return ans;
}
ll getans(ll x)
{
if(x==)
return ;
if(x==)
return ;
is ans,base;
memset(ans.a,,sizeof(ans.a));
ans.a[][]=;
ans.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
ans=quickpow(ans,base,x-);
return (ans.a[][]*+ans.a[][])%mod;
}
int main()
{
ll x,y,z,i,t;
int T;
scanf("%d",&T);
while(T--)
{
scanf("%I64d%I64d",&x,&y);
if(x>=y)
printf("%I64d\n",getans(x-y+));
else
printf("0\n");
}
return ;
}

hdu 4602 Partition 矩阵快速幂的更多相关文章

  1. 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 ...

  2. hdu 4602 递推关系矩阵快速幂模

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

  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 6185 Covering 矩阵快速幂

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

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

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

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

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

  8. HDU 6470 【矩阵快速幂】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6470 写这道题是为了让自己不要忘记矩阵快速幂如何推出矩阵式子的. 注意 代码是TLE的!! #incl ...

  9. HDU 5607 graph 矩阵快速幂 + 快速幂

    这道题得到了学长的助攻,其实就是一个马尔科夫链,算出一步转移矩阵进行矩阵快速幂就行了,无奈手残 这是我第一回写矩阵快速幂,写的各种毛病,等到调完了已经8点44了,交了一发,返回PE,(发现是少了换行) ...

随机推荐

  1. Flowers---hdu4325(区间处理 离散化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4325 题意:有n种花,每种花都有自己的开花时间段从S到E,有m个查询,每个查询都是一个时间点,求这一时 ...

  2. python脚本前两行

    1. 第一行指定解释器路径 推荐写法: #!/usr/bin/env python 详细说明: #!/usr/bin/python是告诉操作系统执行这个脚本的时候,调用/usr/bin下的python ...

  3. Shiro起步

    1.测试环境  IntelliJ Idea 2.pom配置 <?xml version="1.0" encoding="UTF-8"?> <p ...

  4. Apache配置虚拟主机的三种方法(基于IP、端口、域名)

    1 Apache虚拟主机的实现方式有3种. 基于IP的虚拟主机 基于端口的虚拟主机 基于域名的虚拟主机 2.1 启用虚拟主机的准备工作 2.1.1安装httpd [root@mail httpd]# ...

  5. python 随机分类

    #encoding:utf-8import pandas as pdimport numpy as npfrom sklearn import datasets,linear_modelfrom sk ...

  6. 推荐系统第6周--- SVD和基于标签的推荐系统

    “隐语义”的真正背景       LSA(latent semantic analysis)潜在语义分析,也被称为LSI(latent semantic index),是Scott Deerweste ...

  7. mysql lock

    http://blog.chinaunix.net/uid-21505614-id-289450.html http://bbs.csdn.net/topics/340127237 http://ww ...

  8. IOS中程序如何进行推送消息(本地推送,远程推送)2(下)

    内容中包含 base64string 图片造成字符过多,拒绝显示

  9. C++匿名名字空间

    转自:http://blog.csdn.net/eric_arrow/article/details/8978905 名字空间(namespace),是C++提供的一个解决符合名字冲突的特性.标准规定 ...

  10. gitHub新项目的上传

    github作为一个开源托管平台,除了有机会学习各位大神的开源项目,还能托管自己写的一些小Demo,作为github新进菜鸟,今天就整理下上传Demo所需的命令和操作步骤,防止我这谜一样的记忆力. 1 ...