Recursive sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1525    Accepted Submission(s): 710

Problem Description
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 b. After that, the i-th cow says the sum of twice the (i-2)-th number, the (i-1)-th number, and i4. 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. t test cases follow.
Each case contains only one line with three numbers N, a and b where N,a,b < 231 as described above.
 
Output
For each test case, output the number of the N-th cow. This number might be very large, so you need to output it modulo 2147493647.
Sample Input
2
3 1 2
4 1 10
Sample Output
85
369

Hint

In the first case, the third number is 85 = 2*1十2十3^4.

In the second case, the third number is 93 = 2*1十1*10十3^4 and the fourth number is 369 = 2 * 10 十 93 十 4^4.

递推超时,矩阵快速幂

#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <map>
#include <set>
#include <ctime>
#include <queue> #define LL long long using namespace std; const LL _MOD = , maxN = , MOD = _MOD*; int n; LL f(int _n)
{
LL n = _n, ans =, t=;
t = t*n%MOD; ans = (ans + t*)%MOD;
t = t*n%MOD; ans = (ans + t*)%MOD;
t = t*n%MOD; ans = (ans + t*)%MOD;
t = t*n%MOD; ans = (ans + t)%MOD;
return ans/ % _MOD;
} struct matrix
{
int n, m;
LL a[maxN][maxN];
LL* operator [](int x) {return a[x];}
void print()
{
for(int i = ; i <= n; i++)
{
for(int j = ; j <= m; j++)
printf("%d ", a[i][j]);
printf("\n");
}
printf("\n");
}
}; matrix operator *(matrix a, matrix b)
{
matrix c; c.n = a.n; c.m = b.m;
memset(c.a, , sizeof(c.a));
LL tmp;
for(int i = ; i <= a.n; i++)
{
tmp = ;
for(int j = ; j <= b.m; j++)
{
for(int k = ; k <= a.m; k++) tmp = (tmp+a[i][k] * b[k][j])%_MOD;
c[i][j] = tmp % _MOD;
tmp = ;
}
}
return c;
} matrix operator ^(matrix a, LL x)
{
matrix b;
memset(b.a, , sizeof(b.a));
b.n = a.n; b.m = a.m;
for(int i=; i <= a.n; i++) b[i][i]=;
for(;x;a=a*a,x>>=) if(x&) b=b*a;
return b;
} int main()
{
// cout<<2*f(3)+f(4)-f(5)<<endl;
// return 0;
#ifndef ONLINE_JUDGE
freopen("test_in.txt", "r", stdin);
//freopen("test_out.txt", "w", stdout);
#endif
int T; scanf("%d", &T);
while(T--)
{
int a, b, n; scanf("%d%d%d", &n, &a, &b);
LL _a = a; _a += f(); LL _b = b; _b += f();
matrix m; m.n = m.m = ; m[][] = _a; m[][] = _b; m[][] = m[][] = ;
matrix t; t.n = t.m = ; t[][] = ; t[][] = ; t[][] = t[][] = ;
t = t^(n-);
m = m*t;
LL ans = (m[][] - f(n) + _MOD) % _MOD;
printf("%d\n", (int)ans);
}
}

HDU 5950Recursive sequence ICPC沈阳站的更多相关文章

  1. 2015 ICPC 沈阳站M题

    M - Meeting Time Limit:6000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit ...

  2. 2016 ACM/ICPC 沈阳站 小结

    铜铜铜…… 人呐真奇怪 铁牌水平总想着运气好拿个铜 铜牌水平总想着运气好拿个银 估计银牌的聚聚们一定也不满意 想拿个金吧 这次比赛挺不爽的 AB两道SB题,十分钟基本全场都过了 不知道出这种题有什么意 ...

  3. 2020 ICPC 沈阳站 I - Rise of Shadows 题解

    题面看这里 \(PS\):符号 \([\ \rm P\ ]\) 的意义是:当表达式 \(\rm P\) 为真则取值为 \(1\),为假则取值为 \(0\). 题目大意 给你一个一天有 \(H\)​​​ ...

  4. HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  5. HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)

    Counting Cliques Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  6. HDU 5948 Thickest Burger 【模拟】 (2016ACM/ICPC亚洲区沈阳站)

    Thickest Burger Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  7. HDU 5949 Relative atomic mass 【模拟】 (2016ACM/ICPC亚洲区沈阳站)

    Relative atomic mass Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  8. HDU 6227.Rabbits-规律 (2017ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学))

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

  9. HDU 6225.Little Boxes-大数加法 (2017ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学))

    整理代码... Little Boxes Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/O ...

随机推荐

  1. 【转】使用CNPM搭建私有NPM

    最近的Node项目中因为数据模型等问题,需要有一个对各个模块进行统一的管理,如果把私有的模型publish到公共的npm不太合适,所以决定使用cnpm搭建一个私有的npm,同时也可以对项目常用的npm ...

  2. P4编程环境搭建

    本文参照了sdnlab上相关文章的搭建推荐. 使用的系统环境为ubuntu 18.04 组件介绍 主要安装五个组件: BMv2:是一款支持P4编程的软件交换机 p4c:是一款P4的编译器 PI:是P4 ...

  3. Alpha 冲刺4

    队名:日不落战队 安琪(队长) 今天完成的任务 组织第四次站立式会议. 完成40%草稿箱前端界面. 明天的计划 剩下的60%草稿箱前端界面. 如果还有时间,尝试去调用数据. 还剩下的任务 回收站前端界 ...

  4. TCP系列15—重传—5、Linux中RTO的计算

    之前我们介绍的都是协议中给出的RTO计算方法,下面我们看一下linux实现中RTO的计算方法.在linux中维护了srtt.mdev.mdev_max.rttvar.rtt_seq几个状态变量用来计算 ...

  5. OSG学习:位置变换节点示例

    osg::PositionAttitudeTransform节点. #include <osgViewer\Viewer> #include <osg\Node> #inclu ...

  6. 3dContactPointAnnotationTool开发日志(十四)

      貌似每次让用户手动输入文件路径太不人道了,于是参考Unity 实用教程 之 调用系统窗口选择文件或路径增加了让用户浏览文件的功能,点击输入框旁边的+就可以找到文件并加载进来:   貌似调整位置再计 ...

  7. Java线程常用方法详解

    线程的常用方法 1.start() : 线程调用该方法将启动线程,使之从新建状态进入就绪队列排队,一旦轮到它来享用CPU资源时,就可以脱离创建它的线程独立开始自己的生命周期了. 2.run(): Th ...

  8. (沒有介紹標準算法的)RMQ問題

    感謝杜哥代碼滋磁 //以下是廢話 RMQ (Range Minimum/Maximum Query)问题是指:对于长度为n的数列A,回答若干询问RMQ(A,i,j)(i,j<=n),返回数列A中 ...

  9. [洛谷P3793]由乃救爷爷

    题目大意:有$n(n\leqslant2\times10^7)$个数,$m(m\leqslant2\times10^7)$个询问,每次询问问区间$[l,r]$中的最大值.保证数据随机 题解:分块,处理 ...

  10. [Vue源码分析] v-model实现原理

    最近小组有个关于vue源码分析的分享会,提前准备一下… 前言:我们都知道使用v-model可以实现数据的双向绑定,及实现数据的变化驱动dom的更新,dom的更新影响数据的变化.那么v-model是怎么 ...