SDUT1607:Number Sequence(矩阵快速幂)
2024-08-25 01:56:51
题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1607
题目描述
A number sequence is defined as follows:
f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.
Given A, B, and n, you are to calculate the value of f(n).
f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.
Given A, B, and n, you are to calculate the value of f(n).
输入
The input consists of multiple test cases. Each test case contains 3 integers A, B and n on a single line (1 <= A, B <= 1000, 1 <= n <= 100,000,000). Three zeros signal the end of input and this test case is not to be processed.
输出
For each test case, print the value of f(n) on a single line.
示例输入
1 1 3
1 2 10
0 0 0
示例输出
2
5
题目解析:
模板题没什么好说的。
(f[1],f[2])*[ 0,1 ]=(f[2],f[3])
[B, A ]
代码如下:
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
typedef long long ll;
#define inf 0x3f3f3f3f
#define mod 7
#include <math.h>
#include <queue>
using namespace std;
struct ma
{
ll a[][];
} init,res;
int A,B,n;
ma mult(ma x,ma y)
{
ma tmp;
for(int i=; i<; i++)
{
for(int j=; j<; j++)
{
tmp.a[i][j]=;
for(int k=; k<; k++)
{
tmp.a[i][j]=(tmp.a[i][j]+x.a[i][k]*y.a[k][j])%mod;
}
}
}
return tmp;
}
ma Pow(ma x,int K)
{
ma tmp;
for(int i=; i<; i++)
{
for(int j=; j<; j++)
{
tmp.a[i][j]=(i==j);
}
}
while(K)
{
if(K&) tmp=mult(x,tmp);
K>>=;
x=mult(x,x);
}
return tmp;
}
int main()
{
while(scanf("%d%d%d",&A,&B,&n)!=EOF)
{
if(A==&&B==&&n==) break;
if(n==||n==)
{
printf("1\n");
continue;
}
init.a[][]=,init.a[][]=;
init.a[][]=B,init.a[][]=A;
res=Pow(init,(n-));
ll sum=(res.a[][]+res.a[][])%mod;
printf("%lld\n",sum);
}
return ;
}
最新文章
- try--catch--finally中return返回值执行的顺序(区别)
- mysql 关于列的语句
- HDU 1565&;1569 方格取数系列(状压DP或者最大流)
- C/C++ 结构体 函数传递
- 20170721_python字符串操作_《python语言及其应用》
- 安装oh-my-zsh
- ADF BC New Features
- django-团队简介的网页
- 关闭selinux服务
- ajax基本原理
- JVM 运行时数据区 (三)
- ASP.NET MVC HtmlHelper 类的扩展方法
- 【BZOJ 4818】 4818: [Sdoi2017]序列计数 (矩阵乘法、容斥计数)
- SNMP学习笔记之iReasoning MIB Browser
- webpack快速入门——实战技巧:webpack优化黑技能
- dockerfile基础命令
- Python日记——nginx+Gunicorn部署你的Flask项目
- linux文件管理 ->; 系统压缩打包
- java 自定义泛型
- SQL数据库异地备份
热门文章
- MapReduce 中的两表 join 几种方案简介
- Servlet中Request的getAttribute getParameter 区别
- 那么类 Man 可以从类 Human 派生,类 Boy 可以从类 Man 派生
- IIS故障问题(Connections_Refused)分析及处理【转】
- 原生YII2 增删改查的一些操作(非ActiveRecord)
- VS2013和VS2010工具集和字符集
- 将Oracle数据库转换为SQL Server
- ArcGIS ArcMap 与 ArcServer关于Python的冲突
- ios 如何改变UISegmentedControl文本的字体大小?
- js 正则 exec() 和 match() 数据抽取