题目:Algebraic Problem

链接:https://vjudge.net/problem/LightOJ-1070

分析:

1)$ a^n+b^n = ( a^{n-1}+b^{n-1} )*(a+b) - (a*b^{n-1}+a^{n-1}*b) $

构造矩阵: $ \left[ \begin{array}{cc} 0 & -1 \\ a*b & a+b \end{array} \right] $

$$ \left[ \begin{array}{cc} a*b^{n-1}+a^{n-1}*b  &   a^{n-1}+b^{n-1} \end{array} \right]  *  \left[ \begin{array}{cc} 0 & -1 \\ a*b & a+b \end{array} \right]  = \left[ \begin{array}{cc} a*b^n+a^n*b  &   a^n+b^n \end{array} \right] $$

2)注意特判0的情况,至于对$2^{64}$取模,开unsigned long long,自然溢出即可。

 #include <iostream>
#include <cstring>
using namespace std;
typedef unsigned long long LLU;
typedef unsigned int uint;
struct Matrix{
LLU a[][];
Matrix(int f=){
memset(a,,sizeof a);
if(f==)for(int i=;i<;++i)a[i][i]=;
}
};
Matrix operator*(Matrix& A,Matrix& B){
Matrix C;
for(int k=;k<;++k)
for(int i=;i<;++i)
for(int j=;j<;++j)
C.a[i][j]+=A.a[i][k]*B.a[k][j];
return C;
}
Matrix operator^(Matrix A,uint n){
Matrix Rt();
for(;n;n>>=){
if(n&)Rt=Rt*A;
A=A*A;
}
return Rt;
}
int main(){
int T;scanf("%d",&T);
Matrix A,ANS;LLU p,q;uint n;
for(int i=;i<=T;++i){
scanf("%llu%llu%u",&p,&q,&n);
if(n==){
printf("Case %d: 2\n",i);
continue;
}
A.a[][]=;A.a[][]=-;
A.a[][]=q;A.a[][]=p;
ANS=A^(n-);
LLU ans=*q*ANS.a[][]+ANS.a[][]*p;
printf("Case %d: %llu\n",i,ans);
}
return ;
}

3)$ a^n + b^n = (a^{n-1}+b^{n-1})*(a+b) - (a*b^{n-1}+a^{n-1}*b) = (a^{n-1}+b^{n-1})*(a+b)-a*b*(b^{n-2}+a^{n-2}) $

构造矩阵:$ \left[ \begin{array}{cc} a+b & -ab \\ 1 & 0 \end{array} \right] $

$$ \left[ \begin{array}{cc} a+b & -ab \\ 1 & 0 \end{array} \right] *  \left[ \begin{array}{c} a^{n-1}+b^{n-1}   \\   a^{n-2}+b^{n-2} \end{array} \right]  = \left[ \begin{array}{c} a^n+b^n \\   a^{n-1}+b^{n-1} \end{array} \right] $$

[LightOJ1070]Algebraic Problem的更多相关文章

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

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

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

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

  3. LightOJ 1070 - Algebraic Problem 推导+矩阵快速幂

    http://www.lightoj.com/volume_showproblem.php?problem=1070 思路:\({(a+b)}^n =(a+b){(a+b)}^{n-1} \) \(( ...

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

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

  5. 1065 - Number Sequence &&1070 - Algebraic Problem

    1065 - Number Sequence   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB ...

  6. LOJ1070(SummerTrainingDay05-B 矩阵快速幂)

    Algebraic Problem Given the value of a+b and ab you will have to find the value of an+bn. a and bnot ...

  7. Algebraic Kernel ( Arithmetic and Algebra) CGAL 4.13 -User Manual

    1 Introduction Real solving of polynomials is a fundamental problem with a wide application range. T ...

  8. CSUOJ 1525 Algebraic Teamwork

    Problem A Algebraic Teamwork The great pioneers of group theory and linear algebra want to cooperate ...

  9. 1199 Problem B: 大小关系

    求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...

随机推荐

  1. idhttpserver 下载文件

    procedure TForm30.IdHTTPServer1CommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AR ...

  2. spss如何选择需要的变量?

    spss如何选择需要的变量? 今天一位网友问我,spss如何在许多字段(变量)中选择我需要的字段,而不显示其他的字段呢? 这个问题问的很好,在实际的数据分析或者挖掘的过程中,都需要用这个来找出对商业问 ...

  3. Drone 持续集成实践 - 基于 Gogs,以 Golang 为例

    Drone 官方示例 - Example Go project 用 Docker 部署 Go 服务器 Golang 官方示例 - outyet 一个生产环境的例子 用 rsync 复制文件的方式进行部 ...

  4. java.sql.SQLException:连接是只读的。不允许导致数据修改的查询

    我的项目中 service的实现类少了写一个注解   : 只读事务(@Transactional(readOnly = true) 应用场合: 如果你一次执行单条查询语句,则没有必要启用事务支持,数据 ...

  5. JavaScript Tre

    function BinarySearchTree() { var Node = function(key) { this.key = key; this.left = null; this.righ ...

  6. Manacher(最长镜面回文串)

    I - O'My! Gym - 101350I Note: this is a harder version of Mirrored string I. The gorillas have recen ...

  7. 基于Airtest实现python自动抢红包

    一.Airtest的介绍 1)Airtest的简介 Airtest是网易出品的一款基于图像识别和poco控件识别的一款UI自动化测试工具. Airtest的框架是网易团队自己开发的一个图像识别框架,这 ...

  8. 【学习总结】cpu缓存

    参考链接: cpu缓存java性能问题初探 高速缓存 在内存与cpu寄存器之间,还有一块区域叫做cpu高速缓存,即我们常常说的cache. cache分为L1.L2.L3三级缓存,速度递减,离cpu越 ...

  9. Linux系统平台调优

  10. windows切换窗口和网页快捷键

    alt+tab 切换窗口win+D 显示桌面,再按一下返回运来的网页win+M 所有程序最小化 网页之间切换(我用的是360) ctrl + tab 往回切 ctrl + shift +tab