Codeforces 450B Jzzhu and Sequences(矩阵快速幂)

本文介绍了一种使用矩阵快速幂的方法来高效计算特定类型的数列第n项的值,并通过一个具体的编程实现示例说明了该方法的具体应用过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    f1 = x, f2 = y;
    fi = f(i-1) + f(i+1)
        fi = f(i-1)-f(i-2);


类似于斐波那契构造矩阵

| 1 -1 |

| 1 0 |


代码:

#include<bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
struct node
{
    int s[2][2];
    node() {}
    node(int a, int b, int c, int d)
    {
        s[0][0] = a;
        s[0][1] = b;
        s[1][0] = c;
        s[1][1] = d;
    }
};

node mul(node a, node b)
{

    node t = node(0, 0, 0, 0);
    for(int i = 0; i < 2; i++)
        for(int j = 0; j < 2; j++)
            for(int k = 0; k < 2; k++)
                t.s[i][j] = (t.s[i][j]+a.s[i][k]*b.s[k][j])%mod;
    return t;
}

node mt_pow(node p, int n)
{
    node q = node(1, 0, 0, 1);
    while(n)
    {

        if(n&1) q = mul(q, p);
        p = mul(p, p);
        n /= 2;
    }
    return q;
}

int main(void)
{
    int f1, f2, n;
    while(cin >> f1 >> f2 >> n)
    {
        if(n == 1) printf("%d\n", (f1%mod+mod)%mod);
        else if(n == 2) printf("%d\n", (f2%mod+mod)%mod);
        else
        {
            node base = node(1, -1, 1, 0);
            node ans = mt_pow(base, n-2);
            int res = (ans.s[0][0]*f2+ans.s[0][1]*f1)%mod;
            if(res < 0) res += mod;
            printf("%d\n", res);
        }
    }
    return 0;
}


A - Jzzhu and Sequences
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Appoint description: 

Description

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).

Sample Input

Input
2 3
3
Output
1
Input
0 -1
2
Output
1000000006

Hint

In the first sample, f2 = f1 + f33 = 2 + f3f3 = 1.

In the second sample, f2 =  - 1 - 1 modulo (109 + 7) equals (109 + 6).


### Codeforces 平台上的快速幂算法题目及相关实现 #### 快速幂算法简介 快速幂是一种高效的计算 $a^b \mod c$ 的算法,其时间复杂度为 $O(\log b)$。通过将指数分解成二进制形式并利用平方倍增的方式减少乘法次数来提高效率。 --- #### 题目解析与实现方法 以下是两道来自 Codeforces 平台上涉及快速幂的经典题目及其解决方案: --- ##### **题目一:Codeforces 1594B** 这是一道典型的快速幂应用题,目标是找到第 $k$ 个以 $n$ 为底的幂底数累加的结果[^1]。 ###### 解决方案 给定整数 $n$ 和 $k$,我们需要按照 $k$ 的二进制表示逐位判断哪些位置对应的幂需要被加入最终结果中。具体实现如下: ```cpp #include <iostream> #define ll long long using namespace std; const int MOD = 1e9 + 7; int main() { int t; cin >> t; while (t--) { ll n, k; cin >> n >> k; ll ans = 0, s = 1; // 初始化答案和当前幂值 while (k != 0) { if (k & 1) { // 如果当前位为1,则加上对应幂值 ans = (ans + s) % MOD; } s = s * n % MOD; // 更新幂值 k >>= 1; // 右移一位 } cout << ans << endl; } return 0; } ``` 此代码的核心在于使用 `while` 循环逐步处理 $k$ 的每一位,并根据是否为 $1$ 来决定是否将其对应的幂值加入到总和中。 --- ##### **题目二:CodeForces - 894B Ralph And His Magic Field** 本题同样涉及到快速幂的应用,但更侧重于组合数学中的计数问题[^2]。 ###### 解决方案 对于输入的三个参数 $n$, $m$, 和 $k$,我们可以通过快速幂函数高效地求解 $(2^{(n-1)})^{(m-1)} \mod INF$ 的值。如果 $k=-1$ 且 $(n+m)\%2\neq0$,则直接返回 $0$;否则继续执行快速幂逻辑。 ```cpp #include <iostream> using namespace std; const long long INF = 1e9 + 7; long long fast_pow(long long base, long long exp) { long long result = 1; base %= INF; while (exp > 0) { if (exp & 1) { result = (result * base) % INF; } base = (base * base) % INF; exp >>= 1; } return result; } int main() { long long n, m, k; cin >> n >> m >> k; if (k == -1 && (n + m) % 2 != 0) { cout << "0" << endl; return 0; } cout << fast_pow(fast_pow(2, n - 1), m - 1) << endl; return 0; } ``` 这段程序定义了一个通用的快速幂辅助函数 `fast_pow`,用于简化主流程中的幂运算部分[^2]。 --- #### 总结 以上两个例子展示了如何在不同场景下灵活运用快速幂技术解决问题。无论是简单的累加还是复杂的嵌套幂运算,都可以借助这一技巧显著提升性能。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值