UVA1386 快速矩阵幂 O(n²logk)
来源:互联网 发布:java异常中的finally 编辑:程序博客网 时间:2024/06/05 19:45
点击打开链接
友矩阵为循环矩阵,储存和计算矩阵乘法时只算出第一行
#pragma comment(linker, "/STACK:1024000000,1024000000")#include <stdio.h>#include <string.h>#include <iostream>#include <cctype>#include <string>#include <cmath>#include <algorithm>#include <queue>#include <set>#include <vector>#include <map>#define PR pair<int,int>#define MP make_pair#define fi first#define se second#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define sqr(x) ((x)*(x))#define ll long long#define ull unsigned __int64const ll INF = 1e18;const int inf=0x3f3f3f3f;const int M=444444;const int N=505;const int MOD=1000000007;const double eps=1e-10;const double pi=3.1415926536;using namespace std;int n,m,d,c;struct MAT{ ll mat[N]; friend MAT operator*(MAT &x,MAT &y); friend MAT operator^(MAT x,int y);};typedef struct MAT Mat;Mat operator*(Mat &x,Mat &y){ Mat ans; for(int j=0;j<n;j++) { ans.mat[j]=0; for(int k=j,cnt=0;cnt<n;k=(k+1)%n,cnt++) { ans.mat[j]=(ans.mat[j]+y.mat[cnt]%m*x.mat[k]%m)%m; } } return ans;}Mat operator^(Mat x,int y){ y--; Mat init=x; while(y) { if(y&1) init=init*x; x=x*x; y>>=1; } return init;}Mat a,b;int main(){ while(~scanf("%d%d%d%d",&n,&m,&d,&c)) { for(int i=0;i<n;i++) scanf("%lld",&a.mat[i]); memset(b.mat,0,sizeof(b.mat)); for(int i=0;i<d+1;i++) b.mat[i]=1; for(int i=n-1;i>=n-d;i--) b.mat[i]=1; b=b^c; for(int i=0;i<n;i++) { ll ans=0; for(int j=(n-i)%n,cnt=0;cnt<n;j=(j+1)%n,cnt++) ans+=a.mat[cnt]%m*b.mat[j]%m; printf("%lld",ans%m); if(i==n-1) puts(""); else printf(" "); } }}
0 0
- UVA1386 快速矩阵幂 O(n²logk)
- uva1386(矩阵快速幂)
- 矩阵快速幂+优化LA3704(uva1386)
- UVA1386-Cellular Automaton(循环矩阵)
- 找出N个数中最小的k个数问题(复杂度O(N*logk))
- 快速幂( O(log n) )
- uva1386 - Cellular Automaton 循环矩阵
- 矩阵快速幂(求斐波拉契数列的第N项)
- 矩阵快速二分求n次幂
- HLG 1905 f(N) 矩阵快速幂
- 求矩阵的n次方 快速幂
- n个数,找出其中最小的k个数,写出代码,要求最坏情况下的时间复杂度不能高于O(n logk)
- uva 10229 (fib矩阵形式+矩阵快速幂)uva 10518 (fib(n)调用多少次)
- 51nod 1113 矩阵快速幂 (矩阵的n次方)
- LOJ1070:Algebraic Problem(矩阵快速幂 & 数学 a^n+b^n)
- n行n列矩阵快速幂模板
- 最大子矩阵和(任意子阵,O(n^4)和O(n^3))
- 算法学习 - 快速幂和矩阵快速幂(复杂度Olog(n))C++实现
- #ifndef #define #endif的用法
- n a^o7 !
- 客户端多线程向服务端的文件传输
- java对象与json对象间的相互转换
- angularjs的使用:过滤器(5)
- UVA1386 快速矩阵幂 O(n²logk)
- C++模拟键盘按键
- zzulioj 1918 【二分最大匹配】
- Volatile关键字
- 文件IO操作
- Spark的这些事<三>——spark常用的Transformations 和Actions
- 关闭cassandra 或者 tomcat 的方式
- UVA10878 Decode the tape
- 恶意代码--windows脚本wscript恶意样本逆向分析