A Simple Math Problem(HDU 1757 构造矩阵)
2024-10-12 17:16:04
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 .
Sample Input
10 9999
1 1 1 1 1 1 1 1 1 1
20 500
1 0 1 0 1 0 1 0 1 0
Sample Output
45
104
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
int k,n;
struct Matrix
{
int mat[][];
}p;
int aa[];
Matrix mul(Matrix a,Matrix b)
{
Matrix c;
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
c.mat[i][j]=;
for(int k=;k<;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,,sizeof(res.mat));
for(int i=;i<;i++)
res.mat[i][i]=;
while(n)
{
if(n&)
res=mul(res,x);
x=mul(x,x);
n>>=;
}
return res;
}
int main()
{
freopen("in.txt","r",stdin);
while(scanf("%d%d",&k,&n)!=EOF)
{
if(k<)
{
printf("%d\n",k);
continue;
}
memset(p.mat,,sizeof(p.mat));
for(int i=;i<;i++)
cin>>p.mat[][i];
for(int i=;i<;i++)
p.mat[i][i-]=;
Matrix ans= mod_pow(p,k-);
/*for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
cout<<ans.mat[i][j]<<" ";
cout<<endl;
}*/
int sum=;
for(int i=;i<;i++)
sum+=ans.mat[][i]*(-i);
printf("%d\n",sum%n);
}
}
最新文章
- String,StringBuffer
- 最近在学习bootstrap的时候用bootstrap的视频教程2.0的引用bootstrap3.0突然发现很多不同,总结了一下
- codeforces Round #263(div2) D. Appleman and Tree 树形dp
- Android实现贪吃蛇游戏
- uva 701
- OpenStack Networking
- [Android4.4.3] Nubia Z5S Mokee4.4.3 RC2.0 by syhost
- 常用Map实现类对比
- 微信小程序开发-tabbar组件
- 使用DHCP动态管理主机地址
- pytest 2.测试用例setup和teardown
- BZOJ3531[Sdoi2014]旅行——树链剖分+线段树
- doi
- 阿里云服务器 CentOS 安装Mysql 5.6
- Vuex的深入学习
- vue的watcher 关于数组和对象
- hibernate 验证异常 javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint
- android中实现简单的聊天功能
- 字符集(编码)转换_Linux
- 随手练——HUD 2609 How many
热门文章
- 剑指offer之O(1)算法删除指针所指向的节点
- poj 3335 Rotating Scoreboard
- Keil C51 中的函数指针和再入函数
- Linux read语法及浅析
- hadoop深入研究:(十八)——Avro schema兼容
- [Tips]ASP.NET MVC 发布到服务器后Model中属性相关的Attribute失效
- 上海游侠电动汽车团队招募。iOS,Android,产品经理以及 SEVER 端工程师 - V2EX
- springBoot学习
- UIWebView加载html 图片大小自适应的方法汇总
- [ES6] Module export