就是优化一段代码,用矩阵快速幂(刚开始想到了转移矩阵以为是错的)

在搜题解时发现了一个神奇的网站:http://oeis.org/

用来找数列规律 的神器。。。。

规律就是an=an-1+2*an-2+1

然后构造矩阵就行了

还有特例1的时候记得%m

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 10000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=<<+,inf=0x3f3f3f3f; struct Node{
ll row,col;
ll a[N][N];
};
ll m;
Node mul(Node x,Node y)
{
Node ans;
ans.row=x.row,ans.col=y.col;
memset(ans.a,,sizeof ans.a);
for(ll i=;i<x.row;i++)
for(ll j=;j<x.col;j++)
for(ll k=;k<y.col;k++)
ans.a[i][k]=(ans.a[i][k]+x.a[i][j]*y.a[j][k]+m)%m;
return ans;
}
Node quick_mul(Node x,ll n)
{
Node ans;
ans.row=x.row,ans.col=x.col;
memset(ans.a,,sizeof ans.a);
for(ll i=;i<ans.col;i++)ans.a[i][i]=;
while(n){
if(n&)ans=mul(ans,x);
x=mul(x,x);
n/=;
}
return ans;
}
int main()
{ ios::sync_with_stdio(false);
cin.tie();
// cout<<setiosflags(ios::fixed)<<setprecision(2);
ll n;
while(cin>>n>>m){
if(n==)
{
cout<<%m<<endl;
continue;
}
Node A;
A.row=,A.col=;
memset(A.a,,sizeof A.a);
A.a[][]=,A.a[][]=,A.a[][]=;
A.a[][]=,A.a[][]=,A.a[][]=;
A.a[][]=,A.a[][]=,A.a[][]=;
/* for(int i=0;i<A.row;i++)
{
for(int j=0;j<A.col;j++)
cout<<A.a[i][j]<<" ";
cout<<endl;
}*/
Node B;
B.row=,B.col=;
B.a[][]=,B.a[][]=,B.a[][]=;
B=mul(quick_mul(A,n-),B);
cout<<B.a[][]<<endl;
}
return ;
}

hdu4990矩阵快速幂的更多相关文章

  1. hdu4990 矩阵快速幂

    题意:       给你一短代码,让你优化这个代码,代码如下 #pragma comment(linker, "/STACK:1024000000,1024000000") #in ...

  2. hdu4990 Reading comprehension 矩阵快速幂

    Read the program below carefully then answer the question.#pragma comment(linker, "/STACK:10240 ...

  3. 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)

    题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...

  4. 51nod 算法马拉松18 B 非010串 矩阵快速幂

    非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...

  5. 51nod 1113 矩阵快速幂

    题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...

  6. 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】

    还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...

  7. HDU5950(矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 题意:f(n) = f(n-1) + 2*f(n-2) + n^4,f(1) = a , f(2 ...

  8. 51nod 1126 矩阵快速幂 水

    有一个序列是这样定义的:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. 给出A,B和N,求f(n)的值. Input 输 ...

  9. hdu2604(递推,矩阵快速幂)

    题目链接:hdu2604 这题重要的递推公式,找到公式就很easy了(这道题和hdu1757(题解)类似,只是这道题需要自己推公式) 可以直接找规律,推出递推公式,也有另一种找递推公式的方法:(PS: ...

随机推荐

  1. Use of ‘const’ in Functions Return Values

    Use of 'Const' in Function Return Values 为什么要在函数的返回值类型中添加Const? 1.Features Of the possible combinati ...

  2. Spark-RDD算子

    一.Spark-RDD算子简介 RDD(Resilient Distributed DataSet)是分布式数据集.RDD是Spark最基本的数据的抽象. scala中的集合.RDD相当于一个不可变. ...

  3. (windows下)tomcat优化--内存,并发.缓存三方面优化

    一.内存 注: jdk1.8中废弃了-XX:PermSize和-XX:MaxPermSize的持久态(存放常量.静态变量区)配置, 多了一个元数据区(Metadata Space:默认为内存的0.2% ...

  4. iptables 执行清除命令 iptables -F 要非常小心的

    iptables 执行清除命令 iptables -F 要非常小心的 http://blog.csdn.net/netingcn/article/details/5692972 使用 /sbin/ip ...

  5. Openstack(六)RabbitMQ集群

    各组件通过消息发送与接收是实现组件之间的通信: 6.1安装RabbitMQ 6.1.1安装RabbitMQ # yum install rabbitmq-server –y # systemctl s ...

  6. thinkphp5手动注册命名空间

    手动注册 命名空间.利用application/config.php配置文件来注册命名空间1:在application目录同级创建一个myExtend文件夹,里面再创建一个myTest文件夹.里面放一 ...

  7. maven解决“Could not calculate build plan”问题

    错误提示如下:(eclipse+maven) Could not calculate build plan: Failure to transfer org.apache.maven.plugins: ...

  8. 小型网站MYSQL问题二:Percona Xtrabackup实现数据库备份和恢复

    1.安装软件仓库(不要问我为什么不用源码安装,好吧,其实我懒.) 1 2 3 4 5 6 7 8 wget https://www.percona.com/downloads/percona-rele ...

  9. (1)了解cocostudio基础

    操作界面   Cocos Studio的界面主要分为菜单栏.工具栏.对象面板.资源面板.画布面板.属性面板.动画面板.输出窗口.状态栏九部分组成,如下图: 菜单栏   菜单栏为Cocos Studio ...

  10. js动态创建Form表单并提交

    javascript动态创建Form表单和表单项,然后提交表单请求,最后删除表单,代码片段如下(Firefox测试通过): var dlform = document.createElement('f ...