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

矩阵快速幂的应用,还不怎么会用。。。
代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int k,m;
struct Maxtri
{int r,c;int a[15][15];
};
Maxtri ori,res;
void init()
{memset(res.a,0,sizeof(res.a));for(int i=1;i<=10;i++)res.a[i][i]=1;for(int i=2;i<=10;i++)for(int j=1;j<=10;j++){if(i-1!=j)ori.a[i][j]=0;elseori.a[i][j]=1;}
}
Maxtri maxtri(Maxtri x,Maxtri y)
{Maxtri z;memset(z.a,0,sizeof(z.a));for(int i=1;i<=10;i++){for(int k=1;k<=10;k++){if(x.a[i][k]==0)    continue;for(int j=1;j<=10;j++)z.a[i][j]=(z.a[i][j]+(x.a[i][k]*y.a[k][j])%m)%m;//这一点取模一定要注意,错了n遍 }}return z;
}
void maxtri_mod(int n)
{while(n){if(n%2!=0)res=maxtri(res,ori);ori=maxtri(ori,ori);n/=2;}
}
int main()
{while(scanf("%d%d",&k,&m)!=EOF){for(int i=1;i<=10;i++)scanf("%d",&ori.a[1][i]);init();if(k<=9){printf("%d\n",k%m);continue;}maxtri_mod(k-9);int sum=0;for(int i=1;i<=10;i++){sum+=(res.a[1][i]*(10-i))%m;sum%=m;}printf("%d\n",sum%m);}return 0;
}

努力加油a啊,(o)/~

A Simple Math Problem(矩阵快速幂)相关推荐

  1. HDU - 1757 A Simple Math Problem(矩阵快速幂,水题)

    题目链接:点击查看 题目大意:实现公式: f(x)=x,x<10 f(x)=a0*f(x-1)+a1*f(x-2)+--+a9*f(x-10) 题目给出a0~a9,一个n和一个m,要求输出f(n ...

  2. HDU 1757 A Simple Math Problem (矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 在吴神的帮助下才明白如何构造矩阵,还是好弱啊. 此处盗一张图 1 #include <io ...

  3. DUToj1085 Water Problem(矩阵快速幂)

    Problem I: Water Problem Time Limit:3000/1000 MS (Java/Others)   Memory Limit:163840/131072 KB (Java ...

  4. A Simple Math Problem 矩阵打水题

    A Simple Math Problem Lele now is thinking about a simple function f(x). If x < 10 f(x) = x. If x ...

  5. HDU 4291 A Short problem 矩阵快速幂 循环节

    题解思路: 构造矩阵,矩阵乘法计算还是t; 需要找循环节;   (注意因为是复合函数,不可以在里面取mod) 暴力跑只有可以找到g(222222224)%1e9==g(0)%1e9; 所以 g(g(n ...

  6. 【HDU - 1757】A Simple Math Problem (矩阵快速幂)

    题干: Lele now is thinking about a simple function f(x). If x < 10 f(x) = x.  If x >= 10 f(x) = ...

  7. 43行代码AC——HDU 1757 A Simple Math Problem(矩阵快速幂,附快速幂讲解)

    一道经典的矩阵快速幂模板题. 传送门1-->快速幂基本思想 传送门2-->矩阵快速幂讲解(教主传授) 代码(去掉空行43行) #include<iostream> #inclu ...

  8. HDOJ 1757 A Simple Math Problem(矩阵快速幂)

    2018-5-24 简单的矩阵快速幂问题,重点是如何找到对应关系. f(10) a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 f(9) f(9) 1 0 0 0 0 0 0 0 0 0 ...

  9. HDU 1757 A Simple Math Problem(矩阵快速幂)

    题目链接 题意 :给你m和k, 让你求f(k)%m.如果k<10,f(k) = k,否则 f(k) = a0 * f(k-1) + a1 * f(k-2) + a2 * f(k-3) + -- ...

最新文章

  1. java中class对象的理解 讲得相当不错 很接地气 引用下
  2. 批量导入导出站点权限site permissions
  3. auto-sklearn简介
  4. 8-1日复习 模板函数 模板类
  5. Serverless对研发效能的变革和创新
  6. Spring boot 中pom.xml 各个节点详解
  7. Effective C++ 条款03:尽可能使用const
  8. 《精通并发与Netty》学习笔记(02 - 服务端程序编写)
  9. 用delphi模仿.net的string.split
  10. C/C++[codeup 1931]打印日期,一年的第n天是几月几号
  11. 计算机操作系统慕课版(汤小丹)--第一章课后题
  12. Android 退出登录功能
  13. redis的安装,配置
  14. 2020年茶艺师(初级)考试及茶艺师(初级)实操考试视频
  15. PPT怎么调计算机,PPT演示者模式怎么设置
  16. EmbedTLS + Eclipse C/C++测试用例SSL客户端和服务器
  17. 应届生简历怎么写?应届生制作简历注意事项有哪些?
  18. 详解:智能医学影像分析的前沿与挑战
  19. VISTA或WIN7下使用立体声混音作为录音来源时,如何通过编程方式去除或加入麦克风的输入
  20. tensorflow分类图片预处理

热门文章

  1. swift5的SnipKit框架使用
  2. Base64加密和Md5加密用户名
  3. 解决vue中路由跳转同一个路径报错
  4. java sql数组_Sql数组类型解决方案
  5. affine工程难点、亮点汇总
  6. 如何禁用ALT+CLT+DEL组合键
  7. Android开发之The application could not be installed: INSTALL_FAILED_VERSION_DOWNGRADE报错
  8. Android开发之API29以上Environment.getExternalStoragePublicDirectory废弃的问题
  9. java dataset flatmap_Flink 系例 之 FlatMap
  10. php断点调试的几种方法