# Number Sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 85249    Accepted Submission(s): 20209

Problem Description
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).

Input
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.

Output
For each test case, print the value of f(n) on a single line.

Sample Input
1 1 3
1 2 10
0 0 0

Sample Output
2
5

```#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define swap(a,b) {(a)=(a)^(b); (b)=(a)^(b); (a)=(a)^(b);}
#define MAXN 65535
#define INF 1e9

int f[1200];
int main(){
int a,b,n;
int i, j;
int flag, term, temp, begin;
while(~scanf("%d%d%d", &a, &b, &n), (a||b||n)){
memset(f, 0, sizeof(f));
f[1]=1;
f[2]=1;
term = n;
flag = 0;
for(i=3; i<=n&&!flag; i++){
f[i] = (a*f[i-1]+b*f[i-2])%7;
for(j = 2; j<i; j++){
if(f[i]==f[j]&&f[i-1]==f[j-1]){
term = i-j;
begin = j-2;
flag = 1;
break;
}
}
}
if(flag)
printf("%d\n", f[begin+(n-1-begin)%term+1]);
else
printf("%d\n", f[n]);
}
return 0;
}```

## 1005 Number Sequence（HDU）的更多相关文章

1. HDU 1005 Number Sequence（数列）

HDU 1005 Number Sequence(数列) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...

2. HDU 1005 Number Sequence （模拟）

题目链接 Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f( ...

3. HDU 1005 Number Sequence（矩阵）

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

4. HDU 1711 Number Sequence（数列）

HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...

5. hdu 1005:Number Sequence（水题）

Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

6. 1005：Number Sequence（hdu，数学规律题）

Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1 ...

7. HDU - 1005 Number Sequence （矩阵快速幂）

A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mo ...

8. HDU 1711 Number Sequence（KMP）附带KMP的详解

题目代号:HDU 1711 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/ ...

9. Number Sequence （HDU 1711）

Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

## 随机推荐

1. NHibernate 映射失败 is not mapped

1 区分大小写(实体类名) 2 MAP的XML设置为嵌入的资源 3 hibernate.cfg.xml配置添加map的程序集<mapping assembly="Model" ...

2. iOS：CALayer核心动画层上绘图

在CALayer上绘图: •要在CALayer上绘图,有两种方法: 1.创建一个CALayer的子类,然后覆盖drawInContext:方法,可以使用Quartz2D API在其中进行绘图 2.设置 ...

3. 《C++ Primer 4th》读书笔记 第12章-类

原创文章,转载请注明出处:http://www.cnblogs.com/DayByDay/p/3936473.html

4. iOS 9 适配需要注意的问题

iOS 9 适配需要注意的问题 1`网络适配_改用更安全的HTTPS iOS9把所有的http请求都改为https了:iOS9系统发送的网络请求将统一使用TLS 1.2 SSL.采用TLS 1.2 协 ...

5. 移动端Web页面问题

1.安卓浏览器看背景图片,有些设备会模糊.   用同等比例的图片在PC机上很清楚,但是手机上很模糊,原因是什么呢? 经过研究,是devicePixelRatio作怪,因为手机分辨率太小,如果按照分辨率 ...

6. Activity生命周期解决(有图有真相)

Activity完整的生命周期: 启动Activity的周期历程: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcGVuZ2t2/font/5a6L5L2 ...

7. redis的安装部署启动停止&lt;17.3.21已更新&gt;

--------------------------------------------------------- 启动redis时使用下面两条命令: redis-server /etc/redis. ...

8. HTML5基本知识点

一.什么是HTML HTML是超文本标签语言,即网页的源码.而浏览器就是翻译解释HTML源码的工具. 二.HTML的基本格式 <!DOCTYPE html>: ①文档类型声明:让浏览器按照 ...

9. [Postman]证书(13)

Postman的本机应用程序提供了一种基于每个域查看和设置SSL证书的方法. 要管理客户端证书,请单击标题工具栏右侧的扳手图标,选择“设置”,然后选择“ 证书”选项卡. 添加客户端证书 要添加新客户端 ...

10. ieda 运行web--导入其它jar包

== 配置 或选择tomcat 1.打开IntelliJ IDEA,点击菜单的“Run”->“Edit Configurations...”,然后会打开运行设置弹框.2.在弹框中,点击左上角的“ ...