Codeforces 450B Jzzhu and Sequences(矩阵快速幂)
2014-09-24 15:13
337 查看
题目链接:Codeforces 450B Jzzhu and Sequences
题目大意:给定序列f1=x,
f2=y,fi=fi+1−fi−1,求fn
解题思路:矩阵快速幂。
题目大意:给定序列f1=x,
f2=y,fi=fi+1−fi−1,求fn
解题思路:矩阵快速幂。
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; const ll mod = 1e9+7; ll s[2][2] = {1, 0, 0, 1}; void mat_mod (ll a[2][2], ll b[2][2]) { ll c[2][2]; memset(c, 0, sizeof(c)); for (int k = 0; k < 2; k++) { for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) c[i][j] = ((c[i][j] + a[i][k] * b[k][j]) % mod + mod) % mod; } memcpy(a, c, sizeof(c)); } void mat_pow(int n) { ll x[2][2] = {0, 1, -1, 1}; while (n) { if (n&1) mat_mod(s, x); mat_mod(x, x); n >>= 1; } } int main () { int x, y, n; scanf("%d%d%d", &x, &y, &n); if (n == 1) printf("%lld\n", (x % mod + mod) % mod); else { mat_pow(n-2); printf("%lld\n", ((x * s[1][0] % mod + y * s[1][1] % mod) % mod + mod) % mod); } return 0; }
相关文章推荐
- Codeforces 450B f【n】=f【n-1】-f【n-2】(矩阵快速幂,裸题)
- CodeForces 678D Iterated Linear Function 矩阵快速幂
- codeforces 185a(矩阵快速幂)
- CodeForces 719E Sasha and Array 【线段树】【快速矩阵幂】
- codeforces 450-B Jzzhu and Sequences 矩阵快速幂
- Jzzhu and Sequences CodeForces - 450B (矩阵快速幂)
- Codeforces 551D GukiZ and Binary Operations(矩阵快速幂)
- codeforces 691E Xor-sequences 矩阵快速幂
- CodeForces 450B (矩阵快速幂模板题+负数取模)
- codeforces 691E Xor-sequences(矩阵快速幂)
- 矩阵快速幂 CodeForces - 582B Once Again...
- 【矩阵快速幂 】Codeforces 450B - Jzzhu and Sequences (公式转化)
- codeforces 450B B. Jzzhu and Sequences(矩阵快速幂)
- [Codeforces 697F] Legen... (AC自动机+取max的矩阵快速幂)
- codeforces 337C-quiz 贪心加矩阵快速幂
- 【codeforces 691 E】【矩阵快速幂 思维题】【给定序列,从序列中选择k个数(可以重复选择),使得得到的排列满足xi与xi+1异或的二进制表示中1的个数是3的倍数。问满足条件的序列个数】
- Codeforces 392C Yet Another Number Sequence(矩阵快速幂)
- Codeforces 450B Jzzhu and Sequences(矩阵快速幂)
- Codeforces 514E. Darth Vader and Tree DP+矩阵快速幂
- Codeforces 621E Wet Shark and Block【dp + 矩阵快速幂】