矩阵快速幂大合集
来源:互联网 发布:阿里云系统电视好不好 编辑:程序博客网 时间:2024/05/22 10:53
hdu1757:
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.
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.
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 99991 1 1 1 1 1 1 1 1 120 5001 0 1 0 1 0 1 0 1 0
Sample Output
45104
求f(k);
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int N = 10;int a[N],k,m;struct mat {int g[N][N];mat operator*(mat a) {mat tmp;memset(tmp.g, 0, sizeof(tmp.g));for(int i = 0; i < 10; i++) {for(int j = 0; j < 10; j++) {for(int k = 0; k < 10; k++) {tmp.g[i][j]= (tmp.g[i][j] + this->g[i][k] * a.g[k][j]) % m;}}}return tmp;}}res,ori;void cul(int n) {while(n) {if(n & 1)res = res * ori;n >>= 1;ori = ori * ori;}printf("%d\n", res.g[0][0]);}int main() {while(scanf("%d%d",&k,&m) != EOF) {memset(res.g, 0, sizeof(res.g));memset(ori.g, 0, sizeof(ori.g));for(int i = 0; i < 10; i++) {scanf("%d",&ori.g[i][0]);res.g[0][i] = 9 - i;}for(int i = 0; i < 9; i++) {ori.g[i][i + 1] = 1;}cul(k - 9);}}
hdu1575
Description
A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973。
Input
数据的第一行是一个T,表示有T组数据。
每组数据的第一行有n(2 <= n <= 10)和k(2 <= k < 10^9)两个数据。接下来有n行,每行有n个数据,每个数据的范围是[0,9],表示方阵A的内容。
每组数据的第一行有n(2 <= n <= 10)和k(2 <= k < 10^9)两个数据。接下来有n行,每行有n个数据,每个数据的范围是[0,9],表示方阵A的内容。
Output
对应每组数据,输出Tr(A^k)%9973。
Sample Input
22 21 00 13 999999991 2 34 5 67 8 9
Sample Output
22686
求矩阵A的k次方的对角线和(模板题)
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;struct mat {int g[10][10];}res,ori;int n,k;mat mul(mat a, mat b) {mat tmp;memset(tmp.g, 0, sizeof(tmp.g));for(int i = 0; i < n; i++) {for(int j = 0; j < n; j++) {for(int k = 0; k < n; k++) {tmp.g[i][j] = (tmp.g[i][j] + a.g[i][k] * b.g[k][j]) % 9973;}}}return tmp;}void cul(int k) {while(k) {if(k & 1)res = mul(res, ori);k >>= 1;ori = mul(ori, ori);}int ans = 0;for(int i = 0; i < n; i++) {ans = (ans + res.g[i][i]) % 9973;}printf("%d\n",ans);}int main() {int t;scanf("%d",&t);while(t--) {scanf("%d%d",&n,&k);memset(res.g, 0, sizeof(res.g));for(int i = 0; i < n; i++) {for(int j = 0; j < n; j++) {scanf("%d",&res.g[i][j]);}}ori = res;cul(k - 1);}}
hdu2604
Description
Queues and Priority Queues are data structures which are known to most computer scientists. The Queue occurs often in our daily life. There are many people lined up at the lunch time.
Now we define that ‘f’ is short for female and ‘m’ is short for male. If the queue’s length is L, then there are 2 L numbers of queues. For example, if L = 2, then they are ff, mm, fm, mf . If there exists a subqueue as fmf or fff, we call it O-queue else it is a E-queue.
Your task is to calculate the number of E-queues mod M with length L by writing a program.
Now we define that ‘f’ is short for female and ‘m’ is short for male. If the queue’s length is L, then there are 2 L numbers of queues. For example, if L = 2, then they are ff, mm, fm, mf . If there exists a subqueue as fmf or fff, we call it O-queue else it is a E-queue.
Your task is to calculate the number of E-queues mod M with length L by writing a program.
Input
Input a length L (0 <= L <= 10 6) and M.
Output
Output K mod M(1 <= M <= 30) where K is the number of E-queues with length L.
Sample Input
3 84 74 8
Sample Output
621
题意:一个序列只有f和m问长度为L的序列,不含fmf和fff的有几个;
思路:
结尾总共有四种
ff,fm,mf,ff
以ff结尾的再加一位,可以推出fm结尾,即ffm(不能推出ff,因为这样就有fff了)
以fm结尾能推出mm;以mm结尾能推出mm和mf;
以mm结尾的能推出mm和mf;
然后就能构造矩阵乘:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;struct mat {int g[4][4];}res, ori;int n,m;mat mul(mat a, mat b) {mat tmp;memset(tmp.g, 0, sizeof(tmp.g));for(int i = 0; i < 4; i++) {for(int j = 0; j < 4; j++) {for(int k = 0; k < 4; k++) {tmp.g[i][j] = (tmp.g[i][j] + a.g[i][k] * b.g[k][j]) % m;}}}return tmp;}void cul(int k) {while(k) {if(k & 1)res = mul(res, ori);k >>= 1;ori = mul(ori, ori);}int ans = 0;for(int i = 0; i < 4; i++) {ans = (ans + res.g[0][i]) % m;}printf("%d\n",ans);}int main() {while(scanf("%d%d",&n,&m) != EOF) {if(n == 0) {printf("0\n");continue;}else if(n == 1) {printf("2\n");continue;}else if(n == 2) {printf("4\n");continue;}memset(ori.g, 0, sizeof(ori.g));memset(res.g, 0, sizeof(res.g));ori.g[0][3] = 1;ori.g[1][0] = 1;ori.g[2][0] = 1;ori.g[2][1] = 1;ori.g[3][2] = 1;ori.g[3][3] = 1;for(int i = 0; i < 4; i++)res.g[0][i] = 1;cul(n - 2);}}
hdu2256
#include<cstdio>#include<cstring>#include<cmath>struct mat {int g[2][2];}res, ori;int n;mat mul(mat a,mat b) {mat tmp;memset(tmp.g, 0, sizeof(tmp.g));for(int i = 0; i < 2; i++) {for(int j = 0; j < 2; j++) {for(int k = 0; k < 2; k++) {tmp.g[i][j] = (tmp.g[i][j] + a.g[i][k] * b.g[k][j])%1024;}}}return tmp;}void cul(int k) {while(k) {if(k & 1)res = mul(res, ori);k >>= 1;ori = mul(ori, ori);}int ans = (res.g[0][0] * 5 + res.g[0][1] * 2) %1024;printf("%d\n",(ans * 2 - 1) % 1024);}int main() {int t;scanf("%d",&t);while(t--) {scanf("%d",&n);if(n == 1) {printf("9\n");continue;}ori.g[0][0] = 5;ori.g[0][1] = 12;ori.g[1][0] = 2;ori.g[1][1] = 5;res = ori;cul(n - 2);}}
hdu2276
#include<cstdio>#include<cstring>#define ll long longstruct mat {int g[105][105];}res, ori;int n, len;char str[105];char change[2] = {'1','0'};mat mul(mat a, mat b) {mat tmp;memset(tmp.g, 0, sizeof(tmp.g));for(int i = 0; i < len; i++) {for(int j = 0; j < len; j++) {tmp.g[0][i] = (tmp.g[0][i] + a.g[0][j] * b.g[j][i]) % 2;}}return tmp;}mat mul2(mat a, mat b) {mat tmp;memset(tmp.g, 0, sizeof(tmp.g));for(int i = 0; i < len; i++) {for(int j = 0; j < len; j++) {for(int k = 0; k < len; k++) {tmp.g[i][j] = (tmp.g[i][j] + a.g[i][k] * b.g[k][j]) % 2;}}}return tmp;}void cul(int k){while(k) {if(k & 1)res = mul(res, ori);k >>= 1;ori = mul2(ori, ori);}for(int i = 0; i <len; i++) {printf("%d",res.g[0][i]);}printf("\n");}int main() {while(scanf("%d",&n) != EOF) {scanf("%s",str);len = strlen(str);memset(ori.g, 0, sizeof(ori.g));memset(res.g, 0, sizeof(res.g));for(int i = 0; i < len; i++) {res.g[0][i] = str[i] - '0';}for(int i = 0; i < len; i++) {if(i == 0)ori.g[i][i] = ori.g[len - 1][i] = 1;elseori.g[i][i] = ori.g[i - 1][i] = 1;}cul(n);}}
hdu4686
Description
An Arc of Dream is a curve defined by following function:
where
a 0 = A0
a i = a i-1*AX+AY
b 0 = B0
b i = b i-1*BX+BY
What is the value of AoD(N) modulo 1,000,000,007?
where
a 0 = A0
a i = a i-1*AX+AY
b 0 = B0
b i = b i-1*BX+BY
What is the value of AoD(N) modulo 1,000,000,007?
Input
There are multiple test cases. Process to the End of File.
Each test case contains 7 nonnegative integers as follows:
N
A0 AX AY
B0 BX BY
N is no more than 10 18, and all the other integers are no more than 2×10 9.
Each test case contains 7 nonnegative integers as follows:
N
A0 AX AY
B0 BX BY
N is no more than 10 18, and all the other integers are no more than 2×10 9.
Output
For each test case, output AoD(N) modulo 1,000,000,007.
Sample Input
11 2 34 5 621 2 34 5 631 2 34 5 6
Sample Output
41341902
#include<cstdio>#include<cstring>#define ll long longconst ll mod = 1000000007;struct mat{ll g[5][5];}res, ori;mat mul(mat a, mat b) {mat tmp;memset(tmp.g, 0, sizeof(tmp.g));for(int i = 0; i < 5; i++) {for(int j = 0; j < 5; j++) {for(int k = 0; k < 5; k++) {tmp.g[i][j] = (tmp.g[i][j] + a.g[i][k] * b.g[k][j])%mod;}}}return tmp;}void cul(ll k){while(k) {if(k & 1)res = mul(res, ori);k >>= 1;ori = mul(ori, ori);}printf("%lld\n",res.g[0][0]);}ll a0,ax,ay;ll b0,bx,by;ll n;int main() {while(scanf("%lld",&n) != EOF) {memset(res.g,0,sizeof(res.g));memset(ori.g,0,sizeof(ori.g));scanf("%lld%lld%lld%lld%lld%lld",&a0,&ax,&ay,&b0,&bx,&by);res.g[0][1]=a0 * b0 % mod; res.g[0][2]=a0 % mod; res.g[0][3]=b0 % mod; res.g[0][4]=1; ori.g[0][0]=1; ori.g[1][0]=1;ori.g[1][1]=(ax * bx) % mod; ori.g[2][1]=ax * by % mod;ori.g[2][2]=ax % mod; ori.g[3][1]=bx * ay % mod;ori.g[3][3]=bx % mod; ori.g[4][1]=ay * by % mod;ori.g[4][2]=ay % mod;ori.g[4][3]=by % mod;ori.g[4][4]=1; cul(n); } return 0;}
zoj3690
#include<cstdio>#include<cstring>#define ll long longstruct mat {ll g[2][2];}res, ori, ans;ll n,m,k;const ll M = 1000000007;mat mul(mat a, mat b) {mat tmp;memset(tmp.g, 0, sizeof(tmp.g));for(int i = 0; i < 2; i++) {for(int j = 0; j < 2; j++) {for(int k = 0; k < 2; k++) {tmp.g[i][j] = (tmp.g[i][j] + a.g[i][k] * b.g[k][j]) % M;}}}return tmp;}void cul(int l){while(l) {if(l & 1)res = mul(res, ori);l >>= 1;ori = mul(ori, ori);}ll sum = 0;sum = (sum + (res.g[0][0] * (m - k) % M)) % M;sum = (sum + (res.g[0][1] * k % M)) % M;sum = (sum + (res.g[1][0] * (m - k) % M)) % M;sum = (sum + (res.g[1][1] * k % M)) % M;printf("%lld\n",sum);}int main() {while(scanf("%lld%lld%lld",&n,&m,&k) != EOF) {memset(ori.g, 0, sizeof(ori.g));memset(res.g, 0, sizeof(res.g));ori.g[0][0] = m-k;ori.g[0][1] = m-k;ori.g[1][0] = k;ori.g[1][1] = k - 1;res.g[0][1] = res.g[1][0] = 0;res.g[0][0] = res.g[1][1] = 1;cul(n - 1);}}
FZU1683
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;struct mat{int g[4][4];}res, ori;int n;int cas = 1;mat mul(mat a, mat b) {mat tmp;memset(tmp.g, 0, sizeof(tmp.g));for(int i = 0; i < 4; i++) {for(int j = 0; j < 4; j++) {for(int k = 0; k < 4; k++) {tmp.g[i][j] = (tmp.g[i][j] + a.g[i][k] * b.g[k][j]) % 2009;}}}return tmp;}mat mul2(mat a, mat b) {mat tmp;memset(tmp.g, 0, sizeof(tmp.g));for(int i = 0; i < 4; i++) {for(int j = 0; j < 4; j++) {tmp.g[0][i] = (tmp.g[0][i] + a.g[0][j] * b.g[j][i]) % 2009;}}return tmp;}void cul(int k) {while(k) {if(k & 1)res = mul2(res,ori);k >>= 1;ori = mul(ori, ori);}printf("Case %d: %d\n",cas++,res.g[0][3]);}int main() {int t;scanf("%d",&t);while(t--) {scanf("%d",&n);if(n == 0) {printf("Case %d: 1\n",cas++);continue;}else if(n == 1) {printf("Case %d: 4\n",cas++);continue;}else if(n == 2) {printf("Case %d: 9\n",cas++);continue;}memset(res.g, 0, sizeof(res.g));memset(ori.g, 0, sizeof(ori.g));res.g[0][0] = 5, res.g[0][1] = 3, res.g[0][2] = 1, res.g[0][3] = 9;ori.g[0][0] = 3, ori.g[1][0] = 2, ori.g[2][0] = 7;ori.g[0][1] = 1, ori.g[1][2] = 1;ori.g[0][3] = 3;ori.g[1][3] = 2;ori.g[2][3] = 7;ori.g[3][3] = 1;cul(n - 2);}return 0;}
hdu3306
#include<cstdio>#include<cstring>#define ll long longstruct mat {ll g[4][4];}res, ori, ans;ll n,x,y;mat mul(mat a, mat b) {mat tmp;memset(tmp.g, 0, sizeof(tmp.g));for(int i = 0; i < 4; i++) {for(int j = 0; j < 4; j++) {for(int k = 0; k < 4; k++) {tmp.g[i][j] = (tmp.g[i][j] + a.g[i][k] * b.g[k][j]) % 10007;}}}return tmp;}void cul(ll k){while(k) {if(k & 1)res = mul(res, ori);k >>= 1;ori = mul(ori, ori);}printf("%lld\n",res.g[0][0]);}int main() {while(scanf("%lld%lld%lld",&n,&x,&y) != EOF) {memset(ori.g, 0, sizeof(ori.g));memset(res.g, 0, sizeof(res.g));ori.g[0][0] = 1;ori.g[1][0] = 1;ori.g[1][1] = (x * x) % 10007;ori.g[1][2] = 1;ori.g[1][3] = x % 10007;ori.g[2][1] = (y * y) % 10007;ori.g[3][1] = (2 * x * y) % 10007;ori.g[3][3] = y % 10007;res.g[0][0] = 1;res.g[0][1] = 1;res.g[0][2] = 1;res.g[0][3] = 1;cul(n);}}
hdu4565
#include<cstdio>#include<cstring>#define ll long longstruct mat {ll g[2][2];}res, ori;ll a,b, m;ll n;mat mul(mat c, mat d) {mat tmp;memset(tmp.g, 0, sizeof(tmp.g));for(int i = 0; i < 2; i++) {for(int j = 0; j < 2; j++) {for(int k = 0; k < 2; k++) {tmp.g[i][j] = (tmp.g[i][j] + c.g[i][k] * d.g[k][j]) % m;}}}return tmp;}void cul(ll k) {while(k) {if(k & 1)res = mul(res, ori);k >>= 1;ori = mul(ori, ori);}ll ans =(res.g[0][0] * 2)% m;printf("%lld\n",ans);}int main() {while(scanf("%lld%lld%lld%lld",&a,&b,&n,&m) != EOF) {memset(ori.g, 0, sizeof(ori.g));ori.g[0][0] = a;ori.g[0][1] = b;ori.g[1][0] = 1;ori.g[1][1] = a;res = ori;cul(n - 1);}}
0 0
- 矩阵快速幂大合集
- 快速矩阵快速幂
- 转移矩阵+矩阵快速幂
- 矩阵乘法 矩阵快速幂
- 构造矩阵+矩阵快速幂
- 矩阵快速幂,矩阵加法,矩阵乘法
- 快速幂和快速矩阵
- 快速幂||矩阵快速幂
- 快速幂&矩阵快速幂
- 快速幂,矩阵快速幂
- 快速幂 矩阵快速幂
- 快速幂&矩阵快速幂
- 【快速幂】【矩阵快速幂】
- poj3070--矩阵 快速幂
- POJ3233矩阵快速幂
- poj3070Fibonacci 矩阵快速幂
- 矩阵的快速幂
- 矩阵快速幂
- nginx rewrite 参数和例子
- sqlselect内的执行顺序
- sqlselect内的执行顺序
- Cuda kernel failed. Error: invalid device function
- Android 让EditText失去焦点避免自动弹出输入法
- 矩阵快速幂大合集
- 为什么选择Scala,它在大数据处理方面有何优势?
- LeetCode的medium题集合(C++实现)十七
- 九宫格的计算思路
- spring mvc结合freemarker,使用hibernate validation框架做校验及国际化
- 读浅墨博客 十三 笔记
- 海马汽车经销商管理系统技术解析(四)保养管理
- ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务
- adb Connection is down解决方案