hdu 4602 递推关系矩阵快速幂模
Partition
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2362 Accepted Submission(s): 937
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).
递推公式: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 递推关系矩阵快速幂模的更多相关文章
- hdu 4602 Partition 矩阵快速幂
Partition Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Proble ...
- uva 10870 递推关系矩阵快速幂模
Recurrences Input: standard input Output: standard output Consider recurrent functions of the follow ...
- 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( 递推关系 + 矩阵快速幂 )
链接:传送门 题意:一个队列是由字母 f 和 m 组成的,队列长度为 L,那么这个队列的排列数为 2^L 现在定义一个E-queue,即队列排列中是不含有 fmf or fff ,然后问长度为L的E- ...
- HDU 2157(矩阵快速幂)题解
How many ways?? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 6185 Covering 矩阵快速幂
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6185 题意:用 1 * 2 的小长方形完全覆盖 4 * n的矩形有多少方案. 解法:小范围是一个经典题 ...
- HDU 6395 分段矩阵快速幂 HDU 6386 建虚点+dij
http://acm.hdu.edu.cn/showproblem.php?pid=6395 Sequence Time Limit: 4000/2000 MS (Java/Others) Me ...
- Codeforces 185A Plant( 递推关系 + 矩阵快速幂 )
链接:传送门 题意:输出第 n 年向上小三角形的个数 % 10^9 + 7 思路: 设 Fn 为第 n 年向上小三角形的个数,经过分析可以得到 Fn = 3 * Fn-1 + ( 4^(n-1) - ...
随机推荐
- idea中pom.xml没有工作 IDEA中maven项目pom.xml依赖不生效解决
问题: 今天在web项目中需要引入poi相关jar包.查看之下才发现pom.xml中的依赖虽然已经下载到了本地仓库 repository,但是却没有加入到项目路径的 Extenal Libraries ...
- Js笔记 14
<script> // <!-- 课 对象 // //对象的创建方法 // 1.var obj = {} plainobject 对象字面量 对象直接量 // 2.构造函数 ...
- ssh整合思想 Spring与Hibernate和Struts2的action整合 调用action添加数据库 使用HibernateTemplate的save(entity)方法 update delete get 等方法crud操作
UserAction类代码: package com.swift.action; import com.opensymphony.xwork2.ActionSupport; import com.sw ...
- iOS或Mac开发者应该记住的前缀
- tomcat关闭钩子
转 http://501565246-qq-com.iteye.com/blog/1733575 21,tomcat关闭钩子 博客分类: tomcat 在很多环境下,在关闭应用程序的时候需要做一些 ...
- [提供可行性脚本] RHEL/CentOS 7 多节点SSH免密登陆
实验说明: 在自动化部署时,会经常SSH别的机器去操作,然而每次的密码认证却很令人烦躁,尤其是很长的密码,因此SSH免密登陆就显得必不可少: 在机器数目很多的时候,使用更过的往往是Ansible分发并 ...
- 如何用纯 CSS 创作一组昂首阔步的圆点
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/ejrMKe 可交互视频 ...
- python基本操作(四)
与用户交互 为什么交互? 计算机取代人类,解放劳动力 如何交互 print('-'*100) input('请输入你的姓名:') print(""100) Python2和Pyth ...
- HDU - 6199 gems gems gems (DP)
有n(2e4)个宝石两个人轮流从左侧取宝石,Alice先手,首轮取1个或2个宝石,如果上一轮取了k个宝石,则这一轮只能取k或k+1个宝石.一旦不能再取宝石就结束.双方都希望自己拿到的宝石数比对方尽可能 ...
- Linux数据库忘记密码-修改方法
一.拥有原来的myql的root的密码: 方法一:在mysql系统外,使用mysqladmin# mysqladmin -u root -p password "test123"E ...