题目: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. 屏幕适配dip

    android适配一般使用dpi 那dpi与分辨率,屏幕尺寸的关系 DPI值计算是屏幕对角线的像素值除以屏幕的大小 dip=/ 屏幕尺寸, 比如:计算WVGA(800*480)分辨率,3.7英寸的密度 ...

  2. 排序算法三:堆排序(Heapsort)

    堆排序(Heapsort)是一种利用数据结构中的堆进行排序的算法,分为构建初始堆,减小堆的元素个数,调整堆共3步. (一)算法实现 protected void sort(int[] toSort) ...

  3. .net Datatable

    1. ROW remove vs delete datatable dt = new datatable() //fill 5 records for each row as datarow in d ...

  4. JAVA模拟Spring实现IoC过程(附源码)

    前言:本人大四学生,第一次写博客,如果有写得不好的地方,请大家多多指正 一.IoC(Inversion of Control)反转控制 传统开发都是需要对象就new,但这样做有几个问题: 效率低下,创 ...

  5. 红玫瑰&爱情转移

  6. Codeforces 1000E We Need More Bosses (边双连通+最长链)

    <题目链接> 题目大意:给定一个$n$个节点$m$条边的无向图,问你对任意两点,最多有多少条特殊边,特殊边指删除这条边后,这两个点不能够到达. 解题分析: 特殊变其实就是指割边,题意就是问 ...

  7. python 实现加法

    https://ac.nowcoder.com/acm/contest/338/G 链接:https://ac.nowcoder.com/acm/contest/338/G来源:牛客网 题目描述 Th ...

  8. angularjs calling order

    Here's the calling order: app.config()app.run()directive's compile functions (if they are found in t ...

  9. ES6——函数-箭头函数

    箭头函数: 1.普通函数 function 函数名(){...} 2.箭头函数 注意:  1)如果只有一个返回值,{}return可以省略: let arr = [12,5,8,99,33,14,26 ...

  10. rsync之expect脚本shell

    r_expect.sh: #!/bin/expect -f set timeout 30 #spawn rsync -avz --delete --exclude-from=exclude.list ...