• Yet another Number Sequence


     Yet another Number Sequence

    Let’s define another number sequence, given by the following function:
    f(0) = a
    f(1) = b
    f(n) = f(n − 1) + f(n − 2), n > 1
    When a = 0 and b = 1, this sequence gives the Fibonacci Sequence. Changing the values of a and
    b, you can get many different sequences. Given the values of a, b, you have to find the last m digits of
    f(n).
    Input
    The first line gives the number of test cases, which is less than 10001. Each test case consists of a
    single line containing the integers a b n m. The values of a and b range in [0, 100], value of n ranges in
    [0, 1000000000] and value of m ranges in [1, 4].
    Output
    For each test case, print the last m digits of f(n). However, you should NOT print any leading zero.
    Sample Input
    4
    0 1 11 3
    0 1 42 4
    0 1 22 4
    0 1 21 4
    Sample Output
    89
    4296
    7711
    946

    题意:给出a,b,n,m;a为第一个数,b为第二个数,然后以a,b为前两项a[n+1]=a[n]+a[n-1];构造数列

    m为去几位数,即%10的几次方;n为此数列的第几个数

    代码:

     1 #include<iostream>
     2 #include<cstdio>
     3 
     4 using namespace std;
     5 
     6 const int mod[5]={0,10,100,1000,10000};
     7 
     8 #define ll long long
     9 
    10 ll a,b,m,n;
    11 
    12 struct matrix
    13 {
    14     int map[2][2];
    15 }ans,base;
    16 
    17 matrix unit={1,0,0,1};
    18 
    19 matrix multi(matrix a,matrix b)
    20 {
    21     matrix c;
    22     for(int i=0;i<2;i++)
    23     {
    24         for(int j=0;j<2;j++)
    25         {
    26             c.map[i][j]=0;
    27             for(int k=0l;k<2;k++)
    28             {
    29                 c.map[i][j]+=(a.map[i][k]*b.map[k][j])%mod[m];
    30             }
    31             c.map[i][j]%=mod[m];
    32         }
    33     }
    34     return c;
    35 }
    36 
    37 void pow(int n)
    38 {
    39     base.map[0][0]=base.map[0][1]=base.map[1][0]=1;
    40     base.map[1][1]=0;
    41     ans.map[0][0]=ans.map[1][1]=1;
    42     ans.map[0][1]=ans.map[1][0]=0;
    43     while(n)
    44     {
    45         if(n&1)
    46             ans=multi(ans,base);
    47         base=multi(base,base);
    48         n>>=1;
    49     }
    50 }
    51 
    52 int main()
    53 {
    54     int t;
    55     scanf("%d",&t);
    56     while(t--)
    57     {
    58         scanf("%lld%lld%lld%lld",&a,&b,&n,&m);
    59         if(n==0)
    60             return a;
    61         else if(n==1)
    62             return b;
    63         else
    64         {
    65             pow(n-1);
    66             ll result=(ans.map[0][0]*b+ans.map[0][1]*a)%mod[m];
    67             printf("%lld
    ",result);
    68         }
    69     }
    70     return 0;
    71 }

    //很有代表性

  • 相关阅读:
    linux下ipc
    c各种打印集合
    linux环境下,利用gsoap生成webservice客户端进行应用程序开发的小结 (轉)
    Linux C函数之错误处理函数
    Linux操作系统文件系统基础知识详解
    常用字符串操作函数
    ASP.NET偷懒大法二
    ASP.NET偷懒大法三 (利用Attribute特性简化多查询条件拼接sql语句的麻烦)
    用JavaScript获取Asp.net服务器端控件CheckBoxList的选中值数组
    ASP.NET偷懒大法四(动态生成表单对象)
  • 原文地址:https://www.cnblogs.com/moqitianliang/p/4742334.html
Copyright © 2020-2023  润新知