LightOJ 1070 Algebraic Problem (推导+矩阵高速幂)
题目链接: 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 (推导+矩阵高速幂)的更多相关文章
- LightOJ 1070 - Algebraic Problem 推导+矩阵快速幂
http://www.lightoj.com/volume_showproblem.php?problem=1070 思路:\({(a+b)}^n =(a+b){(a+b)}^{n-1} \) \(( ...
- LightOJ 1070 Algebraic Problem:矩阵快速幂 + 数学推导
题目链接:http://lightoj.com/volume_showproblem.php?problem=1070 题意: 给你a+b和ab的值,给定一个n,让你求a^n + b^n的值(MOD ...
- LightOJ 1070 - Algebraic Problem 矩阵高速幂
题链:http://lightoj.com/volume_showproblem.php?problem=1070 1070 - Algebraic Problem PDF (English) Sta ...
- HDU 1757 A Simple Math Problem(矩阵高速幂)
题目地址:HDU 1757 最终会构造矩阵了.事实上也不难,仅仅怪自己笨..= =! f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 ...
- hdu 1757 A Simple Math Problem (矩阵高速幂)
和这一题构造的矩阵的方法同样. 须要注意的是.题目中a0~a9 与矩阵相乘的顺序. #include <iostream> #include <cstdio> #include ...
- LightOJ - 1132 Summing up Powers 矩阵高速幂
题目大意:求(1^K + 2^K + 3K + - + N^K) % 2^32 解题思路: 借用别人的图 能够先打表,求出Cnm,用杨辉三角能够高速得到 #include<cstdio> ...
- 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 ...
- [POJ 3150] Cellular Automaton (矩阵高速幂 + 矩阵乘法优化)
Cellular Automaton Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 3048 Accepted: 12 ...
- HDOJ 4686 Arc of Dream 矩阵高速幂
矩阵高速幂: 依据关系够建矩阵 , 高速幂解决. Arc of Dream Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65535/ ...
随机推荐
- 7. 关于IntelliJ IDEA删除项目
转自:https://www.cnblogs.com/zhangqian27/p/7698148.html 刚开始使用IDEA . 自己创建项目玩,结果发现IDEA无法删除,我也是醉了,Eclipse ...
- RecordAccumulator 1
介绍 前面讲过producer会将数据保存在RecordAccumulator中,并通过Sender发送数据.RecordAccumulator 就相当于一个队列保存着那些准备发送到server的数据 ...
- 验证备份集-使用DBVERIFY工具
DBVERIFY确认备份集是否存在坏块 验证TEST03.DBF 文件的是否存在坏块 C:\Documents and Settings\Administrator>dbv file=D:\or ...
- js中字符串转驼峰转为下划线
function dasherize(str) { return str.replace(/::/g, '/') .replace(/([A-Z]+)([A-Z][a-z])/g, '$1_$2') ...
- python(2) 图像通道,几何变换,裁剪
一.图像通道 1.彩色图像转灰度图 from PIL import Image import matplotlib.pyplot as plt img=Image.open('d:/ex.jpg') ...
- shell清除日志小脚本
#!/bin/bash #清除日志脚本 LOG_DIR=/var/log ROOT_UID=0 #用户id为0的 ,即为root if [ "$UID" -ne "$RO ...
- Maven学习总结(21)——Maven常用的几个核心概念
在使用Maven的过程中,经常会遇到几个核心的概念,准确的理解这些概念将会有莫大的帮助. 1. POM(Project Object Model)项目对象模型 POM 与 Java 代码实现了解耦,当 ...
- Java编程手冊-Collection框架(下)
建议先看Java编程手冊-Collection框架(上) 5. Set<E>接口与实现 Set<E>接口表示一个数学的集合,它不同意元素的反复,仅仅能包括一个null元素. ...
- Light oj 1138 - Trailing Zeroes (III) 【二分查找好题】【 给出N!末尾有连续的Q个0,让你求最小的N】
1138 - Trailing Zeroes (III) PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 ...
- python学习之--SyntaxError: Non-ASCII character '\xe5'
在安装好eclipse之后试了一下 创建了了一个pydev project package.module 在test.py中编写最简单的命令 print "helloworld" ...