• C - Recursive sequence (矩阵ksm)


    原题链接:Problem - 5950

    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

    题意:给三个数n a b,规定一个数组,f[i]=2*f[i-2]+f[i-1]+i^4

    a是数组的第一个数,b是数组的第二个数

    求第n个数是多少(mod2^32)

    思路:根据f[i]=2*f[i-2]+f[i-1]+i^4

    把i^4展开就是i^4 + 4*i^3+ 6*i^2+ 4*i+ 1

    那么我们就需要i^4 i^3 i^2 i 1

    构造Fn={fn-2,fn-1,n^4,n^3,n^2,n,1}

    那么Fn+1={fn-1,fn,(n+1)^4,(n+1)^3,(n+1)^2,(n+1),1}

    那么A矩阵就是:

    0 2 0 0 0 0 0

    1 1 0 0 0 0 0

    0 1 1 0 0 0 0

    0 0 4 1 0 0 0

    0 0 6 3 1 0 0

    0 0 4 3 2 1 0

    0 0 1 1 1 1 1

    f3数组就是{a,b,81,27,9,3,1}

    然后就算出来: f3*A^(n-3)

    最后答案就是:2*f3[0][0]+f3[0][1]+f3[0][2];

    1. #include<bits/stdc++.h>
    2. using namespace std;
    3. typedef long long ll;
    4. const ll mod=2147493647;
    5. ll n,aa,bb;
    6. void mul(ll c[7][7],ll a[7][7],ll b[7][7]){
    7. ll temp[7][7]={0};
    8. for(int i=0;i<7;i++){
    9. for(int j=0;j<7;j++){
    10. for(int k=0;k<7;k++){
    11. temp[i][j]=(temp[i][j]+a[i][k]*b[k][j])%mod;
    12. }
    13. }
    14. }
    15. memcpy(c,temp,sizeof temp);
    16. }
    17. void sove(){
    18. scanf("%lld%lld%lld",&n,&aa,&bb);
    19. if(n==1){
    20. printf("%lld\n",aa);
    21. return ;
    22. }
    23. if(n==2){
    24. printf("%lld\n",bb);
    25. return ;
    26. }
    27. ll a[7][7]={
    28. {0,2,0,0,0,0,0},
    29. {1,1,0,0,0,0,0},
    30. {0,1,1,0,0,0,0},
    31. {0,0,4,1,0,0,0},
    32. {0,0,6,3,1,0,0},
    33. {0,0,4,3,2,1,0},
    34. {0,0,1,1,1,1,1},
    35. };
    36. ll f3[7][7];
    37. memset(f3,0,sizeof f3);
    38. f3[0][0]=aa;
    39. f3[0][1]=bb;
    40. f3[0][2]=81;
    41. f3[0][3]=27;
    42. f3[0][4]=9;
    43. f3[0][5]=3;
    44. f3[0][6]=1;
    45. ll k=n-3;
    46. while(k){
    47. if(k&1)mul(f3,f3,a);
    48. mul(a,a,a);
    49. k>>=1;
    50. }
    51. ll ans=(2*f3[0][0]+f3[0][1]+f3[0][2])%mod;
    52. printf("%lld\n",ans);
    53. }
    54. int main(){
    55. int t;
    56. scanf("%d",&t);
    57. while(t--){
    58. sove();
    59. }
    60. return 0;
    61. }

  • 相关阅读:
    C# 启动自己的服务
    淘宝官方订单API接口,商品分类属性
    935. 骑士拨号器
    springcloud16:总结配置中心+消息中心总结篇
    台式电脑一键重装Win10系统详细教程
    关于C#导出Word时报错“{00020970-0000-0000-C000-000000000046}加载类型库/DLL 时出错”的解决办法
    百度文心大模型4.0发布,对标GPT-4
    高效背单词——单词APP安利
    培养核心人才,落实IPD体系
    Codeforces Round #796 (Div. 2) A. Cirno‘s Perfect Bitmasks Classroom
  • 原文地址:https://blog.csdn.net/qq_61903556/article/details/127927086