A Simple Math Problem(HDU 1757 构造矩阵)
2016-05-06 12:00
381 查看
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 .
[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
![](https://images0.cnblogs.com/blog2015/720994/201505/282358068769333.jpg)
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 .
[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
![](https://images0.cnblogs.com/blog2015/720994/201505/282358068769333.jpg)
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; int k,n; struct Matrix { int mat[10][10]; }p; int aa[10]; Matrix mul(Matrix a,Matrix b) { Matrix c; for(int i=0;i<10;i++) { for(int j=0;j<10;j++) { c.mat[i][j]=0; for(int k=0;k<10;k++) c.mat[i][j]=(c.mat[i][j]+a.mat[i][k]*b.mat[k][j])%n; } } return c; } Matrix mod_pow(Matrix x,int n) { Matrix res; memset(res.mat,0,sizeof(res.mat)); for(int i=0;i<10;i++) res.mat[i][i]=1; while(n) { if(n&1) res=mul(res,x); x=mul(x,x); n>>=1; } return res; } int main() { freopen("in.txt","r",stdin); while(scanf("%d%d",&k,&n)!=EOF) { if(k<10) { printf("%d\n",k); continue; } memset(p.mat,0,sizeof(p.mat)); for(int i=0;i<10;i++) cin>>p.mat[0][i]; for(int i=1;i<10;i++) p.mat[i][i-1]=1; Matrix ans= mod_pow(p,k-9); /*for(int i=0;i<10;i++) { for(int j=0;j<10;j++) cout<<ans.mat[i][j]<<" "; cout<<endl; }*/ int sum=0; for(int i=0;i<10;i++) sum+=ans.mat[0][i]*(9-i); printf("%d\n",sum%n); } }
相关文章推荐
- 模板式编程和钩子式编程
- 允许root用户登录ssh(Ubuntu)
- Android Studio教程从入门到精通
- iOS开发系列--无限循环的图片浏览器
- 用tornado实现websocket
- iOS socket编程
- Dubbo源码分析(二):Dubbo中采用的设计模式
- 怎么在eclipse中安装properties插件(国际化使用)
- ActiveMQ实现负载均衡+高可用部署方案
- 数据字典中的数据类型与ABAP中的中数据类型对应关系
- 设计模式之抽象工厂模式(AbstructFactory)
- iOS开发系列--IOS程序开发概览
- iOS post上传图片
- Android自定义控件进阶篇(一)
- 多通道图像遍历
- java枚举类型enum的使用
- Android Design Support Library初探,NavigationView实践
- android Service 的简单使用
- Imagetrick 漏洞的实例
- 把开源项目打包成jar包导入到本地仓库中