【矩阵快速幂】LightOJ_1070_Algebraic Problem
来源:互联网 发布:ubuntu win7双系统 编辑:程序博客网 时间:2024/06/04 18:29
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 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 qdenotes 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.
For each test case, print the case number and (an+bn) modulo 264.
2
10 16 2
7 12 3
Case 1: 68
Case 2: 91
/*unsigned long long 自动对2^64取模*/#include <bits/stdc++.h>using namespace std;typedef unsigned long long ULL;const int maxn=2;typedef struct node{ ULL a[maxn][maxn]; node(){ memset(a,0,sizeof(a)); }}Matrix;Matrix multiply(Matrix x,Matrix y){ Matrix ans; for(int i=0;i<maxn;i++){ for(int j=0;j<maxn;j++){ for(int k=0;k<maxn;k++){ ans.a[i][j]+=x.a[i][k]*y.a[k][j]; } } } return ans;}Matrix quickpow(Matrix x,int a){ Matrix ans; for(int i=0;i<maxn;i++) ans.a[i][i]=1; while(a){ if(a&1) ans=multiply(ans,x); x=multiply(x,x); a>>=1; } return ans;}int main(){ int t,n; ULL p,q; scanf("%d",&t); for(int cas=1;cas<=t;cas++){ scanf("%llu%llu%d",&p,&q,&n); if(n<3){ if(n==0) printf("Case %d: %llu\n",cas,2); if(n==1) printf("Case %d: %llu\n",cas,p); if(n==2) printf("Case %d: %llu\n",cas,p*p-2*q); continue; } Matrix base,x; x.a[0][0]=p;x.a[0][1]=1;x.a[1][0]=-q;x.a[1][1]=0; base.a[0][0]=p*p-2*q;base.a[0][1]=p; Matrix ans=multiply(base,quickpow(x,n-2)); printf("Case %d: %llu\n",cas,ans.a[0][0]); }}
阅读全文
0 0
- 【矩阵快速幂】LightOJ_1070_Algebraic Problem
- FZU - 1692 Key problem 矩阵快速幂
- LightOJ 1070 - Algebraic Problem 矩阵快速幂
- hdu4291A Short problem 矩阵快速幂
- Problem of Precision(矩阵快速幂)
- [矩阵快速幂]hdu2256 Problem of Precision
- poj - problem 3070 Fibonacci 【矩阵 +快速幂】
- HDU - problem Tr A 【快速幂 + 矩阵】
- A simple math problem 【矩阵快速幂】
- HDU - problem 1757 A Simple Math Problem【矩阵 + 快速幂】
- FZU 1692 Key problem(构造矩阵+矩阵快速幂)
- A Simple Math Problem (矩阵快速幂、构造矩阵)
- hdu - 1757 - A Simple Math Problem(矩阵快速幂)
- uva 10655 Algebraic Problem(矩阵快速幂)
- hdu 1757 A Simple Math Problem(矩阵快速幂)
- HDU 3483 A Very Simple Problem ---矩阵快速幂
- hdu 1757 A Simple Math Problem (矩阵快速幂)
- 【HDU】4291 A Short problem 矩阵快速幂
- android双击事件
- 近5年133个Java面试问题列表
- shell 脚本字符串操作
- struts2工作流程
- [HDU 1160 ] FatMouse's Speed [LIS 原理+ 路径输出]
- 【矩阵快速幂】LightOJ_1070_Algebraic Problem
- hibernate的五大核心(类/接口)简述
- 解决macbook pro开启parallels desktop虚拟机之后切换为独立显卡温度高的问题
- 观察者模式
- 微信小程序代理
- js系列教程6-BOM操作全解
- 文件和目录操作命令 find mkdir mv
- Java重写和重载
- HDU