题目: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[][];
  8. Matrix(int f=){
  9. memset(a,,sizeof a);
  10. if(f==)for(int i=;i<;++i)a[i][i]=;
  11. }
  12. };
  13. Matrix operator*(Matrix& A,Matrix& B){
  14. Matrix C;
  15. for(int k=;k<;++k)
  16. for(int i=;i<;++i)
  17. for(int j=;j<;++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();
  23. for(;n;n>>=){
  24. if(n&)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=;i<=T;++i){
  33. scanf("%llu%llu%u",&p,&q,&n);
  34. if(n==){
  35. printf("Case %d: 2\n",i);
  36. continue;
  37. }
  38. A.a[][]=;A.a[][]=-;
  39. A.a[][]=q;A.a[][]=p;
  40. ANS=A^(n-);
  41. LLU ans=*q*ANS.a[][]+ANS.a[][]*p;
  42. printf("Case %d: %llu\n",i,ans);
  43. }
  44. return ;
  45. }

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. [转载]Java中异常的捕获顺序(多个catch)

    http://blog.sina.com.cn/s/blog_6b022bc60101cdbv.html [转载]Java中异常的捕获顺序(多个catch) (2012-11-05 09:47:28) ...

  2. Drone 的插件 - Docker 插件

    Drone 插件市场 Drone 插件文档 原文地址 - Docker 插件的手册 Docker 插件可以用于构建镜像及发布镜像到 Docker registry.下面的 pipeline 配置,就使 ...

  3. 第 11 章 python线程与多线程

    一.什么是线程 在传统操作系统中,每个进程有一个地址空间,而且默认就有一个控制线程. 进程只是用来把资源集中到一起(进程只是一个资源单位,或者说资源集合),而线程才是cpu上的执行单位. 多线程(即多 ...

  4. Linux 系统故障修复和修复技巧

    我发现Linux系统在启动过程中会出现一些故障,导致系统无法正常启动,我在这里写了几个应用单用户模式.GRUB命令操作.Linux救援模式的故障修复案例帮助大家了解此类问题的解决. 一.单用户模式 L ...

  5. vs2010修改的内容在浏览器页面不变怎么办

    解决方法1: 如果是静态页面, Ctrl+F5 强制刷新浏览器页面 解决方法2: 页面中<%@ Page Language="C#" CodeBehind="Def ...

  6. Java单链表

    一.概述 二.主方法 //创建头结点 private HeroNode head = new HeroNode(-1,null,null); //计数器,用于id的自增 private static ...

  7. 用php实现一个简单的爬虫,抓取电影网站的视频下载地址

    昨天没什么事,先看一下电影,就用php写了一个爬虫在视频网站上进行视频下载地址的抓取,这里总结一下抓取过程中遇到的问题 1:通过访问浏览器来执行php脚本这种访问方式其实并不适合用来爬网页,因为要受到 ...

  8. go 学习之io/ioutil包

    // Discard 是一个 io.Writer 接口,调用它的 Write 方法将不做任何事情// 并且始终成功返回.var Discard io.Writer = devNull(0) // Re ...

  9. 排序---快速排序及其切分函数Partition应用

    快速排序   快速排序通过一个切分元素将数组分成两个子数组,左子数组小于等于切分元素,右子数组大于切分元素,将这两个子数组排序,也就是将整个数组排序了. 代码如下: public class Sort ...

  10. centos7配置sudo免密

    1.chmod  +w   /etc/sudoers 2.vim  /etc/sudoers 在已经有了的root下面加 username  ALL=NOPASSWD:ALL      (这是所有的命 ...