hdu 1757 A Simple Math Problem (矩阵快速幂,简单)

也是和LightOJ 1096 和LightOJ 1065 差不多的简单题目。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; int num,mod;
struct matrix
{
int a[][];
}; matrix multiply(matrix x,matrix y)//矩阵乘法
{
matrix temp;
for(int i=;i<num;i++)
{
for(int j=;j<num;j++)
{
int ans=;
for(int k=;k<num;k++)
{
ans+=((x.a[i][k]*y.a[k][j])%mod);
}
temp.a[i][j]=ans%mod;
}
}
return temp;
} matrix calc(matrix origin ,matrix answ,int n)//矩阵快速幂——answ*origin^n
{
while(n)
{
if(n%==)//原来比赛时一直是这里被我不小心改成了0,怪不得案例一直都过不了
answ=multiply(origin,answ);
origin=multiply(origin,origin);
n/=;
}
return answ;
} int main()
{
int n;
while(scanf("%d%d",&n,&mod)!=EOF)
{
matrix origin= {,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,};
matrix answ={,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,};
num=;
for(int i=;i>=;i--)//要注意这里的a9~a0的输入顺序
scanf("%d",&origin.a[][i]); answ=calc(origin,answ,n-);
printf("%d\n",answ.a[][]%mod);
}
return ;
}
hdu 1757 A Simple Math Problem (矩阵快速幂,简单)的更多相关文章
- HDU 1757 A Simple Math Problem (矩阵快速幂)
题目 A Simple Math Problem 解析 矩阵快速幂模板题 构造矩阵 \[\begin{bmatrix}a_0&a_1&a_2&a_3&a_4&a ...
- hdu 1757 A Simple Math Problem_矩阵快速幂
题意:略 简单的矩阵快速幂就行了 #include <iostream> #include <cstdio> #include <cstring> using na ...
- HDU 1757 A Simple Math Problem(矩阵)
A Simple Math Problem [题目链接]A Simple Math Problem [题目类型]矩阵快速幂 &题解: 这是一个模板题,也算是入门了吧. 推荐一个博客:点这里 跟 ...
- HDU1757 A Simple Math Problem 矩阵快速幂
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- A Simple Math Problem(矩阵快速幂)----------------------蓝桥备战系列
Lele now is thinking about a simple function f(x). If x < 10 f(x) = x. If x >= 10 f(x) = a0 ...
- HDU 1757 A Simple Math Problem 【矩阵经典7 构造矩阵递推式】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=1757 A Simple Math Problem Time Limit: 3000/1000 MS (J ...
- HDU 1757 A Simple Math Problem(矩阵快速幂)
题目链接 题意 :给你m和k, 让你求f(k)%m.如果k<10,f(k) = k,否则 f(k) = a0 * f(k-1) + a1 * f(k-2) + a2 * f(k-3) + …… ...
- hdu 1757 A Simple Math Problem (矩阵快速幂)
Description Lele now is thinking about a simple function f(x). If x < 10 f(x) = x. If x >= 10 ...
- HDU 1757 A Simple Math Problem( 矩阵快速幂 )
<font color = red , size = '4'>下列图表转载自 efreet 链接:传送门 题意:给出递推关系,求 f(k) % m 的值, 思路: 因为 k<2 * ...
随机推荐
- android ListView_Tiger
xml设计 <?xml version="1.0"?> -<LinearLayout tools:context=".MainActivity" ...
- 1 . Robberies (hdu 2955)
The aspiring Roy the Robber has seen a lot of American movies, and knows that the bad guys usually g ...
- 使用inotify检测linux目录内文件变化
#include <unistd.h> #include <sys/inotify.h> #include <stdio.h> #include <error ...
- 完美解决fixed 水平居中问题
群里的朋友问的,发现自己没写过:就写了下,原理和网上的fixed上下左右四个角的原理一样! 1.防止页面振动: body{ _background-image: url(about:blank); _ ...
- Eclipse 代码提示功能设置。
1. 解决实例化时自动补全不必要的单词问题 2. 以MyEclipse 6.5重新配图 鉴 于网上的批评之声甚大,我只想说明我的想法:这样的增强代码提示,最终是用来辅助我们 ...
- TextView中gravity属性值测定
Attributes Explain top 不改变控件大小,对齐到容器顶部 bottom 不改变控件大小,对齐到容器底部 left 不改变控件大小,对齐到容器左侧 right 不改变控件大小,对齐到 ...
- 利用ajax在javascript中获取后台的值
<script type="text/javascript"> function login() { var sa = WebForm1.Hello().value; ...
- Requests库的几种请求 - 通过API操作Github
本文内容来源:https://www.dataquest.io/mission/117/working-with-apis 本文的数据来源:https://en.wikipedia.org/wiki/ ...
- python 内置模块之logging
1.将日志直接输出到屏幕 import logging logging.debug('This is debug message') logging.info('This is info messag ...
- 虚拟机开机提示:This virtual machine appears to be in use
[原因]:由于电脑非正常关机导致,重新启动虚拟机就会出现This virtual machine appears to be in use的提示.[解决方法]:到虚拟机的安装目录下删除所有.lck的目 ...