Matrix_QP(A_2SeqSum)
题目大意:给出an,bn的递推,求ai*bi(i=0,1,……n-1)的和(an=a(n-1)*Ax+Ay, bn=b(n-1)*Bx+By, a0=A0, b0=B0, Ax,Bx,Ay,By已知)。
题解:矩阵快速幂不用说了,式子也好推,开了ll基本没有坑点,奇葩的是这题是做连通分量做到的,有丶意思。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N = 6;
const int MOD = 1e9 + 7;
ll A0, B0, Ax, Bx, Ay, By;
ll p, q, r, s;
struct M
{
ll mat[N];
M(){}
M( int _ )
{
mat[1] = A0*B0%MOD;
mat[2] = A0; mat[3] = B0;
mat[4] = 1;
mat[5] = A0*B0%MOD;
}
};
struct _M
{
ll mat[N][N];
_M(){}
_M( int _ )
{
mat[1][1] = p; mat[1][2] = q; mat[1][3] = r; mat[1][4] = s; mat[1][5] = 0;
mat[2][1] = mat[2][3] = mat[2][5] = 0; mat[2][2] = Ax%MOD; mat[2][4] = Ay%MOD;
mat[3][1] = mat[3][2] = mat[3][5] = 0; mat[3][3] = Bx%MOD; mat[3][4] = By%MOD;
mat[4][1] = mat[4][2] = mat[4][3] = mat[4][5] = 0; mat[4][4] = 1;
mat[5][1] = p; mat[5][2] = q; mat[5][3] = r; mat[5][4] = s; mat[5][5] = 1;
}
};
void init()
{
p = Ax*Bx%MOD;
q = Ax*By%MOD;
r = Bx*Ay%MOD;
s = Ay*By%MOD;
}
M M12( M a, _M b )
{
M c;
memset(c.mat,0,sizeof(c.mat));
for ( int i = 1; i < N; i ++ )
{
ll cur = 0;
for ( int j = 1; j < N; j ++ )
cur += (a.mat[j]%MOD*b.mat[i][j]%MOD)%MOD;
c.mat[i] = cur % MOD;
}
return c;
}
_M M22( _M a, _M b )
{
_M c;
memset(c.mat,0,sizeof(c.mat));
for ( int i = 1; i < N; i ++ )
{
for ( int j = 1; j < N; j ++ )
{
ll cur = 0;
for ( int k = 1; k < N; k ++ )
cur += (a.mat[i][k]%MOD*b.mat[k][j]%MOD)%MOD;
c.mat[i][j] = cur % MOD;
}
}
return c;
}
M qp( M a, _M b, ll c )
{
while ( c )
{
if ( c & 1 ) a = M12( a, b );
c >>= 1;
b = M22(b,b);
}
return a;
}
int main()
{
ll m;
while ( ~scanf("%lld", &m) )
{
scanf("%lld%lld%lld", &A0, &Ax, &Ay);
scanf("%lld%lld%lld", &B0, &Bx, &By);
if ( !m ) puts("0");
else if ( m == 1 )
{
ll now = A0%MOD*B0%MOD%MOD;
printf("%lld\n", now);
}
else
{
init();
M ans(1);
_M base(1);
/* for ( int i = 1; i < N; i ++)
cout << ans.mat[i] << endl;
for ( int i = 1; i < N; i ++ )
{
for ( int j = 1; j < N; j ++ )
cout << base.mat[i][j] << " ";
cout << endl;
}
*/
ans = qp( ans, base, m-1);
printf("%lld\n", (ans.mat[5]%MOD+MOD)%MOD);
}
}
return 0;
}
Matrix_QP(A_2SeqSum)的更多相关文章
随机推荐
- UIScreen(屏幕)、UIWindow(画框)、UIView(画布)、didFinishLaunchingWithOptions的概念
//didFinishLaunchingWithOptions 方法:顾名思义.在app开始运行时会调用里面的方法.- (BOOL)application:(UIApplication *)appli ...
- 1044: Access denied for user 'hehe'@'localhost' to database 'imooc'
当我使用 mysql授予用户时, GRANT ALL PRIVILEGES ON *.* TO hehe IDENTIFIED BY 'some' WITH GRANT OPTION; 出现:1044 ...
- 如何使用 libtorch 实现 VGG16 网络?
参考地址:https://ethereon.github.io/netscope/#/preset/vgg-16 按照上面的图来写即可. 论文地址:https://arxiv.org/pdf/1409 ...
- 16.Update Methods-官方文档摘录
这里没什么好说的,直接贴文了 MongoDB provides the following methods for updating documents in a collection: db.col ...
- NoSQL 数据库概览及其与 SQL 语法的比较
NoSQL数据库的产生就是为了解决大规模数据集合多重数据种类带来的挑战,尤其是大数据应用的难题. 本文对NoSQL数据库的定义.分类.特征.当前比较流行的NoSQL数据库系统等进行了简单的介绍,并对N ...
- 使用selenium找出外卖点餐次数最多的10个顾客
大锅在做外卖,给我说能否统计出这半年点餐次数最多的10个顾客,我不知道APP本身是否有这个功能,想了下最近用selenium较多,就用selenium尝试下吧. 1 定义一个类,这里先描述需要的属性和 ...
- 1.新建项目出现包名有一道红线The SDK platform-tools version ((23)) is too old to check APIs compiled with API 20
原因分析: 就是platform-tools的版本太低导致的 解决方法: 1.点开SDK Manager,打开SDK Tools面板,将Platform-tools更新 2.更新完之后重启as即可
- SIP UserAgent (B2BUA client)——linphonec
1.linphone编译 linphone一般用在android/ios/windows/mobile上,但是没有图形界面的linphonec命令行程序用在资源紧张的硬件平台上也跟pjsip命令行一样 ...
- Spring第五弹—–配置Spring管理的bean的作用域和生命周期
singleton (默认方式) 在每个Spring IoC容器中一个bean定义只有一个对象实例.默认情况下会在容器启动时初始化bean,但我们可以指定bean节点的lazy-init=“true” ...
- EventFiringWebDriver网页事件监听(二)
public class SeleniumDemo { /** * @param args */ public static void main(String[] args) { WebDriver ...