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 ...
随机推荐
- 面试题:Java程序员最常用的20%技术 已看1
首先常用api(String,StringBuffer/StringBuilder等) 1.集合类,线程类 2.Servlet(很少用纯粹的servlet写,但你要懂,因为很多框架都是基于servle ...
- IntelliJ IDEA——利用maven插件构建web工程
- hdu 4681 String(转载)
#include <stdio.h> #include <string.h> #include <algorithm> #include <iostream& ...
- js-简单的作业
作业 1 将课堂 偶数奇数和 猜数字游戏 电话银行转for循环 2 编写 “个人所得税计算器”函数 10000 计算个税的方法: 3500 以下免征 6500 3500 ~ 5000 部分 缴纳 3% ...
- Robot Framework - 基础关键字 BuiltIn 库(一)
今天给大家分享的是Robot Framework 机器人框架中 BuiltIn 基础库的使用...BuiltIn 库里面提供了很多基础方法助力于我们在自动化测试领域中做的更好!——本系列教程是教会大家 ...
- VC解决方案,项目,开发一段时间启动调试很慢,半天才开始链接
笔者这两天写代码过程中,发现自己解决方案下的程序启动调试,半天才开始加载相关的各种库.导致调试的时候很是郁闷 开始以为是项目关联的工程太多导致,但是在相同的解决方案sln下面,新建一个简单的控制台程序 ...
- Capturing ASP.NET Application Startup Exceptions
It has become common practice to perform tasks during an ASP.NET applications start up process. Thes ...
- 深数据 - Deep Data
暂无中文方面的信息,E文的也非常少,原文连接: A lot of great pieces have been written about the relatively recent surge in ...
- [转]不完美解决V社游戏的中文支持问题
先安装安装文泉驿正黑:sudo apt-get install fonts-wqy-zenhei 然后sudo gedit /etc/fonts/conf.avail/25-wqy-zenhei.co ...
- Sobel算法
最近看了一些Sobel算法,并试了一下,源码如下: private void Sobel(Bitmap img) { int width = img.Width; int height = img.H ...