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. HDU 1403 Longest Common Substring(后缀自动机——附讲解 or 后缀数组)

    Description Given two strings, you have to tell the length of the Longest Common Substring of them. ...

  2. 软件工程part5

    1.本周psp 2.本周饼状图 3.本周进度条

  3. MyEclipse2013使用总结

    1.myeclipse10中怎样将建的包设置成树形结构或者并列结构. 右上边三角那里进去设置选第一个是显示完整的包名,第二个显示的是树形结构这种方法没效 2.从高版本到项目的低版本的MyEclipse ...

  4. NeoLoad系列- 快速上手教程

    1.新建工程 2.点击录制脚本按钮 3.在弹出的开始录制对话框中,填写虚拟用户信息. Record in下拉框,用来填写用户路径,一般有三个容器组成: Init, Actions, and End.当 ...

  5. Linux命令之查看cpu个数_核数_内存总数

    http://blog.csdn.net/cgwcgw_/article/details/10000053 cpu个数 cat /proc/cpuinfo | grep "physical ...

  6. 异步请求Python库 grequests的应用和与requests库的响应速度的比较

    requests库是python一个优秀的HTTP库,使用它可以非常简单地执行HTTP的各种操作,例如GET.POST等.不过,这个库所执行的网络请求都是同步了,即cpu发出请求指令后,IO执行发送和 ...

  7. React 16.x & Hooks

    React 16.x & Hooks Hooks https://reactjs.org/docs/hooks-intro.html https://reactjs.org/docs/hook ...

  8. InnoDB高并发原理

    一.并发控制 为啥要进行并发控制? 并发的任务对同一个临界资源进行操作,如果不采取措施,可能导致不一致,故必须进行并发控制(Concurrency Control). 技术上,通常如何进行并发控制? ...

  9. WPF一个对象显示多个属性

    一个对象显示多个属性使用模板的方法: 如图: <dataTemplate x:key="MyDataTemplate">

  10. Selector 模型

    1.服务器端: import selectors import socket sel = selectors.DefaultSelector() #生成一个select对象 def accept(so ...