HDU - 4686 函数积的前缀和
题意:求\(\sum_{i=0}^{n-1}a_ib_i\)
其中,\(a_i=A_xa_{i-1}+A_y,b_i=B_xb_{i-1}+B_y\)
构造矩阵分别维护\(a_ib_i,a_i,b_i,A_yB_y,A_y,B_y,S_i\)
a_ib_i \\ a_i \\ bi \\ A_yB_y \\ A_y \\ B_y \\ S_i\\
\end{bmatrix} = \begin{bmatrix}
A_xB_x & A_xB_y & A_yB_x & 1 & 0 & 0 & 0 \\
0 & A_x & 0 & 0 & 1 & 0 & 0 \\
0 & 0 & B_x & 0 & 0 & 1 & 0 \\
0 & 0 & 0 & 1 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 1 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 1 & 0 \\
1 & 0 & 0 & 0 & 0 & 0 & 1 \\
\end{bmatrix} \begin{bmatrix}
a_{i-1}b_{i-1} \\ a_{i-1} \\ b_{i-1} \\ A_yB_y \\ A_y \\ B_y \\ S_{i-1}\\
\end{bmatrix}
\]
那么\(S_n\)即为所求
#include<bits/stdc++.h>
#define rep(i,j,k) for(register int i=j;i<=k;i++)
#define println(a) printf("%lld\n",(ll)a)
using namespace std;
typedef long long ll;
const ll mod = 1e9+7;
ll read(){
ll x=0,f=1;register char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
struct Matrix{
ll mt[9][9],r,c;
void init(int rr,int cc,bool flag=0){
r=rr;c=cc;
memset(mt,0,sizeof mt);
if(flag) rep(i,1,r) mt[i][i]=1;
}
Matrix operator * (Matrix rhs){
Matrix ans; ans.init(r,rhs.c);
rep(i,1,r){
rep(j,1,rhs.c){
int t=max(r,rhs.c);
rep(k,1,t){
ans.mt[i][j]+=(mt[i][k]*rhs.mt[k][j])%mod;
ans.mt[i][j]=(ans.mt[i][j])%mod;
}
}
}
return ans;
}
};
Matrix fpw(Matrix A,ll n){
Matrix ans;ans.init(A.r,A.c,1);
while(n){
if(n&1) ans=ans*A;
n>>=1;
A=A*A;
}
return ans;
}
ll a0,ax,ay,b0,bx,by;
ll n;
ll bas2[8],base[8][8];
int main(){
while(cin>>n){
cin>>a0>>ax>>ay;
cin>>b0>>bx>>by;
if(n==0){
println(0);
continue;
}
bas2[1]=(a0%mod)*(b0%mod)%mod;bas2[2]=a0%mod;bas2[3]=b0%mod;bas2[4]=(ay%mod)*(by%mod)%mod;
bas2[5]=ay%mod;bas2[6]=by%mod; bas2[7]=0;
memset(base,0,sizeof base);
base[1][1]=(ax%mod)*(bx%mod)%mod;base[1][2]=(ax%mod)*(by%mod)%mod;base[1][3]=(ay%mod)*(bx%mod)%mod;base[1][4]=1;
base[2][2]=ax%mod;base[2][5]=1;
base[3][3]=bx%mod;base[3][6]=1;
base[4][4]=1;
base[5][5]=1;
base[6][6]=1;
base[7][1]=1;base[7][7]=1;
Matrix A; A.init(7,7);
rep(i,1,7)rep(j,1,7) A.mt[i][j]=base[i][j];
Matrix b; b.init(7,1);
rep(i,1,7) b.mt[i][1]=bas2[i];
Matrix res=fpw(A,n)*b;
println(res.mt[7][1]);
}
return 0;
}
HDU - 4686 函数积的前缀和的更多相关文章
- Master of Phi (欧拉函数 + 积性函数的性质 + 狄利克雷卷积)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6265 题目大意:首先T是测试组数,n代表当前这个数的因子的种类,然后接下来的p和q,代表当前这个数的因 ...
- HDU 4686 Arc of Dream(矩阵)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4686 题意: 思路: #include <iostream>#include <cs ...
- hdu 4746 Mophues 莫比乌斯反演+前缀和优化
Mophues 题意:给出n, m, p,求有多少对a, b满足gcd(a, b)的素因子个数<=p,(其中1<=a<=n, 1<=b<=m) 有Q组数据:(n, m, ...
- Hdu 4311-Meeting point-1 曼哈顿距离,前缀和
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4311 Meeting point-1 Time Limit: 2000/1000 MS (Java/Oth ...
- HDU 6186 CS Course(前缀+后缀)
http://acm.hdu.edu.cn/showproblem.php?pid=6186 题意:给出n个数,共有n次询问,每次询问给出一个数p,求除去第p个数后的n-1个数的&.|.^值. ...
- hdu 4686 Arc of Dream(矩阵快速幂)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4686 题意: 其中a0 = A0ai = ai-1*AX+AYb0 = B0bi = bi-1*BX+BY ...
- HDU 4686 Arc of Dream 矩阵快速幂,线性同余 难度:1
http://acm.hdu.edu.cn/showproblem.php?pid=4686 当看到n为小于64位整数的数字时,就应该有个感觉,acm范畴内这应该是道矩阵快速幂 Ai,Bi的递推式题目 ...
- 2016 ACM/ICPC亚洲区大连站 F - Detachment 【维护前缀积、前缀和、二分搜索优化】
F - Detachment In a highly developed alien society, the habitats are almost infinite dimensional spa ...
- LightOJ-1007-Mathematically Hard-欧拉函数打表+前缀和+预处理
Mathematically some problems look hard. But with the help of the computer, some problems can be easi ...
随机推荐
- 自制模仿apache访问日志文件格式的php日志类
<?php // 访问日志写入类 @author 王伟 2011.12.14class Log{ //项目跟路径 private $root_path; //日 ...
- solr开发 小案例
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" ...
- 复习action委托
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- HttpRuntime.Cache
a.在Web开发中,我们经常能够使用到缓存对象(Cache),在ASP.NET中提供了两种缓存对象,HttpContext.Current.Cache和HttpRuntime.Cache,那么他们有什 ...
- 第三周Linux编程实例练习
通过以下程序来练习 head.h # ifndef HEAD_H #define HEAD_H #include <stdio.h> int add(int,int); int sub(i ...
- Struts2获取Action中的数据
当我们用Struts2框架开发时,经常有要获取jsp页面的数据或者在jsp中获取后台传过来的数据(Action),那么怎么去获取自己想要的数据呢? 后台获取前端数据: 在java程序中生成要获取字段的 ...
- 编写高质量代码改善C#程序的157个建议——建议19:使用更有效的对象和集合初始化
建议19:使用更有效的对象和集合初始化 依赖于属性和FCL 3.5之后的语法规则,现在我们有了更加简洁有效的对象和集合初始化机制:对象和集合初始化设定项. 对象初始化: class Person { ...
- 激光样式——第九届蓝桥杯C语言B组(国赛)第二题
原创 标题:激光样式x星球的盛大节日为增加气氛,用30台机光器一字排开,向太空中打出光柱.安装调试的时候才发现,不知什么原因,相邻的两台激光器不能同时打开!国王很想知道,在目前这种bug存在的情况下, ...
- javascript ie8兼容 a标签href javascript:void(0);
ie8兼容 a标签href javascript:void(0); 尽量不要用javascript:协议做为A的href属性,这样不仅会导致不必要的触发window.onbeforeunload事件;
- EXCEL vlookup和small 综合运用
表数据如下: 如何通过EXCEL函数把 “谁拥有错误的代码” 的名称列出来,数组公式如下: =IFERROR(INDIRECT("A"& IFERROR(SMALL(IF( ...