前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >hdu------(1757)A Simple Math Problem(简单矩阵快速幂)

hdu------(1757)A Simple Math Problem(简单矩阵快速幂)

作者头像
Gxjun
发布2018-03-26 15:22:47
6820
发布2018-03-26 15:22:47
举报
文章被收录于专栏:mlml

A Simple Math Problem

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

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

Author

linle

Source

2007省赛集训队练习赛(6)_linle专场

 代码:

代码语言:javascript
复制
 1 //#define LOCAL
 2 #include<cstdio>
 3 #include<cstring>
 4 #define LL __int64
 5 using namespace std;
 6 const int maxn=10;
 7 LL k,m;
 8 int aa[maxn],mat[maxn][maxn];
 9 int ans[maxn][maxn];
10 
11 void init()
12 {
13     for(int i=0;i<10;i++)
14     {
15      for(int j=0;j<10;j++)
16      {
17        if(i==0)
18          mat[i][j]=aa[j];
19        else
20           if(i==j+1)mat[i][j]=1;
21        else mat[i][j]=0;
22        if(i==j)
23            ans[i][j]=1;
24        else ans[i][j]=0;
25      }
26     }
27 }
28 
29 void Matrix(int a[][10],int b[][10])
30 {
31 
32      int c[10][10];
33      for(int i=0;i<10;i++)
34      {
35          for(int j=0;j<10;j++)
36         {
37             c[i][j]=0;
38           for(int k=0;k<10;k++)
39            {
40             c[i][j]=(c[i][j]+a[i][k]*b[k][j])%m;
41           }
42         }
43      }
44     for(int i=0;i<10;i++)
45     {
46       for(int j=0;j<10;j++)
47       {
48         a[i][j]=c[i][j];
49       }
50     }
51 }
52 
53 void pow(LL n)
54 {
55     while(n>0)
56     {
57         if(n&1) Matrix(ans,mat);
58         n>>=1L;
59         if(n==0)break;
60         Matrix(mat,mat);
61     }
62 }
63 
64 int main()
65 {
66     #ifdef LOCAL
67      freopen("test.in","r",stdin);
68     #endif
69 
70   while(scanf("%I64d%I64d",&k,&m)!=EOF)
71   {
72        for(int i=0;i<10;i++)
73        scanf("%d",aa+i);
74      if(k<10) printf("%I64d\n",k%m);
75      else
76      {
77        init();
78        pow(k-9);
79        int res=0;
80        for(int i=0;i<10;i++)
81         res=(res+(10-i-1)*ans[0][i])%m;
82        printf("%d\n",res);
83     }
84   }
85   return 0;
86 }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014-09-24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • A Simple Math Problem
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档