Recursive sequence (矩阵快速幂)2016ACM/ICPC亚洲区沈阳站
题目
Farmer John likes to play mathematics games with his N cows. Recently, they are attracted by recursive sequences. In each turn, the cows would stand in a line, while John writes two positive numbers a and b on a blackboard. And then, the cows would say their identity number one by one. The first cow says the first number a and the second says the second number bb. After that, the ii-th cow says the sum of twice the (i−2)-th number, the (i−1)-th number, and i^4. Now, you need to write a program to calculate the number of the N-th cow in order to check if John’s cows can make it right.
Input
The first line of input contains an integer t, the number of test cases. tt test cases follow.
Each case contains only one line with three numbers N, aa and bb where N,a,b<2^31 as described above.
Output
For each test case, output the number of the NN-th cow. This number might be very large, so you need to output it modulo 2147493647.
In the first case, the third number is $85 = 21+2+3^4.Inthesecondcase,thethirdnumberis.Inthesecondcase,thethirdnumberis93 = 21+1*10+3^4andthefourthnumberisandthefourthnumberis369 = 2 * 10 + 93 + 4^4$.
题意
题目给出这样一个递推公式:
f(n)=f(n-2)*2+f(n-1)+n^ 4
f(1)=a,
f(2)=b;
给出n,a,b求f(n)%2147493647;
根据递推公式容易得出一个O(N)的暴力算法。但这题的数据范围为N,a,b<2^31 ,直接暴力肯定超时,要用O(1),O(lngn)或者是O(n^(1/2))的算法才有可能ac
观察这种递推公式,发现我们可以使用矩阵快速幂来计算,矩阵快速幂的复杂度是O(m^3 *lngn)m是矩阵的大小,复杂度很小可以一试。
矩阵快速幂是定义一个状态矩阵和一个转移矩阵,前一个状态矩阵乘转移矩阵可得到后一个状态矩阵。
这题和普通的矩阵快速幂有点区别,这题的递推函数在混合了一个变量n^4。
所以我们这题首先要考虑的就是,如何从n^4推出(n+1)^4,我们发现通过二项式定理可以得出,(n+1)^4=n^4+4n^3+6n^2+4n+1
这样就有f(n+1)=2*f(n-1)+f(n)+n^4+4n^3+6n^2+4n+1
不妨设状态矩阵F(n-1)=[f(n-1),f(n),n^4,4n^3,6n^2,4n,1]
若F(n)=F(N-1)*A 即[f(n),f(n+1),(n+1)^4,4(n+1)^3,6(n+1)^2,4(n+1),1]=[f(n-1),f(n),n^4,4n^3,6n^2,4n,1]*A
计算得A=
{0,2,0,0,0,0,0}
{1,1,0,0,0,0,0}
{0,1,1,0,0,0,0}
{0,1,1,1,0,0,0}
{0,1,1,2,1,0,0}
{0,1,1,3,3,1,0}
{0,1,1,4,6,4,1}
AC代码
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<set>
using namespace std;
typedef long long ll;
const ll mod=2147493647;
ll n; void mul(ll f[7],ll a[7][7]){
ll c[7];
memset(c,0,sizeof(c));
for(ll j=0;j<7;j++)
for(ll k=0;k<7;k++)
c[j]=(c[j]+(ll)f[k]*a[k][j])%mod;
memcpy(f,c,sizeof(c));
} void mulself(ll a[7][7]){
ll c[7][7];
memset(c,0,sizeof(c));
for(ll i=0;i<7;i++)
for(ll j=0;j<7;j++)
for(ll k=0;k<7;k++)
c[i][j]=(c[i][j]+(ll)a[i][k]*a[k][j])%mod;
memcpy(a,c,sizeof(c)); } int main(){
ll t,a,b;
scanf("%lld",&t);
while(t--){
scanf("%lld%lld%lld",&n,&a,&b);
ll f[]={a,b,16,32,24,8,1};
ll a[][7]={
{0,2,0,0,0,0,0},
{1,1,0,0,0,0,0},
{0,1,1,0,0,0,0},
{0,1,1,1,0,0,0},
{0,1,1,2,1,0,0},
{0,1,1,3,3,1,0},
{0,1,1,4,6,4,1},
};
n--;
for(;n;n>>=1){
if(n&1)mul(f,a);
mulself(a);
}
printf("%lld\n",f[0]); } }
Recursive sequence (矩阵快速幂)2016ACM/ICPC亚洲区沈阳站的更多相关文章
- HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- HDU5950 Recursive sequence —— 矩阵快速幂
题目链接:https://vjudge.net/problem/HDU-5950 Recursive sequence Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 5950 Recursive sequence 矩阵快速幂
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- HDU5950 Recursive sequence (矩阵快速幂)
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- 2016ACM/ICPC亚洲区沈阳站 - A/B/C/E/G/H/I - (Undone)
链接:传送门 A - Thickest Burger - [签到水题] ACM ICPC is launching a thick burger. The thickness (or the heig ...
- HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)
Counting Cliques Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- HDU 5948 Thickest Burger 【模拟】 (2016ACM/ICPC亚洲区沈阳站)
Thickest Burger Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- HDU 5949 Relative atomic mass 【模拟】 (2016ACM/ICPC亚洲区沈阳站)
Relative atomic mass Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- HDU 5950 - Recursive sequence - [矩阵快速幂加速递推][2016ACM/ICPC亚洲区沈阳站 Problem C]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 Farmer John likes to play mathematics games with ...
随机推荐
- 【python开发】迈出第一步,这可能是我唯一一次的Python开发了
好久没写博了,今天就瞎唠唠吧 背景: 组内有个测试平台,是基于Python2+tornado 框架写的,之前自己维护了一套系统的UIweb自动化代码,现在需要集成进去.这很可能是自己唯一一次基于pyt ...
- jumpserver如何在远程时使用复制粘贴-windwos系统下
jumpserver堡垒机搭建好了,但是在使用的时候,有时候会出现远程下不能复制粘贴,这让体验十分不爽. 于是着手解决这个问题,附上参考链接,感谢大佬:http://itren.xiaolee.net ...
- HDU-6608-Fansblog(威尔逊定理+快速乘)(多校)
Problem Description Farmer John keeps a website called ‘FansBlog’ .Everyday , there are many people ...
- 用navicat进行身份验证连接出现cannot connect to Mongodb authentication failed
用navicat进行身份验证连接出现cannot connect to Mongodb authentication failed. 解决办法: 1.打开mongoDB连接 win+r --cmd-- ...
- 阿里面试官:HashMap 熟悉吧?好的,那就来聊聊 Redis 字典吧!
最近,小黑哥的一个朋友出去面试,回来跟小黑哥抱怨,面试官不按套路出牌,直接打乱了他的节奏. 事情是这样的,前面面试问了几个 Java 的相关问题,我朋友回答还不错,接下来面试官就问了一句:看来 Jav ...
- 11_IO多路复用
1.IO概述 input 和 output: 是在内存中存在的数据交换操作 内存和磁盘交换: 文件读写, 打印 内存和网络交换: recv send recvfrom, sendto IO密集型程序: ...
- Ajxax技术-1
1.什么是Ajax Ajax: Asynchronous javascript and xml (异步javascript和xml). ==Ajax并不是一种新技术,而是已有技术的集合.JavaScr ...
- Java实现打开google浏览器
说明: 博主的Google浏览器版本:75.0.3770.142,如果运行异常,需要自行查找对应版本的驱动(chromedriver.exe) 需要的jar包: https://pan.baidu.c ...
- C#程序执行时设置刷新等待
背景 C#在进行某些调用操作时需要隐藏背景,等待刷新. 步骤 1.前端设置 <!--Loading Mask Region--> <div> <div id=" ...
- Ubuntu查看并修改主机名的方法
1 查看主机名 在Ubuntu系统中,快速查看主机名有多种方法: 在终端窗口中,主机名通常位于@符号后:root@lzyws739307453:~#例如这个的主机名就是lzyws739307453. ...