HDU1757 A Simple Math Problem
2016-03-17 18:23
323 查看
A Simple Math Problem
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3821 Accepted Submission(s): 2314
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each case, output f(k) % m in one line.
[align=left]Sample Input[/align]
10 9999
1 1 1 1 1 1 1 1 1 1
20 500
1 0 1 0 1 0 1 0 1 0
[align=left]Sample Output[/align]
45
104
[align=left]Author[/align]
linle
[align=left]Source[/align]
2007省赛集训队练习赛(6)_linle专场
[align=left]Recommend[/align]
lcy | We have carefully selected several similar problems for you: 1575 1588 3117 2276 2256
#include <iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #include<queue> #include<stack> #include<vector> #include<map> #define LL long long using namespace std; int n,m; struct Matrix { int x,y; int A[11][11]; void Clean() { x=y=0; memset(A,0,sizeof(A)); } Matrix operator + (const Matrix& b)const { Matrix tmp; tmp.x=x,tmp.y=y; for(int i=0;i<x;i++) { for(int j=0;j<y;j++) { tmp.A[i][j]=A[i][j]+b.A[i][j]; } } return tmp; } Matrix operator -(const Matrix& b)const { Matrix tmp; tmp.x=x;tmp.y=y; for(int i=0;i<x;i++) { for(int j=0;j<y;j++) { tmp.A[i][j]=A[i][j]-b.A[i][j]; } } return tmp; } Matrix operator *(const Matrix& b)const { Matrix tmp; tmp.Clean(); tmp.x=x;tmp.y=b.y;//x代表行数,y代表列数; for(int i=0;i<x;i++) { for(int j=0;j<tmp.y;j++) { for(int k=0;k<x;k++) { (tmp.A[i][j]+=A[i][k]*b.A[k][j])%=m; } } } return tmp; } }S; int Matrixpower(int n) { Matrix ans; ans.Clean(); ans.x=10; ans.y=1; for(int i=0;i<10;i++) { ans.A[i][0]=i; } S.Clean(); S.x=10,S.y=10; for(int i=0;i<9;i++) { S.A[i][i+1]=1; } for(int i=9;i>=0;i--) { scanf("%d",&S.A[9][i]); } while(n) { if(n&1) { ans=S*ans; } S=S*S; n>>=1; } return ans.A[9][0]; } int main() { while(~scanf("%d%d",&n,&m)) { if(n<10) printf("%d\n",n%m); else printf("%d\n",Matrixpower(n-9)); } return 0; }
相关文章推荐
- 页表到底占用多少空间?一些疑惑和思考
- 本地计算机 上的 Redis Server 服务启动后停止
- 对象的深拷贝-序列化拷贝
- 《统计学习方法》李航著——第一章学习笔记
- Python核心编程笔记----Python对象
- 【Data Algorithms_Recipes for Scaling up with Hadoop and Spark】Chapter 7 Market Basket Analysis
- iOS9 SDK新特性
- redis学习笔记3--基本操作1
- SpringBoot接口服务处理Whitelabel Error Page
- 开启elastic search 脚本
- 网络通信编程
- Handler、Message、MessageQueue、 Looper、Handler Thread
- POJ 1287 Networking(最小生成树)
- iOS8 SDK新特性
- swift开发笔记23 引入百度地图
- Android&IOS实时抓包
- 杭电2068 RPG的错排
- "SetDestination" can only be called on an active agent that has been placed on a NavMesh.
- android自定义控件
- android学习路线