原题链接:
Given the value of a+b and ab you will have to find the value of an+bn. a and b not necessarily have to be real numbers.

Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.

Each case contains three non-negative integers, p, q and n. Here p denotes the value of a+b and q denotes the value of ab. Each number in the input file fits in a signed 32-bit integer. There will be no such input so that you have to find the value of 00.

Output
For each test case, print the case number and (an+bn) modulo 264.

Sample Input
2

10 16 2

7 12 3

Sample Output
Case 1: 68

Case 2: 91
题意:
给出a+b的和p,ab的积q,要求计算出a^n+b^n的值。
题解:
利用递推公式:a^n+b^n=(a+b)(a^(n-1)+b^(n-1))-(a
b)(a^(n-2)+b^(n-2))
建立矩阵关系:A=(p,1,-q,0)(2x2方阵)
(ans[n],ans[n-1])=A^(n-2)(ans[2],ans[1])
**注意:**由于A的指数为n-2,所以要对于n=0,1,2的三种情况进行特判。
由于题目要求mod2^64,所以数据类型必须是unsigned long long,至于mod2^64,这个好像没有必要。
附上AC代码:

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
#define LL unsigned long long
struct mat
{LL a[2][2];mat(){memset(a,0,sizeof(a));}mat operator * (mat b){mat tmp;for(int i=0;i<2;++i)for(int j=0;j<2;++j)for(int k=0;k<2;++k)tmp.a[i][j]+=(a[i][k]*b.a[k][j]);return tmp;}
};
LL p,q,n;
mat quickpow(mat a,LL b)
{mat res;for(int i=0;i<2;++i)res.a[i][i]=1;while(b!=0){if(b&1)res=res*a;a=a*a;b>>=1;}return res;
}
int main()
{int t;scanf("%d",&t);for(int cas=1;cas<=t;cas++){scanf("%llu%llu%llu",&p,&q,&n);if(n==0){printf("Case %d: 2\n",cas);continue;}if(n==1){printf("Case %d: %llu\n",cas,p);continue;}if(n==2){printf("Case %d: %llu\n",cas,p*p-2*q);continue;}mat A,ans;ans.a[0][1]=p,ans.a[0][0]=(p*p-2*q);A.a[0][0]=p,A.a[0][1]=1,A.a[1][0]=-q;A=quickpow(A,n-2);ans=ans*A;printf("Case %d: %llu\n",cas,ans.a[0][0]);}return 0;
}

欢迎评论!

LightOJ-1070- Algebraic Problem (矩阵快速幂)相关推荐

  1. LightOJ 1070 Algebraic Problem (推导+矩阵快速幂)

    题目链接:LightOJ 1070 Algebraic Problem 题意:已知a+b和ab的值求a^n+b^n.结果模2^64. 思路: 1.找递推式 得到递推式之后就是矩阵快速幂了 注意:模2^ ...

  2. DUToj1085 Water Problem(矩阵快速幂)

    Problem I: Water Problem Time Limit:3000/1000 MS (Java/Others)   Memory Limit:163840/131072 KB (Java ...

  3. LightOJ 1096 - nth Term 矩阵快速幂

    http://www.lightoj.com/volume_showproblem.php?problem=1096 题意:\(f(n)  = a * f(n-1) + b * f(n-3) + c, ...

  4. HDU - 1757 A Simple Math Problem(矩阵快速幂,水题)

    题目链接:点击查看 题目大意:实现公式: f(x)=x,x<10 f(x)=a0*f(x-1)+a1*f(x-2)+--+a9*f(x-10) 题目给出a0~a9,一个n和一个m,要求输出f(n ...

  5. HDU 1757 A Simple Math Problem (矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 在吴神的帮助下才明白如何构造矩阵,还是好弱啊. 此处盗一张图 1 #include <io ...

  6. HDU 4291 A Short problem 矩阵快速幂 循环节

    题解思路: 构造矩阵,矩阵乘法计算还是t; 需要找循环节;   (注意因为是复合函数,不可以在里面取mod) 暴力跑只有可以找到g(222222224)%1e9==g(0)%1e9; 所以 g(g(n ...

  7. A Simple Math Problem(矩阵快速幂)

    Lele now is thinking about a simple function f(x). If x < 10 f(x) = x. If x >= 10 f(x) = a0 * ...

  8. 【HDU - 1757】A Simple Math Problem (矩阵快速幂)

    题干: Lele now is thinking about a simple function f(x). If x < 10 f(x) = x.  If x >= 10 f(x) = ...

  9. foj2198 Problem 2198 快来快来数一数 dp 矩阵快速幂

    Problem 2198 快来快来数一数 Accept: 67 Submit: 194 Time Limit: 1000 mSec Memory Limit : 65536 KB Problem De ...

  10. BC#29A:GTY's math problem(math) B:GTY's birthday gift(矩阵快速幂)

    A: HDU5170 这题让比较a^b与c^d的大小.1<=a,b,c,d<=1000. 显然这题没法直接做,要利用对数来求,但是在math库中有关的对数函数返回的都是浮点数,所以这又要涉 ...

最新文章

  1. vue - 使用axios
  2. oracle撤销管理的方法,Oracle9i中利用自动撤销管理的优点
  3. iis 重启 (三种方法)
  4. 【云图】一键生成连锁店品牌地图
  5. python调用java方法_python调用java
  6. OSI七层-相关协议
  7. JavaScript从入门到放弃 -(二)继承
  8. oracle关于分区,关于Oracle分区
  9. storm风暴英雄 tempo_Tempostorm战队攻略:新版本下的终结者天赋
  10. Redis Srem 命令
  11. python数据分析之Panads-1
  12. 美国英文简写日期格式转换
  13. 自动驾驶平台Apollo 2.5环境搭建
  14. 什么是即时消息(IM)?
  15. cct省计算机等级有用吗,省计算机二级有用吗
  16. 近期金三银四旺季,网上出现各种各样的面试文章跟视频,以下是我整理的一些拙见
  17. 2021年茶艺师(初级)考试总结及茶艺师(初级)模拟考试
  18. 唐骏离职新华都当老板
  19. Python网络爬虫与信息提取(9)—— scrapy实战之爬取黑马程序员网页讲师信息
  20. 2018年AI领域成人才紧缺行业

热门文章

  1. CAD工程数据库的建立及其访问方法
  2. Spring 事务注解@Transactional使用注意事项
  3. java 写入parquet_java-Spark:写入 parquet file数据类型时不理解的行为
  4. C++ 使用typename来修复模板编译错误
  5. 企业员工电脑监控软件的优势有哪些
  6. python模糊匹配忽略大小写_正则表达式忽略大小写匹配,但保留特定大小写的结果...
  7. pda扫描mysql,获取pda扫码枪 扫描的数据
  8. Cypress使用npm启用时报Error: Cannot find module ‘minimist‘
  9. 在jupytor中运行随机森林预测泰坦尼克旅客生存情况
  10. 空调制冷站设计的设计方法和流程丨行有嘉