[LightOJ1070]Algebraic Problem
题目: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的更多相关文章
- LightOJ 1070 - Algebraic Problem 矩阵高速幂
题链:http://lightoj.com/volume_showproblem.php?problem=1070 1070 - Algebraic Problem PDF (English) Sta ...
- LightOJ 1070 Algebraic Problem (推导+矩阵高速幂)
题目链接:problem=1070">LightOJ 1070 Algebraic Problem 题意:已知a+b和ab的值求a^n+b^n.结果模2^64. 思路: 1.找递推式 ...
- LightOJ 1070 - Algebraic Problem 推导+矩阵快速幂
http://www.lightoj.com/volume_showproblem.php?problem=1070 思路:\({(a+b)}^n =(a+b){(a+b)}^{n-1} \) \(( ...
- LightOJ 1070 Algebraic Problem:矩阵快速幂 + 数学推导
题目链接:http://lightoj.com/volume_showproblem.php?problem=1070 题意: 给你a+b和ab的值,给定一个n,让你求a^n + b^n的值(MOD ...
- 1065 - Number Sequence &&1070 - Algebraic Problem
1065 - Number Sequence PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB ...
- 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 ...
- 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 ...
- CSUOJ 1525 Algebraic Teamwork
Problem A Algebraic Teamwork The great pioneers of group theory and linear algebra want to cooperate ...
- 1199 Problem B: 大小关系
求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...
随机推荐
- Nginx 实现全站 HTTPS(基于 Let's Encrypt 的免费通配符证书)
单域名证书的生成可以 参考这里. acme.sh 项目中文文档 Let's Encrypt 在 18 年 1 月份推出了 ACME v2,支持通配符域名证书,对小网站.个人站长的友好度进一步增加. 常 ...
- 【PP系列】SAP 取消报工后修改日期
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[PP系列]SAP 取消报工后修改日期 前言 ...
- Linux 内核剖析
https://www.ibm.com/developerworks/cn/linux/l-linux-kernel/ 由于本文的目标是对 Linux 内核进行介绍并探索其体系结构和主要组件,因此首先 ...
- Recurrent Neural Network(2):BPTT and Long-term Dependencies
在RNN(1)中,我们将带有Reccurent Connection的node依照时间维度展开成了如下的形式: 在每个时刻t=0,1,2,3,...,神经网络的输出都会产生error:E0,E1,E2 ...
- vscode 在ubuntu的terminal中下划线不显示解决方案
Ctrl+Shift+P,打开搜索,Perferences:Open User Settings 设置Editor:Font Family 为 'Ubuntu Mono', monospace 保存, ...
- Vuejs中关于computed、methods、watch,mounted的区别
1.computed是在HTML DOM加载后马上执行的,如赋值: 2.methods则必须要有一定的触发条件才能执行,如点击事件: 3.watch呢?它用于观察Vue实例上的数据变动.对应一个对象, ...
- TCL环境检查
set w [open 1.txt w+] foreach a [info var] { if { [llength [array name $a]]==0 } { puts $w $a:[set $ ...
- Go语言_方法和接口
方法和接口 本节课包含了方法和接口,可以用这种构造来定义对象及其行为. Go 作者组编写,Go-zh 小组翻译. https://tour.go-zh.org/methods/1 方法 Go 没有类. ...
- sql插入语句笔记
使用INSERT插入数据行 [一次插入一行数据] 全写: INSERT INTO renshi (name, sex, age ,tel) VALUES ('胡大姐','女','35','13 ...
- JavaScript常用字符串方法和属性
一直以来 在喜马拉雅上听 陪你读书(JavaScript WEB前端) 主播沙翼 讲的很好 果断买了这本书 现在做个笔记 var str = ‘abcd’ str.charAt(0); // ...