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);
}
}

最新文章

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

热门文章

  1. jdk1.8新特性-Lambda表达式使用要点
  2. 20172330 2017-2018-1 《Java程序设计》第七周学习总结
  3. c# word 删除指定内容
  4. 针对XX系统的可用性和易用性构想
  5. 链表相加(Add Two Numbers)
  6. iOS开发UIColor,CGColor,CIColor三者的区别和联系
  7. django 使用json.dumps转换queryset的datatime报错问题解决
  8. RPC架构-美团,京东面试题目
  9. 在DBGrid中可选中行而又可进入编辑状态
  10. BZOJ4247 挂饰(动态规划)