HDU 1005 Number Sequence(矩阵快速幂、循环节)

Number Sequence(传送门)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Problem Description
A number sequence is defined as follows:

f(1)=1,f(2)=1 f ( 1 ) = 1 , f ( 2 ) = 1 , f(n)=(Af(n1)+Bf(n2))mod7 f ( n ) = ( A ∗ f ( n − 1 ) + B ∗ f ( n − 2 ) ) m o d 7 .

Given A, B, and n, you are to calculate the value of f(n).

Input
The input consists of multiple test cases. Each test case contains 3 integers A, B and n on a single line (1 <= A, B <= 1000, 1 <= n <= 100,000,000). Three zeros signal the end of input and this test case is not to be processed.

Output
For each test case, print the value of f(n) on a single line.
Sample Input
1 1 3
1 2 10
0 0 0
Sample Output
2
5

解法一:因为给出了递推式,可以矩阵快速幂来解决,倒也不难。

#include<iostream>
#include<cstring>
using namespace std;
void multi(int matrix1[][3], int matrix2[][3])
{
    int temp[3][3] = {0};
    for(int i = 0; i < 3; i++)
        for(int j = 0; j < 3; j++)
        for(int k = 0; k < 3; k++)
        temp[i][k] += ((matrix1[i][j] % 7)* (matrix2[j][k] % 7)) % 7;
    for(int i = 0; i < 3; i++)
        for(int j = 0; j < 3; j++)
        matrix1[i][j] = temp[i][j] % 7;
}
int ans[3][1];
void multi1(int matrix1[3][3], int matrix2[3][1])
{
    memset(ans, 0, sizeof ans);
    for(int i = 0; i < 3; i++)
        for(int j = 0; j < 3; j++)
        for(int k = 0; k < 1; k++)
        {
            ans[i][k] += (matrix1[i][j] * matrix2[j][k]) % 7;
            ans[i][k] %= 7;
        }
}
int res[3][3];
void pow(int matrix[][3], int n)
{
    memset(res, 0, sizeof res);
    for(int i = 0; i < 3; i++)res[i][i] = 1;
    while(n > 0)
    {
        if(n & 1)
            multi(res, matrix);
        multi(matrix, matrix);
        n = n >> 1;
    }
}
int main()
{
    ios::sync_with_stdio(false);
    int a, b, n;
    while(cin>>a>>b>>n)
    {
        if(a == 0 && b == 0 && n == 0)break;
        int m[3][3] = {
            a, b, 0,
            1, 0, 0,
            0, 1, 0
        };
        pow(m, n  - 1);
        int t = (a + b) % 7;
        int f1[3][1] = {t, 1, 1};
        multi1(res, f1);
        cout<<ans[2][0]<<endl;
    }
    return 0;
}

解法2利用循环节解决 。 因为 f(n)=(Af(n1)+Bf(n2))mod7. f ( n ) = ( A ∗ f ( n − 1 ) + B ∗ f ( n − 2 ) ) m o d 7.
f(n) 能取的值有:0,1,2,3,4,5,6,7个数,f(n - 1)有7种可能,f(n - 2)也有7种可能, 所以最多有49种情况。因为f(n)是由两个数递推而来,所以只要后面出现两个数与前面的两个数相同,必定开始产生循环节,但是要注意,循环节不一定由1,1开始(例如当 A = 247, B = 602, n = 35363857 时, 它的产生循环为(1,1,4,2,1,4,2 ……)因此我们可以通过枚举循环节的起点来找到循环节。

#include<iostream>
using namespace std;
int main()
{
    int f[100] = {0,1,1};
    int a, b, n;
    while(cin>>a>>b>>n)
    {
        if(a == 0 && b == 0 && n == 0)break;
        for(int i = 3; i < 100; i++)
            f[i] = (a * f[i-1] + b * f[i-2]) % 7;
        int s = 1; //循环节的起点
        int findt = 0;
        int temp = 0; //循环节的大小
        for(int j = s; j < 50; j++)
        {//枚举循环节的起点
            for(int i = j + 2; i < 99; i++)
            {//匹配
                if(f[i] == f[j] && f[i + 1] == f[j + 1]){
                    findt = 1;
                    temp = i - j;
                    s = j; //更新循环节的起点
                    break;
                }
            }
            if(findt == 1)break;
        }
        if(s == 1)
        {
            if(n % temp == 0) cout<<f[temp]<<endl;
            else cout<<f[n % temp]<<endl;
        }
        else{//循环节的起点不是第一个数
            if(n < s)cout<<f[n]<<endl;
            else {
                n = n - s;
                if(n % temp == 0)cout<<f[s+temp]<<endl;
                else cout<<f[n % temp + s]<<endl;
            }
        }
    }
    return 0;
}

其实,网上关于这道题的题解也很多,但很多都是错的(虽然能够AC)。
附上一些测试数据(测试数据来自discuss):
247 602 35363857
376 392 9671521
759 623 18545473
53 399 46626337
316 880 10470347
0 0 0


Output:
4
3
5
2
3

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值