矩阵
来源:互联网 发布:php微信报名系统源码 编辑:程序博客网 时间:2024/04/29 11:09
原题http://acm.hdu.edu.cn/showproblem.php?pid=4990
本题先把方程写出来,就是把光计算n,不管m。公式为f[n] = f[n-2]*2+f[n-1]+1;
然后用矩阵快速幂。以前用的是AAAAAB型。以后都采用ABBBB型。就把这题当做模板吧。
如何计算构建矩阵:因为要从已知的是f[n-2],f[n-],我们要得到的是新矩阵里是要f[n-1],f[n],由此来构建需要的矩阵。
因为已知的是两个数,所以只要计算n-2次就可以了
代码虐我千百遍,我待代码如初恋
Reading comprehension
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 252 Accepted Submission(s): 121
Problem Description
Read the program below carefully then answer the question.
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include<iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include<vector>
const int MAX=100000*2;
const int INF=1e9;
int main()
{
int n,m,ans,i;
while(scanf("%d%d",&n,&m)!=EOF)
{
ans=0;
for(i=1;i<=n;i++)
{
if(i&1)ans=(ans*2+1)%m;
else ans=ans*2%m;
}
printf("%d\n",ans);
}
return 0;
}
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include<iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include<vector>
const int MAX=100000*2;
const int INF=1e9;
int main()
{
int n,m,ans,i;
while(scanf("%d%d",&n,&m)!=EOF)
{
ans=0;
for(i=1;i<=n;i++)
{
if(i&1)ans=(ans*2+1)%m;
else ans=ans*2%m;
}
printf("%d\n",ans);
}
return 0;
}
Input
Multi test cases,each line will contain two integers n and m. Process to end of file.
[Technical Specification]
1<=n, m <= 1000000000
[Technical Specification]
1<=n, m <= 1000000000
Output
For each case,output an integer,represents the output of above program.
Sample Input
1 103 100
Sample Output
15
#include <stdio.h>#include <stdlib.h>#include <malloc.h>#include <ctype.h>#include <limits.h>#include <string.h>#include <string>#include <math.h>#include <algorithm>#include <iostream>#include <queue>#include <stack>#include <deque>#include <vector>#include <set>#include <map>using namespace std;#define max 15struct Matrix{ __int64 m[max][max];};struct Matrix I,s;__int64 n,mod;Matrix Mul(Matrix a,Matrix b){ Matrix c; __int64 i,j,k; for(i=0;i<3;i++){ for(j=0;j<3;j++){ c.m[i][j] = 0; for(k=0;k<3;k++){ c.m[i][j]+=(a.m[i][k]*b.m[k][j]); c.m[i][j]%=mod; } } } return c;}Matrix Pow(Matrix a,__int64 n){ Matrix m,b; m = a; b = I; while(n){ if(n%2){ b = Mul(b,m); } n/=2; m = Mul(m,m); } return b;}int main(){ __int64 i,j; __int64 k,a,b; while(~scanf("%I64d%I64d",&n,&mod)){ Matrix ans,p,q; memset(I.m,0,sizeof(I.m)); memset(p.m,0,sizeof(p.m)); memset(q.m,0,sizeof(q.m)); for(i=0;i<3;i++){ I.m[i][i] = 1; } q.m[0][0] = 1; q.m[0][1] = 2; q.m[0][2] = 1; p.m[0][1] = 2; p.m[1][0] = 1; p.m[1][1] = 1; p.m[2][1] = 1; p.m[2][2] = 1; if(n == 1){ printf("%I64d\n",1%mod); } else if(n == 2){ printf("%I64d\n",2%mod); } else{ ans = Pow(p,n-2); ans = Mul(q,ans); printf("%I64d\n",ans.m[0][1]%mod); } } return 0;}
0 0
- 矩阵
- 矩阵
- 矩阵
- 矩阵
- 矩阵
- 矩阵
- 矩阵
- 矩阵
- 矩阵
- 矩阵
- 矩阵
- 矩阵
- 矩阵
- 矩阵
- 矩阵
- 矩阵
- 矩阵
- 矩阵
- 全排列
- [寒江孤叶丶的CrossApp之旅_09][入门系列]CrossApp中CAButton的使用
- 关于sql语句(较全)
- dispatch_async 与 dispatch_get_global_queue 的使用方法
- android SDK 下载列表
- 矩阵
- 在二元树中找出和为某一值的所有路径
- Match类和MatchCollection类
- awesome 对照表
- 求二叉树的深度
- 微信实现股票查询功能
- uva 11235 Frequent values(游程编码+区间最小值查询)
- mySql日期截取和格式转换
- PID5 能量项链 矩阵链乘