https://vjudge.net/problem/UVALive-3704

参考:http://www.cnblogs.com/iwtwiioi/p/3946211.html

循环矩阵。。。

我们发现,这道题n^3logk过不了 那么就要用循环矩阵

矩阵乘法:a[i][j]=b[i][k]*c[k][j]

列向量的复杂度是O(n^2),因为只有一列,每次列向量和系数矩阵里的值对应乘起来就可以了,每次O(n),做n次。

但是系数矩阵自乘的复杂度就是O(n^3),也就意味着我们要优化系数矩阵的自乘。

我们发现,系数矩阵是一个循环矩阵。。。

也就是说系数矩阵的每一行都是上一行平移一位。

那么也就是说我们只用求第一行就可以了。

这里说明一下我们只优化了系数矩阵的自乘,没有优化快速幂中求答案的那一部分。

其实,循环矩阵的行和列是对应相等的,第一行=第一列,第二行=第二列,也就是说自乘的时候第二个元素=第一行*第二列=第一行*第二行,那么只用保存第一行就行了

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = ;
struct mat {
ll a[N];
} x, a, b;
int n, m, d, k;
mat operator * (mat A, mat B)
{
mat ret; memset(ret.a, , sizeof(ret.a));
for(int i = ; i < n; ++i)
for(int j = ; j < n; ++j)
if(i - j >= ) ret.a[i] = (ret.a[i] + A.a[j] * B.a[i - j]) % m;
else ret.a[i] = (ret.a[i] + A.a[j] * B.a[i - j + n]) % m;
return ret;
}
int main()
{
while(scanf("%d%d%d%d", &n, &m, &d, &k) != EOF)
{
memset(a.a, , sizeof(a.a));
memset(b.a, , sizeof(b.a));
for(int i = ; i < n; ++i) scanf("%lld", &x.a[i]);
for(int i = ; i <= d; ++i) a.a[i] = ;
for(int i = n - ; i >= n - d; --i) a.a[i] = ;
b.a[] = ;
for(; k; a = a * a, k >>= ) if(k & ) b = b * a;
x = b * x;
for(int i = ; i < n - ; ++i) printf("%lld ", x.a[i] % m);
printf("%lld\n", x.a[n - ] % m);
}
return ;
}

LA3704的更多相关文章

随机推荐

  1. 竞赛Noi_Linux使用总结(vim)

    刚换完Linux,趁着教练给的改题时间(T2确实猛)自己上网找了好多博客,发现很多跟竞赛有关的内容是碎片化的,从最基本的如何用vim写代码.编译.运行,再到怎么改设置使打代码时手感强一些,最后学对拍, ...

  2. 谷歌浏览器添加Bing搜索引擎:

    谷歌浏览器添加Bing搜索引擎:   https://www.bing.com/search?q=%s&pc=MOZI&form=MOZLBR  

  3. java读utf8 的txt文件,第一个字符为空或问号问题

    参考:https://blog.csdn.net/yangzhichao888/article/details/79529756 https://blog.csdn.net/wangzhi291/ar ...

  4. HDU-1022Train Problem I,简单栈模拟;

    Train Problem I                                                                                     ...

  5. 转盘抽奖 canvas & 抽奖 H5 源码

    转盘抽奖 canvas https://github.com/givebest/wechat-turntalbe-canvas https://blog.givebest.cn/GB-canvas-t ...

  6. codeforces 361A

    //这题看着吓人,为何这么水 #include<stdio.h> int main() {  int n,m,i,j;  while(scanf("%d%d",& ...

  7. 洛谷P1186 玛丽卡

    题目描述 麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复. 因为她和他们不住在同一个城市,因此她开始准备她的长途旅行. 在这个国家中每两个城市之间最多只有一条路相通,并且我们知道从一个城市到另一个城 ...

  8. 【NOIP2017练习】溢出(模拟)

    题意: 思路: a*b<=c <====> b<=c div a var ch,maxs,s:ansistring; v,k,i,maxl,l,len,cnt,cas:long ...

  9. 安装最新版本的zabbix

    1. 先安装php5.4 最新版本: yum安装php5.4或5.5 https://blog.csdn.net/MarkBoo/article/details/49424183 2. 然后参照官网或 ...

  10. Apache 使用localhost(127.0.0.1)可以访问,使用本机IP(局域网)不能访问

    本机ip是:192.168.1.25,输入后提示: Forbidden You don't have permission to access / on this server 对于此问题的解决办法, ...