[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,自然溢出即可。
1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 typedef unsigned long long LLU; 5 typedef unsigned int uint; 6 struct Matrix{ 7 LLU a[2][2]; 8 Matrix(int f=0){ 9 memset(a,0,sizeof a); 10 if(f==1)for(int i=0;i<2;++i)a[i][i]=1; 11 } 12 }; 13 Matrix operator*(Matrix& A,Matrix& B){ 14 Matrix C; 15 for(int k=0;k<2;++k) 16 for(int i=0;i<2;++i) 17 for(int j=0;j<2;++j) 18 C.a[i][j]+=A.a[i][k]*B.a[k][j]; 19 return C; 20 } 21 Matrix operator^(Matrix A,uint n){ 22 Matrix Rt(1); 23 for(;n;n>>=1){ 24 if(n&1)Rt=Rt*A; 25 A=A*A; 26 } 27 return Rt; 28 } 29 int main(){ 30 int T;scanf("%d",&T); 31 Matrix A,ANS;LLU p,q;uint n; 32 for(int i=1;i<=T;++i){ 33 scanf("%llu%llu%u",&p,&q,&n); 34 if(n==0){ 35 printf("Case %d: 2\n",i); 36 continue; 37 } 38 A.a[0][0]=0;A.a[0][1]=-1; 39 A.a[1][0]=q;A.a[1][1]=p; 40 ANS=A^(n-1); 41 LLU ans=2*q*ANS.a[0][1]+ANS.a[1][1]*p; 42 printf("Case %d: %llu\n",i,ans); 43 } 44 return 0; 45 } 46
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] $$
转载于:https://www.cnblogs.com/hjj1871984569/p/10034169.html
[LightOJ1070]Algebraic Problem相关推荐
- LOJ 1070 Algebraic Problem
LOJ 1070 Algebraic Problem 题目链接:Light OJ 1070 Algebraic Problem Problem Description Given the value ...
- LightOJ 1070 Algebraic Problem (推导+矩阵快速幂)
题目链接:LightOJ 1070 Algebraic Problem 题意:已知a+b和ab的值求a^n+b^n.结果模2^64. 思路: 1.找递推式 得到递推式之后就是矩阵快速幂了 注意:模2^ ...
- LIGHT OJ1070 Algebraic Problem
题目: Given the value of a+b and ab you will have to find the value of a^n+b^n. a and b not necessaril ...
- list下界_下界理论
list下界 下界理论 (Lower Bound Theory) Lower bound (L(n)) is a property of the specific problem i.e. the s ...
- 当代数学大师迈克尔阿蒂亚证明黎曼猜想
去9月24日,89岁高龄的当代数学大师迈克尔阿蒂亚先生亲自前往海德堡高级数学论坛公开证明黎曼猜想,此举吸引了全球数学界的目光. 阿蒂亚指出:黎曼猜想的核心问题是素数分布定理,两者关系密切.国人对此事件 ...
- CSUOJ 1525 Algebraic Teamwork
Problem A Algebraic Teamwork The great pioneers of group theory and linear algebra want to cooperate ...
- 【minimal problem】资料整理
minimal problem use as few data as to generate a system of algebraic equaIons with a finite number o ...
- 【P9】Point to the Expression:Solving Algebraic Word Problems using the Expression-Pointer Transformer
Point to the Expression: Solving Algebraic Word Problems using the Expression-Pointer Transformer Mo ...
- linux下yum错误:[Errno 14] problem making ssl connection Trying other mirror.
所有的base 都要取消注释 mirrorlist 加上注释 另外所有的enable都要设为零 目录 今天是要yum命令安装EPEL仓库后 yum install epel-release 突然发现y ...
最新文章
- 皮一皮:前女友竟然说这种话...
- Python后端相关技术/工具栈
- 成员函数的const究竟修饰的是谁
- webstorm设置
- 最简单的composer 包 使用
- VTK使用矢量数据弯曲几何体
- 选择安防交换机时需要避免哪些误区呢?
- CoderHub接口文档
- 傅奎:十年安全路,一颗好奇心
- 华为HarmonyOS 鸿蒙,华为鸿蒙HarmonyOS2.0手机开发者Beta版正式发布
- c# 模拟登陆 webbrowser 抓取_《VR+电力——更换绝缘子培训》已登陆Pico Neo 2
- 知识点026-rsync命令的使用
- MOQL--面向流的统计过滤技术
- win10安装kali组双系统
- c++ 11/14新特性
- 软件测试-兼容性测试
- 汉语语法研究参考文献
- 简单使用pdf插件pdf.js
- IE浏览器控制台空白
- 信息级联(Information cascade)