cf 450b 矩阵快速幂(数论取模 一大坑点啊)
Jzzhu has invented a kind of sequences, they meet the following property:
You are given x and y, please calculate fn modulo 1000000007 (109 + 7).
Input
The first line contains two integers x and y (|x|, |y| ≤ 109). The second line contains a single integer n (1 ≤ n ≤ 2·109).
Output
Output a single integer representing fn modulo 1000000007 (109 + 7).
Example
2 3
3
1
0 -1
2
1000000006
Note
In the first sample, f2 = f1 + f3, 3 = 2 + f3, f3 = 1.
In the second sample, f2 = - 1; - 1 modulo (109 + 7) equals (109 + 6).
题意 : 显然是矩阵快速幂么
坑点 : 就是数论取模这一块 , 稍不注意就错了
const ll mod = 1e9+7;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
#define Max(a,b) a>b?a:b
#define Min(a,b) a>b?b:a struct mat
{
ll a[2][2];
}; mat mul(mat a, mat b){
mat r;
memset(r.a, 0, sizeof(r.a)); for(int i = 0; i < 2; i++){
for(int k = 0; k < 2; k++){
if (a.a[i][k]){
for(int j = 0; j < 2; j++){
if (b.a[k][j]){
r.a[i][j] += (a.a[i][k] * b.a[k][j] + mod)%mod;
r.a[i][j] = (r.a[i][j] + mod)%mod;
}
}
}
}
}
return r;
} mat pow(mat a, int n){
mat b; for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
if (i == j) b.a[i][i] = 1;
else b.a[i][j] = 0; while(n){
if (n & 1) b = mul(a, b);
a = mul(a, a);
n >>= 1;
}
return b;
} int main() {
ll x, y, n; scanf("%lld%lld%lld", &x, &y, &n);
mat a;
a.a[0][0] = a.a[1][0] = 1;
a.a[0][1] = -1;
a.a[1][1] = 0; if (n == 1){
printf("%lld\n", (x+mod)%mod);
}
else if (n == 2){
printf("%lld\n", (y+mod)%mod);
}
else {
a = pow(a, n-2);
ll ans = ((a.a[0][0]*y+mod)%mod + (a.a[0][1]*x+mod)%mod + mod)%mod; // 重点就是这里
//ll ans = (a.a[0][0]*y+a.a[0][1]*x+mod)%mod;
//ans = (ans + mod)%mod;
printf("%lld\n", ans );
} return 0;
}
/*
-9 -11
12345
*/
cf 450b 矩阵快速幂(数论取模 一大坑点啊)的更多相关文章
- [CQOI2018]交错序列 (矩阵快速幂,数论)
[CQOI2018]交错序列 \(solution:\) 这一题出得真的很好,将原本一道矩阵快速幂硬生生加入组合数的标签,还那么没有违和感,那么让人看不出来.所以做这道题必须先知道(矩阵快速幂及如何构 ...
- HDU6395 Sequence(矩阵快速幂+数论分块)
题意: F(1)=A,F(2)=B,F(n)=C*F(n-2)+D*F(n-1)+P/n 给定ABCDPn,求F(n) mod 1e9+7 思路: P/n在一段n里是不变的,可以数论分块,再在每一段里 ...
- codefroces 450B矩阵快速幂
找出递推关系式就好了 (fi+1)=(1 -1)(fi ) ( fi)=(1 0)(fi-1) 不会打矩阵将就着看吧... 这是第一道矩阵快速幂.细节还是有很多没注意到的 本来想看挑战写 ...
- 51nod 1197 字符串的数量 V2(矩阵快速幂+数论?)
接上一篇,那个递推式显然可以用矩阵快速幂优化...自己随便YY了下就出来了,学了一下怎么用LaTeX画公式,LaTeX真是个好东西!嘿嘿嘿 如上图.(刚画错了一发...已更新 然后就可以过V2了 or ...
- 51nod 1013 3的幂的和 - 快速幂&除法取模
题目地址:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1013 Konwledge Point: 快速幂:https:/ ...
- hdu 1757 (矩阵快速幂) 一个简单的问题 一个简单的开始
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 题意不难理解,当x小于10的时候,数列f(x)=x,当x大于等于10的时候f(x) = a0 * ...
- CF1106F Lunar New Year and a Recursive Sequence——矩阵快速幂&&bsgs
题意 设 $$f_i = \left\{\begin{matrix}1 , \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ i < k\\ ...
- Luogu3824 [NOI2017]泳池 【多项式取模】【递推】【矩阵快速幂】
题目分析: 用数论分块的思想,就会发现其实就是连续一段的长度$i$的高度不能超过$\lfloor \frac{k}{i} \rfloor$,然后我们会发现最长的非$0$一段不会超过$k$,所以我们可以 ...
- 【BZOJ2432】【NOI2011】兔农(数论,矩阵快速幂)
[BZOJ2432][NOI2011]兔农(数论,矩阵快速幂) 题面 BZOJ 题解 这题\(75\)分就是送的,我什么都不想写. 先手玩一下,发现每次每次出现\(mod\ K=1\)的数之后 把它减 ...
随机推荐
- 带你认识“货真价实”的P2P网贷风控
文/杨帆 说起P2P,多数金融圈内人士已经并不陌生.国内现有近千家的P2P网贷平台,动辄打出高息诱人的收益率宣传口号以及眼花缭乱的安全承诺.但是在这些浮华表面的背后,关于P2P的风控很多人仍然是一 ...
- Django入门7--博客撰写页面开发
- 关于Spring JavaWeb工程中的ContextRefreshedEvent事件
在应用启动时,通常想在此时预加载一些资源,全局使用. Spring会在操作应用上下文时,使用ApplicationEventPublisher触发相关ApplicationContextEvent,我 ...
- vue组件中data是个函数
当我们const vm = new Vue({ el : '#app', data : { msg:‘hello World’ } })用习惯了,data是一个对象,可到了vue组件 Vue.co ...
- H3C MSTP
- vue 项目使用局域网多端访问并实时自动更新(利用 browser-sync)
在写vue的项目中时,虽然vue会自动更新页面,但是切换页面切来切去也很麻烦,有时候我们还要在公司另一台电脑或者手机上调试,这时候利用browser-sync插件,无需改动vue的代码即可实现: 1. ...
- 【t081】序列长度(贪心做法)
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 有一个整数序列,我们不知道她的长度是多少(即序列中整数的个数),但我们知道在某些区间中至少有多少个整数 ...
- 【19.05%】【codeforces 680D】Bear and Tower of Cubes
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 螺旋矩阵O(1)根据坐标求值
传送门 洛谷2239 •题意 从矩阵的左上角(第11行第11列)出发,初始时向右移动: 如果前方是未曾经过的格子,则继续前进,否则右转: 重复上述操作直至经过矩阵中所有格子. 根据经过顺序,在格子中依 ...
- Deep Learning ——Yann LeCun,Yoshua Bengio&Geoffrey Hinton
引言: 深度学习的本质是用多层的神经网络找到一个可以被学习的复杂的函数实现语音识别,图像识别等功能. 多层神经网络的结构: 多层神经元的组成,每一层的输入都等于上一层的输出. 应用领域:cv,nlp ...