HDU 5950Recursive sequence ICPC沈阳站
2024-08-24 22:21:12
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.
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);
}
}
最新文章
- merge into在oracle10g和oracle 11g中的使用差别一
- virtual_login
- Python模块:collections
- 【原创】初识懒人开发库---ButterKnife
- ssh端口转发
- mssql 动态行转列。
- DP(01背包) UESTC 1218 Pick The Sticks (15CCPC C)
- HDU 1003 Max Sum 解题报告
- 简单易懂的现代魔法&mdash;&mdash;Play Framework攻略3
- C++ 排序函数 sort(),qsort()的使用方法
- centOS tengine 安装后 不能访问的问题
- 跟踪对象属性值的修改, 设置断点(Break on property change)
- [转]C++ list 类学习笔记
- The Swift Programming Language--语言指南--协议
- 为什么在Python3.4.1里输入print 10000L或10000L失败
- nautilus出现一闪而过现象
- PostgreSQL对汉字按拼音排序
- cc.Lable组件,RichText组件,AudioSouce组件的使用
- spy-debugger 安装以及使用
- 6.3 基于二分搜索树、链表的实现的集合Set复杂度分析