题目链接: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. Number Sequence (HDU 1711)

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

  8. Spring-1-H Number Sequence(HDU 5014)解题报告及测试数据

    Number Sequence Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Pro ...

  9. ACM学习历程——HDU 5014 Number Sequence (贪心)(2014西安网赛)

    Description There is a special number sequence which has n+1 integers. For each number in sequence, ...

随机推荐

  1. 权限框架 - shiro 授权demo

    之前说了权限认证,其实也就是登录验证身份 这次来说说shiro的授权 shiro可以针对角色授权,或者访问资源授权 两者都行,但是在如今的复杂系统中,当然使用后者,如果你是小系统或者私活的话,前者即可 ...

  2. hdoj 2199 Can you solve this equation?【浮点型数据二分】

    Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  3. openssl 第一篇

    自从老罗赞助了openssl以及心脏出血事件的新闻,得以了解了openssl.那么什么是openssl呢?下面摘自官网: The OpenSSL Project is a collaborative  ...

  4. TPL相关

    C#中的Timer System.Windows.Forms.TimerSystem.Threading.Timer System.Timers.Timer 1.System.Windows.Form ...

  5. centos7.2自带的php5.4升级为5.6

    参考:http://jingyan.baidu.com/article/380abd0a1f176c1d91192c4b.html 今天在新购的阿里云上部署个phpmyadmin,结果显示了个如下信息 ...

  6. (NO.00002)iOS游戏精灵战争雏形(一)

    原本想做一个复杂点的平面动作游戏,可以觉得还是有点把握不了.还是先从简单的原型开始吧. 构思中的精灵战争(SpriteWar)是一个类似FC时代的小游戏,可以造兵,可以捕获敌兵.原本还想加上保卫老巢的 ...

  7. vonic单页面应用

    Vonic—基于Vue.js和ionic样式的移动端UI框架 先放上源码和demo地址: 标签演示:  https://wangdahoo.github.io/vonic/docs/       源码 ...

  8. docker 镜像存放路径的修改

    可以通过在启动时使用--graph参数来指定存储路径. 或者使用systemd来管理服务, 就在/lib/systemd/system/docker.service中修改这一行: 1.ExecStar ...

  9. Bytom资产发行与部署合约教程

    比原项目仓库: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchain/bytom 发行资产 ...

  10. vue.js 三种方式安装(转)

    https://blog.csdn.net/m0_37479246/article/details/78836686