题目链接:

problem=1070">LightOJ 1070 Algebraic Problem

题意:已知a+b和ab的值求a^n+b^n。结果模2^64。

思路:

1.找递推式

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

得到递推式之后就是矩阵高速幂了

注意:模2^64,定义成unsigned long long 类型,由于无符号类型超过最大范围的数与该数%最大范围 的效果是一样的。

AC代码:

#include<stdio.h>
#include<string.h>
#define LL unsigned long long struct Matrix {
LL m[10][10];
};
LL n;
Matrix geti(LL n) {
LL i;
Matrix b;
memset(b.m,0,sizeof b.m);
for(i=0;i<2;i++)
b.m[i][i]=1;
return b;
} Matrix matrixmul(Matrix a,Matrix b) {
LL i,j,k;
Matrix c;
for(i=0;i<2;i++) {
for(j=0;j<2;j++) {
c.m[i][j]=0.0;
for(k=0;k<2;k++) {
c.m[i][j]+=(a.m[i][k]*b.m[k][j]);
}
}
}
return c;
} Matrix quickpow(Matrix a,LL p) {
Matrix m=a;
Matrix b=geti(n);
while(p) {
if(p%2)
b=matrixmul(b,m);
p/=2;
m=matrixmul(m,m);
}
return b;
} int main() {
LL t;
LL p,q;
int cas=1;
Matrix pp,init,ans;
//printf("%llu\n",kmod);
scanf("%llu",&t);
while(t--) {
scanf("%llu %llu %llu",&p,&q,&n);
memset(pp.m,0,sizeof pp);
pp.m[0][0]=p;
pp.m[0][1]=1;
pp.m[1][0]=-q;
pp.m[1][1]=0; init.m[0][0]=p;
init.m[0][1]=2;
init.m[1][0]=0;
init.m[1][1]=0; printf("Case %d: ",cas++);
if(n==0){
printf("%llu\n",init.m[0][1]);
}
else if(n==1){
printf("%llu\n",init.m[0][0]);
}
else {
ans=quickpow(pp,n-1);
ans=matrixmul(init,ans);
printf("%llu\n",ans.m[0][0]);
}
}
return 0;
}
/* */

LightOJ 1070 Algebraic Problem (推导+矩阵高速幂)的更多相关文章

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

    http://www.lightoj.com/volume_showproblem.php?problem=1070 思路:\({(a+b)}^n =(a+b){(a+b)}^{n-1} \) \(( ...

  2. LightOJ 1070 Algebraic Problem:矩阵快速幂 + 数学推导

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1070 题意: 给你a+b和ab的值,给定一个n,让你求a^n + b^n的值(MOD ...

  3. LightOJ 1070 - Algebraic Problem 矩阵高速幂

    题链:http://lightoj.com/volume_showproblem.php?problem=1070 1070 - Algebraic Problem PDF (English) Sta ...

  4. HDU 1757 A Simple Math Problem(矩阵高速幂)

    题目地址:HDU 1757 最终会构造矩阵了.事实上也不难,仅仅怪自己笨..= =! f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 ...

  5. hdu 1757 A Simple Math Problem (矩阵高速幂)

    和这一题构造的矩阵的方法同样. 须要注意的是.题目中a0~a9 与矩阵相乘的顺序. #include <iostream> #include <cstdio> #include ...

  6. LightOJ - 1132 Summing up Powers 矩阵高速幂

    题目大意:求(1^K + 2^K + 3K + - + N^K) % 2^32 解题思路: 借用别人的图 能够先打表,求出Cnm,用杨辉三角能够高速得到 #include<cstdio> ...

  7. HDU 2256 Problem of Precision(矩阵高速幂)

    题目地址:HDU 2256 思路: (sqrt(2)+sqrt(3))^2*n=(5+2*sqrt(6))^n; 这时要注意到(5+2*sqrt(6))^n总能够表示成an+bn*sqrt(6); a ...

  8. [POJ 3150] Cellular Automaton (矩阵高速幂 + 矩阵乘法优化)

    Cellular Automaton Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 3048   Accepted: 12 ...

  9. HDOJ 4686 Arc of Dream 矩阵高速幂

    矩阵高速幂: 依据关系够建矩阵 , 高速幂解决. Arc of Dream Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/ ...

随机推荐

  1. 图像几何变换(geometric transformation)

    1. imwarp B = imwarp(A,tform) demo I = imread('cameraman.tif'); tform = affine2d([1 0 0; .5 1 0; 0 0 ...

  2. TurtleWorld Exercises

    1. Write a function called square that takes a parameter named t, which is a turtle. It should use t ...

  3. 搭建Hadoop的全分布模式

    此教程仅供参考 注意:此文档目的是为了本人方便以后复习,不适合当教程,以免误导萌新... 1.安装三台Linux2.在每台机器上安装JDK3.配置每台机器的免密码登录 (*) 生成每台机器的公钥和私钥 ...

  4. Android属性动画-基本用法

    在手机上去实现一些动画效果算是件比较炫酷的事情,因此Android系统在一开始的时候就给我们提供了两种实现动画效果的方式,逐帧动画(frame-by-frame animation)和补间动画(twe ...

  5. css hover图片hover效果兼容ie8

    例子: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...

  6. UI Framework-1: views Windowing

    views Windowing views provides support for creating dialog boxes and other kinds of windows through ...

  7. The IDL compiler

    The IDL compiler or bindings generator transcompiles Web IDL to C++ code, specifically bindings betw ...

  8. bzoj 1088 [SCOI2005] 扫雷

    SCOI2005 扫雷 一道很有趣的(水)题 “这道题有四种解法,你知道么” 给你矩阵的第二列的数字,求出第一列雷有多少种可能的摆法. 不懂扫雷规则的自行按win+R然后输入winmine 思考过后我 ...

  9. 【转】 HTML解析:基于XPath的C#类库HtmlAgiliytyPack

    [转] HTML解析:基于XPath的C#类库HtmlAgiliytyPack 最近处于毕业设计开始阶段,前期工作需要去国外的一些专业数据库网站比对一些所需TF家族信息,为了快捷方便,想到用程序去帮助 ...

  10. 坑爹的RockSaw和坑爹的windows7

    坑爹的RockSaw和坑爹的windows7 http://chen4w.iteye.com/blog/1153433