• HDU 5950Recursive sequence ICPC沈阳站


    Recursive sequence

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 1525    Accepted Submission(s): 710


    Problem Description
    Farmer John likes to play mathematics games with his N cows. Recently, they are attracted by recursive sequences. In each turn, the cows would stand in a line, while John writes two positive numbers a and b on a blackboard. And then, the cows would say their identity number one by one. The first cow says the first number a and the second says the second number b. After that, the i-th cow says the sum of twice the (i-2)-th number, the (i-1)-th number, and i4. Now, you need to write a program to calculate the number of the N-th cow in order to check if John’s cows can make it right. 
     
    Input
    The first line of input contains an integer t, the number of test cases. t test cases follow.
    Each case contains only one line with three numbers N, a and b where N,a,b < 231 as described above.
     
    Output
    For each test case, output the number of the N-th cow. This number might be very large, so you need to output it modulo 2147493647.
    Sample Input
    2
    3 1 2
    4 1 10
    Sample Output
    85
    369
    Hint
    In the first case, the third number is 85 = 2*1十2十3^4.
    In the second case, the third number is 93 = 2*1十1*10十3^4 and the fourth number is 369 = 2 * 10 十 93 十 4^4.

    递推超时,矩阵快速幂

    #pragma comment(linker, "/STACK:102400000,102400000")
    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #include <cstdlib>
    #include <map> 
    #include <set>
    #include <ctime> 
    #include <queue> 
    
    #define LL long long
    
    using namespace std;
    
    const LL _MOD = 2147493647, maxN = 4, MOD = _MOD*2;
    
    int n;
    
    LL f(int _n)
    {
        LL n = _n, ans =200, t=1;
        t = t*n%MOD; ans = (ans + t*139)%MOD;
        t = t*n%MOD; ans = (ans + t*48)%MOD;
        t = t*n%MOD; ans = (ans + t*10)%MOD;
        t = t*n%MOD; ans = (ans + t)%MOD;
        return ans/2 % _MOD;
    }
    
    struct matrix
    {
        int n, m;
        LL a[maxN][maxN];
        LL* operator [](int x) {return a[x];}
        void print()
        {
            for(int i = 1; i <= n; i++)
            {
                for(int j = 1; j <= m; j++)
                    printf("%d ", a[i][j]);
                printf("
    ");
            }
            printf("
    ");
        }
    };
    
    matrix operator *(matrix a, matrix b)
    {
        matrix c; c.n = a.n; c.m = b.m;
        memset(c.a, 0, sizeof(c.a));
        LL tmp;
        for(int i = 1; i <= a.n; i++)
        {
            tmp = 0;
            for(int j = 1; j <= b.m; j++)
            {
                for(int k = 1; k <= a.m; k++)    tmp = (tmp+a[i][k] * b[k][j])%_MOD;
                c[i][j] = tmp % _MOD;
                tmp = 0;
            }
        }
        return c;
    }
    
    matrix operator ^(matrix a, LL x)
    {
        matrix b;
        memset(b.a, 0, sizeof(b.a));
        b.n = a.n; b.m = a.m;
        for(int i=1; i <= a.n; i++)    b[i][i]=1;
        for(;x;a=a*a,x>>=1)    if(x&1)    b=b*a;
        return b;
    }
    
    int main()
    {
    //    cout<<2*f(3)+f(4)-f(5)<<endl;
    //    return 0;
        #ifndef ONLINE_JUDGE
        freopen("test_in.txt", "r", stdin); 
        //freopen("test_out.txt", "w", stdout);
        #endif
        int T; scanf("%d", &T);
        while(T--)
        {
            int a, b, n; scanf("%d%d%d", &n, &a, &b);
            LL _a = a; _a += f(1); LL _b = b; _b += f(2);
            matrix m; m.n = m.m = 2; m[1][1] = _a; m[1][2] = _b; m[2][1] = m[2][2] = 0;
            matrix t; t.n = t.m = 2; t[1][1] = 0; t[1][2] = 2;  t[2][1] = t[2][2] = 1;
            t = t^(n-2); 
            m = m*t;
            LL ans = (m[1][2] - f(n) + _MOD) % _MOD;
            printf("%d
    ", (int)ans);
        }
    }
  • 相关阅读:
    Hibernate
    Mysql
    JavaWeb
    Springboot
    spring MVC
    spring
    mybatis学习
    ftp客户端封装
    win10子系统 wsl开机启动ssh服务
    eclipse 终于官方支持代码模糊提示了
  • 原文地址:https://www.cnblogs.com/kimsimple/p/7402154.html
Copyright © 2020-2023  润新知