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. POJ 2653 Pick-up sticks(线段判交)

    Description Stan has n sticks of various length. He throws them one at a time on the floor in a rand ...

  2. 2018-9-25kanboard安装及使用

    2018-9-25kanboard安装及使用 教程 小书匠  欢迎走进zozo的学习之旅. 简介 运行官方docker容器 使用kanboard 简介 Kanboard的安装提供了两种方式一种是直接安 ...

  3. “Hello World”团队第一周博客汇总

    时间:2017-10-13——2017-10-19 Scrum会议: 会议要求博客:https://edu.cnblogs.com/campus/nenu/SWE2017FALL/homework/1 ...

  4. 关于CString总结

    前言:串操作是编程中最常用也最基本的操作之一. 做为VC程序员,无论是菜鸟或高手都曾用过CString.而且好像实际编程中很难离得开它(虽然它不是标准C++中的库).因为MFC中提供的这个类对 我们操 ...

  5. VS2005、VS2008中的快捷键、组合键大全

    Ctrl+E,D ----格式化全部代码 Ctrl+E,F ----格式化选中的代码 CTRL + SHIFT + B生成解决方案 CTRL + F7 生成编译 CTRL + O 打开文件 CTRL ...

  6. 利用SqlServer的作业定时清除过期数据

    有时候我们的数据库中可能会有那么些存放动态数据的表,比如一些每天定时发出的消息通知信息等数据.这些数据我们只需要临时保存,一些老旧的数据需要定时去清除掉,不然时间一长的话单表数据堆积非常严重.导致数据 ...

  7. Scala快速入门-基础

    HelloWorld 从HelloWorld开始,使用scala IDE编辑器. 新建scala project 新建scala object 编写HelloWorld run as scala ap ...

  8. CentOS 7 开放防火墙端口

    我:最近在使 CentOS 7时发现在本地不能访问linux上8080端口,以上是我的操作,修改后访问成功 CentOS 7 开放防火墙端口 命令 最近公司新的server要求用CentOS7, 发现 ...

  9. 《Effective C#》快速笔记(六)- - C# 高效编程要点补充

    目录 四十五.尽量减少装箱拆箱 四十六.为应用程序创建专门的异常类 四十七.使用强异常安全保证 四十八.尽量使用安全的代码 四十九.实现与 CLS 兼容的程序集 五十.实现小尺寸.高内聚的程序集 这是 ...

  10. 生活中的goto

    if(你是个傻逼?){ out.println("继续你的傻逼生活吧!"); }else(你不是傻逼?){ out.println("你说不是都不是啊,继续你的傻逼生活吧 ...