http://www.lightoj.com/volume_showproblem.php?problem=1070

思路:\({(a+b)}^n =(a+b){(a+b)}^{n-1} \) \((ab)C_{n}^{r}a^{n-r}b{r} = C_{n+2}^{r}a^{n-r+2}b{r} - a^{n+2} - b^{n+2} \)

综上\( f(n) = (a+b)f(n-1)-(ab)f(n-2) \)

/** @Date    : 2016-12-19-19.53
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version :
*/
#include<bits/stdc++.h>
#define LL long long
#define PII pair
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8;
typedef unsigned long long ull; struct matrix
{
ull mt[2][2];
void init()
{
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
mt[i][j] = 0;
}
void cig()
{
for(int i = 0; i < 2; i++)
mt[i][i] = 1;
}
}; matrix mul(matrix a, matrix b)
{
matrix c;
c.init();
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
for(int k = 0; k < 2; k++)
{
c.mt[i][j] += a.mt[i][k] * b.mt[k][j];
}
return c;
} matrix fpow(matrix a, LL n)
{
matrix r;
r.init();
r.cig();
while(n > 0)
{
if(n & 1)
r = mul(r, a);
a = mul(a, a);
n >>= 1;
}
return r;
} ull fun(LL p, LL q, LL n)
{
if(n < 1)
{
return 2;
}
matrix base;
base.mt[0][0] = p;
base.mt[0][1] = -q;
base.mt[1][0] = 1;
base.mt[1][1] = 0;
base = fpow(base, n - 1);
ull ans = base.mt[0][0] * p + base.mt[0][1] * 2;
return ans;
} int main()
{
int T;
int cnt = 0;
cin >> T;
while(T--)
{
LL n, p , q;
scanf("%lld%lld%lld", &p, &q, &n);
ull ans = fun(p, q, n);
printf("Case %d: %llu\n", ++cnt, ans);
}
return 0;
}
//f(n) = (a+b)*f(n-1) - (ab)*f(n-2)

LightOJ 1070 - Algebraic Problem 推导+矩阵快速幂的更多相关文章

  1. LightOJ 1070 Algebraic Problem:矩阵快速幂 + 数学推导

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1070 题意: 给你a+b和ab的值,给定一个n,让你求a^n + b^n的值(MOD ...

  2. LightOJ 1070 Algebraic Problem (推导+矩阵高速幂)

    题目链接:problem=1070">LightOJ 1070 Algebraic Problem 题意:已知a+b和ab的值求a^n+b^n.结果模2^64. 思路: 1.找递推式 ...

  3. HDU1757-A Simple Math Problem,矩阵快速幂,构造矩阵水过

    A Simple Math Problem 一个矩阵快速幂水题,关键在于如何构造矩阵.做过一些很裸的矩阵快速幂,比如斐波那契的变形,这个题就类似那种构造.比赛的时候手残把矩阵相乘的一个j写成了i,调试 ...

  4. LightOJ 1070 - Algebraic Problem 矩阵高速幂

    题链:http://lightoj.com/volume_showproblem.php?problem=1070 1070 - Algebraic Problem PDF (English) Sta ...

  5. hdu 1757 A Simple Math Problem (矩阵快速幂,简单)

    题目 也是和LightOJ 1096 和LightOJ 1065 差不多的简单题目. #include<stdio.h> #include<string.h> #include ...

  6. LightOj 1065 - Number Sequence (矩阵快速幂,简单)

    题目 和 LightOj 1096 - nth Term 差不多的题目和解法,这道相对更简单些,万幸,这道比赛时没把模版给抽风坏. #include<stdio.h> #include&l ...

  7. [ An Ac a Day ^_^ ] hdu 4565 数学推导+矩阵快速幂

    从今天开始就有各站网络赛了 今天是ccpc全国赛的网络赛 希望一切顺利 可以去一次吉大 希望还能去一次大连 题意: 很明确是让你求Sn=[a+sqrt(b)^n]%m 思路: 一开始以为是水题 暴力了 ...

  8. A Simple Math Problem(矩阵快速幂)(寒假闭关第一题,有点曲折啊)

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...

  9. 2018.09.26 bzoj5221: [Lydsy2017省队十连测]偏题(数学推导+矩阵快速幂)

    传送门 由于没有考虑n<=1的情况T了很久啊. 这题很有意思啊. 考试的时候根本不会,骗了30分走人. 实际上变一个形就可以了. 推导过程有点繁杂. 直接粘题解上的请谅解. 不得不说这个推导很妙 ...

随机推荐

  1. 第四章 深入JSP技术

    JSP简介 JSP工作原理 JSP是一种servlet,但先部署后编译. JSP生命周期 运行时只会有一个实例,同servlet. JSP语法 JSP元素和模板数据 模板数据就是JSP中的HTML代码 ...

  2. QMdiArea及QMdiSubWindow实现父子窗口及布局方法

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QMdiArea及QMdiSubWindow实现父子窗口及布局方法     本文地址:http ...

  3. Qt动态连接库/静态连接库创建与使用,QLibrary动态加载库

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:Qt动态连接库/静态连接库创建与使用,QLibrary动态加载库     本文地址:https ...

  4. linux php 访问sql server设置

    1.安装freeTDS wget ftp://ftp.freetds.org/pub/freetds/stable/freetds-stable.tgz 1.1.进入到你下载的目录然后解压.tar - ...

  5. linux mysql表名大小写

    1.用ROOT登录,修改/etc/my.cnf 2.在[mysqld]下加入一行:lower_case_table_names=1 0:区分大小写,1:不区分大小写 3.重新启动数据库即可

  6. web_config配置

    <configuration>    <system.web>      <compilation debug="true" targetFramew ...

  7. 移动端开发-viewport

    1.viewport viewport 即设备 屏幕上显示网页的区域.因为移动设备屏幕比较小,为了能让移动设备能够显示更多内容,默认设置的viewport 并不是屏幕真是像素点的宽度,一般为980px ...

  8. DjangoORM字段参数介绍

    参数介绍: 字段的参数:     null:             ->db是否可以为空     default:          ->默认值     primary_key:     ...

  9. Ubuntu上搭建比特币运行环境

    Ubuntu版本:16.04.3 Bitcoin Core版本:0.16 1. 比特币运行依赖的开源库 (1)必须依赖的库 库 目的 描述 libssl 加密 随机数生成,椭圆曲线加密算法 libbo ...

  10. Splitter Control for Dialog

    原文链接地址:https://www.codeproject.com/Articles/595602/Splitter-Control-for-Dialog Introduction Yes, tha ...