zoukankan      html  css  js  c++  java
  • 8.3.2 A Simple Math Problem

    A Simple Math Problem

    Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 44 Accepted Submission(s): 42

    Problem Description
    Lele now is thinking about a simple function f(x).

    If x < 10 f(x) = x.
    If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10);
    And ai(0<=i<=9) can only be 0 or 1 .

    Now, I will give a0 ~ a9 and two positive integers k and m ,and could you help Lele to caculate f(k)%m.
     

    Input
    The problem contains mutiple test cases.Please process to the end of file.
    In each case, there will be two lines.
    In the first line , there are two positive integers k and m. ( k<2*10^9 , m < 10^5 )
    In the second line , there are ten integers represent a0 ~ a9.
     

    Output

                For each case, output f(k) % m in one line.
     

    Sample Input
    10 9999
    1 1 1 1 1 1 1 1 1 1
    20 500
    1 0 1 0 1 0 1 0 1 0
     

    Sample Output
    45
    104

    思路:递推数列都可以用矩阵解的~例如一个递推数列Fn=Fn-1+Fn-2+..+Fn-k

    构造Fk,Fk+1,Fk+2..Fn-1,Fn

     --->Fk+1,Fk+2,....Fn,Fn+1即可……

     1 #include <cmath>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cstring>
     5 #include <string>
     6 #include <cstdlib>
     7 using namespace std;
     8 
     9 typedef long long ll;
    10 const int maxn=10;
    11 struct matrix
    12 {
    13     int m[maxn][maxn];
    14 };
    15 matrix c,base,ans;
    16 int n,k,mod,sum,x;
    17 int a[maxn];
    18 
    19 
    20 void close()
    21 {
    22 exit(0);
    23 }
    24 
    25 matrix mul(matrix a,matrix b)
    26 {
    27     memset(c.m,0,sizeof(c.m));
    28     for (int i=0;i<=9;i++)
    29         for (int j=0;j<=9;j++)
    30             for (int k=0;k<=9;k++)
    31                 c.m[i][j]=(c.m[i][j]+(ll)a.m[i][k]*b.m[k][j]) % mod;
    32     return c;
    33 }
    34 
    35 void print(matrix a)
    36 {
    37     for (int i=0;i<=9;i++)
    38     {
    39         for (int j=0;j<=9;j++)
    40             printf("%d ",a.m[i][j]);
    41         printf("
    ");
    42     }
    43 }
    44 
    45 void work()
    46 {
    47     k-=9;
    48     memset(ans.m,0,sizeof(ans.m));
    49     for (int i=0;i<=9;i++)
    50         ans.m[i][i]=1;
    51     while (k!=0)
    52     {
    53         if (k & 1)
    54             ans=mul(base,ans);
    55         k/=2;
    56         base=mul(base,base);
    57     }
    58     sum=0;
    59     for (int i=0;i<=9;i++)
    60         sum=(sum + (ll)(i)*ans.m[9][i]) % mod;
    61     printf("%d
    ",sum);
    62 }
    63 
    64 
    65 void init()
    66 {
    67     while(scanf("%d %d",&k,&mod)!=EOF)
    68     {
    69         if (k<=9)
    70         {
    71             printf("%d
    ",k);
    72             continue;
    73         }
    74         for (int i=0;i<=9;i++)
    75             scanf("%d",&a[9-i]);
    76         memset(base.m,0,sizeof(base.m));
    77         for (int i=0;i<=8;i++)
    78             base.m[i][i+1]=1;
    79         for (int i=0;i<=9;i++)
    80             base.m[9][i]=a[i];
    81         work();
    82     }
    83 }
    84 
    85 int main ()
    86 {
    87     init();
    88     close();
    89     return 0;
    90 }
  • 相关阅读:
    Javascript
    CSS3新增特性HTML标签类型
    HTML5新增的标签
    prototype __proto__ Function
    oninput
    extend
    hasOwnProperty()
    this prototype constructor
    Array类型判断
    指针
  • 原文地址:https://www.cnblogs.com/cssystem/p/3335268.html
Copyright © 2011-2022 走看看