题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1005

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

解析:

这是一道寻找循环点的问题,可能很多人在杭电上通过了这个题目,但是我建议大家将自己的代码再贴到另一个OJ上进行测试http://zju.acmclub.com/index.php?app=problem_title&id=1&problem_id=2603

很多人都认为周期是49,但是给出的解题报告都不是很有说服力。

所以,我们可以寻找循环的开头以及周期,然后输出,这样能够保证正确性,当然一开始的记录数组最好能够相对大一些,不然仍然不能通过测试。

代码:

#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. VS2012编译Snmp++ v3.2.25

    VS2012编译Snmp++ v3.2.25跟用VC6/VC2010等编译方法区别不大. 网上和教程上盛传的方式是把snmp++的cpp源文件和头文件都加到工程里,再编译.我觉得添加所有头文件到工程里 ...

  2. Search in Rotated Sorted Array I II

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

  3. linux之SQL语句简明教程---LIKE

    LIKE 是另一个在 WHERE 子句中会用到的指令.基本上,LIKE 能让我们依据一个套式 (pattern) 来找出我们要的资料.相对来说,在运用 IN 的时候,我们完全地知道我们需要的条件:在运 ...

  4. poj 2531 Network Saboteur(经典dfs)

    题目大意:有n个点,把这些点分别放到两个集合里,在两个集合的每个点之间都会有权值,求可能形成的最大权值.   思路:1.把这两个集合标记为0和1,先默认所有点都在集合0里.             2 ...

  5. shu_1186 字符排列问题

    cid=1079&pid=23">http://202.121.199.212/JudgeOnline/problem.php?cid=1079&pid=23 分析: ...

  6. Wheel ProgressBar 实现之三——模拟进度过程

    1. 效果展示: 知道如何画圆弧,如何精确画出进度文本之后,我们将进入 Wheel ProgressBar 实现的最后一个过程:模拟其动态呈现过程.如下图所示,初始时显示进度为 0 (上图),点击进度 ...

  7. RMAN备份之丢失数据文件及控制文件的恢复

    About Recovery with a Backup Control FileIf all copies of the current control file are lost or damag ...

  8. 屏蔽错误:LNK2038

    最近在使用Qt(VS2010编译)的过程中,需要调用COM库,在Qt中加入了QAxContainer模块,是一个LIB库,在把编译模式从Debug改为Release 后链接报告了一堆错误 -1: 错误 ...

  9. 多个不同的表合并到一个datatable中,repeater在绑定datatable

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...

  10. linux 下idea 启动tomcat报JMX 1099错误解决办法

    开始使用linux开发环境,在配置idea 下tomcat 启动时报错,提示错误为unable to ping server at localhost:1099. 解决办法: 修改/etc/hosts ...