您的位置:首页 > 产品设计 > UI/UE

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

解题思路:矩阵快速幂。
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: