hdu 6198 number number number

本文介绍了一种使用矩阵快速幂算法高效计算斐波那契数的方法,并提供了两种不同的C++实现方式,包括定义矩阵结构体及其实现矩阵乘法、快速幂等关键步骤。

学了下矩阵做斐波那契数列。。

#include <cstdio>
#include <iostream>

using namespace std;

const int MOD =998244353;

struct matrix//定义矩阵结构体
{
    long long  m[2][2];
}ans, base;

matrix multi(matrix a, matrix b)//定义矩阵乘法
{
    matrix tmp;
    for(int i = 0; i < 2; ++i)
    {
        for(int j = 0; j < 2; ++j)
        {
            tmp.m[i][j] = 0;
            for(int k = 0; k < 2; ++k)
                tmp.m[i][j] = (tmp.m[i][j] + 1LL*a.m[i][k] * b.m[k][j]%MOD) % MOD;
        }
    }
    return tmp;
}
int fast_mod(int n)  // 求矩阵 base 的  n 次幂
{
    base.m[0][0] = base.m[0][1] = base.m[1][0] = 1;
    base.m[1][1] = 0;
    ans.m[0][0] = ans.m[1][1] = 1;  // ans 初始化为单位矩阵
    ans.m[0][1] = ans.m[1][0] = 0;
    while(n)
    {
        if(n & 1)  //实现 ans *= t; 其中要先把 ans赋值给 tmp,然后用 ans = tmp * t
            ans = multi(ans, base);

        base = multi(base, base);
        n >>= 1;
    }
    return ans.m[0][1];
}

int main()
{
    int n;
    while(~scanf("%d", &n))
    {
        //n++;
        //n*=2;
        //n+=2;
        n=3+n*2;
        printf("%d\n", fast_mod(n)-1);
    }
    return 0;
}

队友代码

#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdio>
#include<vector>

typedef long long unsigned ll;
const ll mod = 998244353;
const int inf = 0x3f3f3f3f;

using namespace std;

typedef vector<ll> vec;
typedef vector<vec> mat;

mat mul(mat A,mat B){
    mat C(2,vec(2));
    for(int i=0;i<2;i++)
        for(int k=0;k<2;k++)
            for(int j=0;j<2;j++){
                C[i][j]=(C[i][j]+A[i][k]*B[k][j])%mod;
            }
    return C;
}

mat Pow(mat A,ll n){
    mat C(2,vec(2));
    C[0][0]=C[1][1]=1;
    while(n){
        if(n&1)    C=mul(C,A);
        n>>=1;
        A=mul(A,A);
    }
    return C;
}

int main(){
    ll n;
    while(scanf("%lld",&n)==1){
        mat C(2,vec(2));
        C[0][0]=C[0][1]=C[1][0]=1;
        C[1][1]=0;
        mat D=Pow(C,n*2+1);
        ll ans=(D[0][0]+D[0][1]-1)%mod;
    //    cout<<(Pow(C,n))[0][0]<<endl;
        printf("%lld\n",ans);
    }
    return 0;
}
### HDU 1682 Problem Explanation and Solution HDU 1682 is titled "Find a way". This problem involves finding the shortest path in a grid with specific constraints. The grid contains obstacles, and the task is to determine the minimum number of steps required to reach the destination from the starting point while avoiding obstacles[^5]. #### Problem Description The input consists of multiple test cases. Each test case includes: - A grid size `N x M`. - A grid where each cell is either empty (denoted by '.') or blocked (denoted by '#'). - The starting position `(x1, y1)` and the destination position `(x2, y2)`. The goal is to find the shortest path from the start to the destination, moving only up, down, left, or right, and avoiding blocked cells. #### Approach This problem can be solved using **Breadth-First Search (BFS)**, which is ideal for finding the shortest path in an unweighted graph. BFS ensures that the first time a node is visited, it is reached via the shortest possible path from the source. Here is a step-by-step explanation of the algorithm: - Represent the grid as a 2D array. - Use a queue to store the current position and the number of steps taken to reach it. - Mark visited cells to avoid revisiting them. - Expand the search in all four directions (up, down, left, right) at each step. - If the destination is reached, output the number of steps. Otherwise, if no path exists, output -1. #### Implementation Below is a Python implementation of the solution: ```python from collections import deque def solve(): T = int(input()) # Number of test cases results = [] for _ in range(T): N, M = map(int, input().split()) # Grid dimensions grid = [input().strip() for _ in range(N)] x1, y1, x2, y2 = map(int, input().split()) # Start and end positions # Adjust for zero-based indexing x1 -= 1; y1 -= 1; x2 -= 1; y2 -= 1 # BFS Initialization queue = deque([(x1, y1, 0)]) # (current_x, current_y, steps) visited = [[False] * M for _ in range(N)] visited[x1][y1] = True # Directions: up, down, left, right directions = [(-1, 0), (1, 0), (0, -1), (0, 1)] found = False while queue: cx, cy, steps = queue.popleft() if cx == x2 and cy == y2: results.append(steps) found = True break for dx, dy in directions: nx, ny = cx + dx, cy + dy if 0 <= nx < N and 0 <= ny < M and not visited[nx][ny] and grid[nx][ny] == '.': visited[nx][ny] = True queue.append((nx, ny, steps + 1)) if not found: results.append(-1) for result in results: print(result) # Example Input/Output # Input: # 1 # 3 3 # ... # .## # ... # 1 1 3 3 # Output: # 4 ``` #### Key Points - BFS guarantees the shortest path in an unweighted grid. - Visited cells are marked to prevent cycles and redundant computations. - The algorithm terminates early if the destination is reached.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值