题目: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).

输入

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 ;
}

最新文章

  1. try--catch--finally中return返回值执行的顺序(区别)
  2. mysql 关于列的语句
  3. HDU 1565&amp;1569 方格取数系列(状压DP或者最大流)
  4. C/C++ 结构体 函数传递
  5. 20170721_python字符串操作_《python语言及其应用》
  6. 安装oh-my-zsh
  7. ADF BC New Features
  8. django-团队简介的网页
  9. 关闭selinux服务
  10. ajax基本原理
  11. JVM 运行时数据区 (三)
  12. ASP.NET MVC HtmlHelper 类的扩展方法
  13. 【BZOJ 4818】 4818: [Sdoi2017]序列计数 (矩阵乘法、容斥计数)
  14. SNMP学习笔记之iReasoning MIB Browser
  15. webpack快速入门——实战技巧:webpack优化黑技能
  16. dockerfile基础命令
  17. Python日记——nginx+Gunicorn部署你的Flask项目
  18. linux文件管理 -&gt; 系统压缩打包
  19. java 自定义泛型
  20. SQL数据库异地备份

热门文章

  1. MapReduce 中的两表 join 几种方案简介
  2. Servlet中Request的getAttribute getParameter 区别
  3. 那么类 Man 可以从类 Human 派生,类 Boy 可以从类 Man 派生
  4. IIS故障问题(Connections_Refused)分析及处理【转】
  5. 原生YII2 增删改查的一些操作(非ActiveRecord)
  6. VS2013和VS2010工具集和字符集
  7. 将Oracle数据库转换为SQL Server
  8. ArcGIS ArcMap 与 ArcServer关于Python的冲突
  9. ios 如何改变UISegmentedControl文本的字体大小?
  10. js 正则 exec() 和 match() 数据抽取