2016 pku campus/OpenJ_POJ - C16H(推公式+矩阵快速幂)
描述
Wenwen has a magical ball. When put on an infinite plane, it will keep duplicating itself forever.
Initially, Wenwen puts the ball on the location (x0, y0) of the plane. Then the ball starts to duplicate itself right away. For every unit of time, each existing ball on the plane will duplicate itself, and the new balls will be put on the adjacent locations. The duplication rule of these balls is, during the i-th unit of time, a ball, which locates at (x, y), will duplicate ui balls to (x, y+1), di balls to (x, y-1), li balls to (x-1, y) and ri balls to (x+1, y).
The duplication rule has a period of M. In another words, ui=ui-M, di=di-M, li=li-M, ri=ri-M, for i=M+1,M+2,...
Wenwen is very happy because she will get many balls. It is easy to calculate how many balls she will get after N units of time. However, she wants to know the sum of x-coordinates and y-coordinates of all balls after N units of time. This is a bit difficult for her. Could you help her? Since the sum might be very large, you should give the sum modulo 1,000,000,007 to her.
输入The first line contains an integer T (1 ≤ T ≤ 25), indicating the number of test cases.
For each test case:
The first line contains four integers N (1 ≤ N ≤ 10^18), M (1 ≤ M ≤ 20,000), x0 and y0 (-10^18 ≤ x0,y0 ≤ 10^18);
Then follows M lines, the i-th line contains four integers: ui, di, li and ri (0 ≤ ui,di,li,ri ≤ 10,000).输出For each test case, output one integer on a single line, indicating the sum of x-coordinates and y-coordinates of all balls after N units of time, modulo 1,000,000,007.样例输入
1 2 2 1 1 2 0 0 0 0 0 0 1
样例输出
19
Initially, there is 1 ball on (1,1).
After 1 unit of time, there is 1 ball on (1,1) and 2 balls on (1,2);
After 2 units of time, there is 1 ball on (1,1), 2 balls on (1,2), 1 ball on (2,1) and 2 balls on (2,2).
Therefore, after 2 units of time, the sum of x-coordinates and y-coordinates of all balls is
(1+1)*1+(1+2)*2+(2+1)*1+(2+2)*2=19.
#include <bits/stdc++.h>
#define maxn 100005
using namespace std;
const int mod=1e9+7;
typedef long long ll;
struct Matrix{ll mo[2][2];Matrix(){memset(mo,0,sizeof(mo));}
};
Matrix Mul(Matrix x,Matrix y){Matrix c;for(int i=0;i<2;i++){for(int j=0;j<2;j++){for(int k=0;k<2;k++){c.mo[i][j]=(c.mo[i][j]+x.mo[i][k]*y.mo[k][j])%mod;}}}return c;
}
Matrix powmod(Matrix x,ll n){Matrix res;for(int i=0;i<2;i++){res.mo[i][i]=1;}while(n){if(n&1) res=Mul(res,x);n>>=1;x=Mul(x,x);}return res;
}
int A[maxn];
int B[maxn];
int main()
{int t;scanf("%d",&t);while(t--){ll n,m,x0,y0;scanf("%lld%lld%lld%lld",&n,&m,&x0,&y0);x0=(x0%mod+mod)%mod;y0=(y0%mod+mod)%mod;for(int i=0;i<m;i++){int a,b,c,d;scanf("%d%d%d%d",&a,&b,&c,&d);A[i]=1+a+b+c+d;B[i]=a-b+d-c;}Matrix base;for(int i=0;i<2;i++){base.mo[i][i]=1;}for(int i=0;i<m;i++){Matrix tmp;tmp.mo[0][0]=tmp.mo[1][1]=A[i],tmp.mo[1][0]=B[i];base=Mul(base,tmp);}base=powmod(base,n/m);for(int i=0;i<n%m;i++){Matrix tmp;tmp.mo[0][0]=tmp.mo[1][1]=A[i],tmp.mo[1][0]=B[i];base=Mul(base,tmp);}cout<<(base.mo[1][0]+base.mo[1][1]*(x0+y0))%mod<<endl;}return 0;
}
转载于:https://www.cnblogs.com/Chen-Jr/p/11007294.html
2016 pku campus/OpenJ_POJ - C16H(推公式+矩阵快速幂)相关推荐
- HDU - 4565 So Easy!(共轭构造+推公式+矩阵快速幂)
题目链接:点击查看 题意:给出a,b,m,n,求 解析:题意一目了然,此题的关键是推出解决问题的公式. 首先,题目要求括号内n次方后向上取整再模m,而这里给到的a与b的关系接下来也会用到:<b& ...
- 牛客14607 递推(矩阵快速幂构造)
链接:https://ac.nowcoder.com/acm/problem/14607 来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 131072K,其他语言2621 ...
- UESTC - 1610 递推方程+矩阵快速幂
感觉像是HDU Keyboard的加强版,先推出3张牌时的所有组合,然后递推出n张牌 看到n=1e18时吓尿了 最后24那里还是推错了.. (5行1列 dp[1][n],dp[2][n],dp[3][ ...
- hdu 6395Sequence【矩阵快速幂】【分块】
Sequence Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total ...
- 20181023(模拟+矩阵快速幂及推公式+最短路+不知道什么DP)
NOIP欢乐%你赛 1. 小澳的方阵 (matrix.cpp/c/pas) [题目描述] 小澳最近迷上了考古,他发现秦始皇的兵马俑布局十分有特点,热爱钻研的小澳打算在电脑上还原这个伟大的布局. 他努力 ...
- HDU 5863 cjj's string game ( 16年多校10 G 题、矩阵快速幂优化线性递推DP )
题目链接 题意 : 有种不同的字符,每种字符有无限个,要求用这k种字符构造两个长度为n的字符串a和b,使得a串和b串的最长公共部分长度恰为m,问方案数 分析 : 直觉是DP 不过当时看到 n 很大.但 ...
- C语言实现求解斐波那契数列的四种方法及优化处理(递归,迭代,特殊性质公式,矩阵快速幂)
众所周知,斐波那契数列是非常经典的一个数列,它的数学公式如下 为了便于观察,我们列出它的几项:0 1 1 2 3 5 8 13 21...... 下面我们将介绍四种方法来用C语言计算机 ...
- 矩阵快速幂递推(五行)
矩阵快速幂 第一次写博客,想着写哪个知识点呢..来想着还是写我第一个理解了的并且在比赛中成功写出来的(orz)矩阵快速幂. 快速幂 首先快速幂先来复习一下 所谓快速幂,就是加快幂运算的算法. 主要公式 ...
- HDU 6185 Covering 矩阵快速幂 递推
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6185 题目描述: 一个4*n的矩形, 你用1*2的矩形覆盖有多少种方案, n <= 1e18 ...
最新文章
- 停电后程序员怎么写代码 | 每日趣闻
- 买房必看!又一程序员自编“购房宝典”火爆 GitHub
- python项目超级大脑-python项目之超级大脑
- [Machine Learning]kNN代码实现(Kd tree)
- mysql ondumplictcate_4.5万字手把手教你实现MySQL TB级数据存储!!
- java 数组构造_java – 从数组构造(非二进制)树
- linux运行级别与服务
- 英文简历 计算机知识,计算机应届生英文简历范文
- 【Kafka】Kafka BrokerEndPointNotAvailableException: End point with security protocol PLAINTEXT not
- 关于scanf 与 cin gets(),getline()......输入输出字符串的区别
- Java命令运行没反应_cmd中执行java命令没有输出结果
- python 身份证验证系统_用Python写一个身份证号码校验系统
- 【转】鼠标右键菜单设置大全
- python爬取网页原理_网页基本构成和抓取原理
- ubuntu安装aircrack-ng/reaver/minidwep-gtk用来跑pin
- Python爬虫获取网易云歌单封面(带Cookie)
- PC端调用摄像头录制视频——vue标准写法
- 阿里云sms短信服务
- Android视频编辑器(二)预览、录制视频加上水印和美白磨皮效果
- 如果通过这次面试我们单位录用了你,但工作一段时间却发现你根本不适合这个职位,你怎么办?