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 ...
随机推荐
- opennebula 对接创建模板参数
{ "id": 8, "name": "c5d1390c-1930-45a5-a686-5cef38b319d7", "displ ...
- 3-No resource found that matches the given name 'Theme.AppCompat.Light 的完美解决方案
转载:http://www.360doc.com/content/15/0316/15/9200790_455576135.shtml 由于我在配置安卓环境时也碰到了类似问题,用这篇博客解决了主要问题 ...
- Ubuntu16.04 ARM 编译 编译器版本和unordered_map map问题
源文件内使用unordered_map时候,例如如下demo #include <unordered_map> void foo(const std::unordered_map<i ...
- linux操作小技巧
巧妙利用别称 alias,让工作更有效率 在我的个人目录下/home/zdwu,打开.bashrc文件进行修改: 将 ll='ls -alF' 改为 ll='ls -ahlF',是的观察的结果显示更 ...
- (转)菜鸟去重复之Sql
原文地址:http://www.cnblogs.com/fatbird/p/Sql-Remove-duplicate.html 前言 本文主要是总结平时工作学习中遇到的使用Sql Server的去除重 ...
- linux下SVN服务器配置
SVN服务器配置 1. 安装svn服务 yum install subversion 2. 新建一个目录用于存储SVN所有文件 mkdir /home/svn 3. 创建项目 在上面创建的文件夹中为项 ...
- java多线程的基本介绍
Java多线程 1.进程与线程 进程是程序的一次动态执行过程,它需要经历从代码加载,代码执行到执行完毕的一个完整的过程,这个过程也是进程本身从产生,发展到最终消亡的过程.多进程操作系统能同时达运行多个 ...
- JavaScript对象(持续更新中)
1Array对象 2.Boolean对象 3.Date对象 4.Math对象 5.Number对象 6.String对象 ※String.replace():替换字符串 实例: str.replace ...
- c# enum遍历
public enum Suit { Spades, Hearts, Clubs, Diamonds } //遍历valueforeach (Suit suit in (Suit[]) Enum.Ge ...
- spring 学习(一):使用 intellijIDEA 创建 maven 工程进行 Spring ioc 测试
spring学习(一):使用 intellijIDEA 创建 maven 工程进行 Spring ioc 测试 ioc 概念 控制反转(Inversion of Control,缩写为IOC),是面向 ...