zoukankan      html  css  js  c++  java
  • hdu 1005 Number Sequence zoj 1105

    使用矩阵的方法解决zoj2105

    结果出现了一下两种情况:

    仅仅是改了一下求第n项,变为先求第n-1,n-2项,再求第n项!结果竟是不一样~~路过的大牛指导一下,谢谢!!

    Wrong的代码:

    /*
    * zoj2105.c
    *
    * Created on: 2011-9-20
    * Author: bjfuwangzhu
    */

    #include
    <stdio.h>
    #define nmax 2
    #define nnum 7
    typedef
    struct matrix {
    int num[nmax][nmax];
    } matrix;
    matrix mul_matrix(matrix a, matrix b) {
    matrix c;
    int i, j, k;
    for (i = 0; i < nmax; i++) {
    for (j = 0; j < nmax; j++) {
    c.num[i][j]
    = 0;
    for (k = 0; k < nmax; k++) {
    c.num[i][j]
    += a.num[i][k] * b.num[k][j] % nnum;
    c.num[i][j]
    %= nnum;
    }
    }
    }
    return c;
    }
    void solve(int a, int b, int n) {
    matrix temp, res;
    res.num[
    0][0] = 1, res.num[1][1] = 1, res.num[1][0] = res.num[0][1] = 0;
    temp.num[
    0][0] = a % nnum, temp.num[0][1] = b % nnum, temp.num[1][0] = 1, temp.num[1][1] =
    0;
    while (n) {
    if (n & 1) {
    res
    = mul_matrix(res, temp);
    }
    temp
    = mul_matrix(temp, temp);
    n
    >>= 1;
    }
    printf(
    "%d\n", res.num[0][0]);
    }
    int main() {
    #ifndef ONLINE_JUDGE
    freopen(
    "data.in", "r", stdin);
    #endif
    int a, b, n;
    while (~scanf("%d %d %d", &a, &b, &n)) {
    if (a == 0 && b == 0 && n == 0) {
    break;
    }
    if (n == 1 || n == 2) {
    puts(
    "1");
    continue;
    }
    solve(a, b, n
    - 1);
    }
    return 0;
    }

    Accept的代码:

    /*
    * zoj2105.c
    *
    * Created on: 2011-9-20
    * Author: bjfuwangzhu
    */

    #include
    <stdio.h>
    #define nmax 2
    #define nnum 7
    typedef
    struct matrix {
    int num[nmax][nmax];
    } matrix;
    matrix mul_matrix(matrix a, matrix b) {
    matrix c;
    int i, j, k;
    for (i = 0; i < nmax; i++) {
    for (j = 0; j < nmax; j++) {
    c.num[i][j]
    = 0;
    for (k = 0; k < nmax; k++) {
    c.num[i][j]
    += a.num[i][k] * b.num[k][j] % nnum;
    c.num[i][j]
    %= nnum;
    }
    }
    }
    return c;
    }
    void solve(int a, int b, int n) {
    matrix temp, res;
    res.num[
    0][0] = 1, res.num[1][1] = 1, res.num[1][0] = res.num[0][1] = 0;
    temp.num[
    0][0] = a % nnum, temp.num[0][1] = b % nnum, temp.num[1][0] = 1, temp.num[1][1] =
    0;
    while (n) {
    if (n & 1) {
    res
    = mul_matrix(res, temp);
    }
    temp
    = mul_matrix(temp, temp);
    n
    >>= 1;
    }
    printf(
    "%d\n", (res.num[0][0] + res.num[0][1]) % nnum);
    }
    int main() {
    #ifndef ONLINE_JUDGE
    freopen(
    "data.in", "r", stdin);
    #endif
    int a, b, n;
    while (~scanf("%d %d %d", &a, &b, &n)) {
    if (a == 0 && b == 0 && n == 0) {
    break;
    }
    if (n == 1 || n == 2) {
    puts(
    "1");
    continue;
    }
    solve(a, b, n
    - 2);
    }
    return 0;
    }

    另一种写法,找规律:

    #include<iostream>
    using namespace std;
    int main() {
    int A, B, n, i, a[2000];
    while (cin >> A >> B >> n) {
    if (A == 0 && B == 0 && n == 0)
    break;
    if (n == 1 || n == 2) {
    cout
    << "1" << endl;
    continue;
    }
    a[
    1] = 1;
    a[
    2] = 1;
    A
    %= 7;
    B
    %= 7;
    for (i = 3; i < 2000; i++) {
    a[i]
    = (A * a[i - 1] + B * a[i - 2]) % 7;
    if (a[i - 1] == 1 && a[i] == 1)
    break;
    }
    i
    -= 2;
    n
    %= i;
    a[
    0] = a[i];
    cout
    << a[n] << endl;
    }
    return 0;
    }




  • 相关阅读:
    如何使用数据卷在宿主机和docker容器之间共享文件
    Debian 7 安装 Docker
    ajax简单封装
    GridView列的排序功能
    SqlHelper帮助类
    模板引擎小例子
    WCF练习小程序总结
    热线接口开发调试工作记录
    在Oracle中使用rank()over()排名的问题
    项目实施中出现的问题点汇总
  • 原文地址:https://www.cnblogs.com/xiaoxian1369/p/2182835.html
Copyright © 2011-2022 走看看