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/ ...
随机推荐
- BZOJ 1379 模拟退火
模拟退火的第一题~ //By SiriusRen #include <cmath> #include <cstdio> #include <algorithm> u ...
- ajax无刷新翻页后,jquery失效问题的解决
例如 $(".entry-title a").click(function () { 只对第一页有效, 修改为 $(document).on('click', ".e ...
- <Sicily>Brackets Matching
一.题目描述 Let us define a regular brackets sequence in the following way: Empty sequence is a regular s ...
- BZOJ 3626 LCA(离线+树链剖分+差分)
显然,暴力求解的复杂度是无法承受的. 考虑这样的一种暴力,我们把 z 到根上的点全部打标记,对于 l 到 r 之间的点,向上搜索到第一个有标记的点求出它的深度统计答案.观察到,深度其实就是上面有几个已 ...
- luoguP1401 城市(二分答案+最大流)
题意 N(2<=n<=200)个城市,M(1<=m<=40000)条无向边,你要找T(1<=T<=200)条从城市1到城市N的路,使得最长的边的长度最小,边不能重复 ...
- PHP取不定个数数组交集
最近有个需求,有一个N个二维数组,N是动态的,不固定个数,现需取这N个数组的交集内容. 用到的函数是array_intersect_assoc 用法 $result_arr = array_inter ...
- 【BZOJ 1045】 [HAOI2008] 糖果传递
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 思路来自hzwer.. 设xi表示第i个人往左传递了xi个糖果. (如果小于0表示旁边的人给他了糖果. 则ans=∑|xi| 最后所 ...
- 洛谷——P1965 转圈游戏
https://www.luogu.org/problem/show?pid=1965 题目描述 n 个小伙伴(编号从 0 到 n-1)围坐一圈玩游戏.按照顺时针方向给 n 个位置编号,从0 到 n- ...
- 洛谷 P1443 马的遍历
P1443 马的遍历 题目描述 有一个n*m的棋盘(1<n,m<=400),在某个点上有一个马,要求你计算出马到达棋盘上任意一个点最少要走几步 输入输出格式 输入格式: 一行四个数据,棋盘 ...
- 怎样给UINavigationBar加入button?
Mads Mobæk:给UINavigationBar加入button的演示样例代码 1 2 3 4 5 6 7 8 UIBarButtonItem *rightButton = [[UIBarBut ...