hdu1757 A Simple Math Problem【矩阵快速幂】
2017-06-05 13:37
603 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757
题意:题目有一个表达式f(x),让你求他的第k项,即f(k)%m
解析:应该就是一个矩阵快速幂了,构造一个矩阵,草稿纸上算一下不难得出这个矩阵
[0,1,0,0,0,0,0,0,0,0
0,0,1,0,0,0,0,0,0,0
0,0,0,1,0,0,0,0,0,0
0,0,0,0,1,0,0,0,0,0
0,0,0,0,0,1,0,0,0,0
0,0,0,0,0,0,1,0,0,0
0,0,0,0,0,0,0,1,0,0
0,0,0,0,0,0,0,0,1,0
0,0,0,0,0,0,0,0,0,1
a9,a8,a7,a6,a5,a4,a3,a2,a1,a0]
题意:题目有一个表达式f(x),让你求他的第k项,即f(k)%m
解析:应该就是一个矩阵快速幂了,构造一个矩阵,草稿纸上算一下不难得出这个矩阵
[0,1,0,0,0,0,0,0,0,0
0,0,1,0,0,0,0,0,0,0
0,0,0,1,0,0,0,0,0,0
0,0,0,0,1,0,0,0,0,0
0,0,0,0,0,1,0,0,0,0
0,0,0,0,0,0,1,0,0,0
0,0,0,0,0,0,0,1,0,0
0,0,0,0,0,0,0,0,1,0
0,0,0,0,0,0,0,0,0,1
a9,a8,a7,a6,a5,a4,a3,a2,a1,a0]
#include <bits/stdc++.h> using namespace std; int k,mod; struct matrix { long long a[15][15]; int n,m; matrix() {} matrix(int _n,int _m) { n = _n,m = _m; memset(a,0,sizeof(a)); } matrix operator * (const matrix &b)const { matrix res(n,b.m); for(int i=0;i<n;i++) { for(int j=0;j<b.m;j++) { for(int k=0;k<m;k++) res.a[i][j] = (res.a[i][j]+a[i][k]*b.a[k][j])%mod; } } return res; } void print() { for(int i=0;i<n;i++) { for(int j=0;j<m;j++) printf("%I64d ",a[i][j]); puts(""); } } }; matrix qpow(matrix x,int n) { matrix res(x.n,x.m); for(int i=0;i<res.n;i++) res.a[i][i] = 1; while(n>0) { if(n&1) res = res*x; x = x*x; n >>= 1; } return res; } int main(void) { while(~scanf("%d %d",&k,&mod)) { matrix op(10,10); matrix ans(10,1); for(int i=0;i<10;i++) ans.a[i][0] = i; for(int i=0;i<9;i++) op.a[i][i+1] = 1; for(int i=9;i>=0;i--) scanf("%I64d",&op.a[9][i]); //op.print(); op = qpow(op,k); //op.print(); ans = op*ans; //ans.print(); printf("%I64d\n",ans.a[0][0]); } return 0; }
相关文章推荐
- hdu1757 A Simple Math Problem 矩阵快速幂 水题
- hdu1757 A Simple Math Problem (矩阵快速幂)
- Hdu1757 - A Simple Math Problem - 矩阵快速幂
- [HDU1757]A Simple Math Problem(矩阵快速幂)
- hdu1757 A Simple Math Problem(矩阵快速幂题解)
- hdu1757 A Simple Math Problem(矩阵快速幂)
- hdu1757 - A Simple Math Problem 矩阵快速幂
- HDU1757-A Simple Math Problem(矩阵快速幂)
- hdu1757 - A Simple Math Problem 矩阵快速幂
- HDU1757-A Simple Math Problem,矩阵快速幂,构造矩阵水过
- hdu1757-- A Simple Math Problem(矩阵快速幂优化)
- 【矩阵快速幂】 hdu1757 A Simple Math Problem
- HDU1757:A Simple Math Problem(矩阵快速幂)
- hdu1757 A Simple Math Problem(矩阵快速幂)
- hdu 1757 A Simple Math Problem 矩阵快速幂
- hdu 1757 A Simple Math Problem(矩阵快速幂)
- 2017 Wuhan University Programming Contest 现场赛 I. A simple math problem(矩阵快速幂)
- hdu 1757 A Simple Math Problem(矩阵快速幂)
- HDU 1757 A Simple Math Problem(矩阵快速幂)
- hdu 1757 A Simple Math Problem【矩阵快速幂】