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. 
OutputFor 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

思路:矩阵太大,用5*5的代为表达吧~

#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define debug(a,i) cout<<#a<<"["<<i<<"] = "<<a[i]<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 100086;
const int maxm = 100086;
const int inf = 2.1e9;
const ll Inf = 999999999999999999;
int mod;
const double eps = 1e-6;
const double pi = acos(-1);struct Matrix{int mp[15][15];
};
Matrix mul(Matrix a,Matrix b,int n){Matrix ans;for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){ans.mp[i][j]=0;for(int k=1;k<=n;k++){ans.mp[i][j]+=a.mp[i][k]*b.mp[k][j];}ans.mp[i][j]%=mod;}}return ans;
}Matrix q_pow(Matrix a,int b,int n){Matrix ans;memset(ans.mp,0,sizeof(ans.mp));for(int i=1;i<=n;i++){ans.mp[i][i]=1;}while (b){if(b&1){ans=mul(ans,a,n);}b>>=1;a=mul(a,a,n);}return ans;
}int main()
{
//    ios::sync_with_stdio(false);
//    freopen("in.txt","r",stdin);
Matrix exa;int n;while(scanf("%d%d",&n,&mod)!=EOF){memset(exa.mp,0,sizeof(exa.mp));for(int i=1;i<=10;i++){scanf("%d",&exa.mp[1][i]);}if(n<10){printf("%d\n",n);continue;}for(int i=2;i<=10;i++){exa.mp[i][i-1]=1;}exa = q_pow(exa,n-9,10);ll ans=0;for(int i=1;i<=10;i++){ans+=(10-i)*exa.mp[1][i];//注意这里是10-ians%=mod;}printf("%lld\n",ans);}return 0;
}

View Code

转载于:https://www.cnblogs.com/ZGQblogs/p/10880578.html

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

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

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

  2. 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 ...

  3. 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) + -- ...

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

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

  5. hdu 1757 A Simple Math Problem 构造矩阵

    题意:函数f(x), 若 x < 10 f(x) = x. 若 x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + ...

  6. HDU 1757 A Simple Math Problem

    Problem Description Lele now is thinking about a simple function f(x). If x < 10 f(x) = x. If x & ...

  7. 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 ...

  8. 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 ...

  9. HDU - 5974 A Simple Math Problem 题解

    D - Simple Math Problem 戳上方进入原题哟~ 题目大意 给一个数 a,b. 让你求满足一下条件的 X,Y: X+Y = a LCM(X,Y) = b 解析 题目给出时间限制1s, ...

最新文章

  1. 怎么用计算机实现矩阵摹乘法,基于距离矩阵摹乘法的生鲜产品配送路径优化
  2. PMCAFF微课堂 | 产品汪如何做好“时间管理”让效率提高100倍
  3. Import project出现Select at least one project的解决方法
  4. lamda获取参数集合去空_(转)Java8使用lambda表达式进行集合的遍历
  5. 16个简单实用的.htaccess技巧
  6. Python产生随机数(转)
  7. Digits of Factorial LightOJ - 1045(数学题?)
  8. c语言中定义宏的利与弊
  9. SpringBoot 自定义监听器(Listener)
  10. Atitit  jdbc 处理返回多个结果集
  11. 3GP转换器 Pro 3.5
  12. 注册和登录Github
  13. 单位元转换万元怎么转_元换算万元单位(元与万元的换算器)
  14. 破解双中台困局:万家数科 x StarRocks 数字化技术实践
  15. css中的counter计数器
  16. Android之设置WebView的UserAgent
  17. 一切恍如昨日,却又截然不同...
  18. 再补充三种内网穿透的方法:ngrok 、ngrok.cc、IOEE
  19. 最热开源实时提升动画画质MPV+Anime4K使用方法
  20. 刷脸支付智慧零售解决方案迅速在线下落地

热门文章

  1. Freescale MC9S08AW60汇编学习笔记(三)
  2. 【轻院热身赛】级数求和、进制转换、candy
  3. 《Windows核心编程》---Windows服务
  4. 安全基础知识 最强0到33600端口详解
  5. JZ46把数字翻译成字符串
  6. Linux操作Oracle(7)—连接Oracle12C 或 OracleRac 出现 ORA-28040: No matching authentication protocol,没有匹配的验证协议
  7. [转]35个高级Python知识点总结
  8. as3 primitives
  9. javabean和java类_java对象 POJO和JavaBean的区别
  10. PHP 怎样理解go指针,golang指针传递和值传递的区别是什么?