zoukankan      html  css  js  c++  java
  • hdu 1005 Number Sequence

    Number Sequence

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

     
    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
     
    Author
    CHEN, Shunbao
     


    Source
     


    Recommend
    JGShining   |   We have carefully selected several similar problems for you:  1071 1006 1007 1170 1032 
     
    通过打表发现循环是有规律的,但是没有发现循环节是多少,在网上查看的时候,看到了这段解释。
    对于公式 f[n] = A * f[n-1] + B * f[n-2]; 后者只有7 * 7 = 49 种可能,为什么这么说,因为对于f[n-1] 或者 f[n-2] 的取值只有 0,1,2,3,4,5,6 这7个数,A,B又是固定的,所以就只有49种可能值了。由该关系式得知每一项只与前两项发生关系,所以当连续的两项在前面出现过循环节出现了,注意循环节并不一定会是开始的 1,1 。
     
    题意:题意很简单,题目中已经给了一个公式,输入数据A,B,n,A,B为公式A,B的值,n指的是输出数组中第n个数字。
     
    附上代码:
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 using namespace std;
     5 int a,b,n;
     6 int s[1005]= {0,1,1};
     7 void init()
     8 {
     9     /*  for(int i=3; i<=100; i++)
    10       s[i]=(a*s[i-1]+b*s[i-2])%7;
    11       for(int i=1;i<=100;i++)
    12           cout<<s[i]<<" ";
    13      cout<<endl;*/        //打表发现数据有规律
    14     for(int i=3; i<=48; i++)
    15         s[i]=(a*s[i-1]+b*s[i-2])%7;    //发现规律中最大循环次数
    16 }
    17 int main()
    18 {
    19     int i,j;
    20     while(~scanf("%d%d%d",&a,&b,&n))
    21     {
    22         if(a==0&&b==0&&n==0)
    23             break;
    24         init();
    25         printf("%d
    ",s[n%48]);   //对最大循环次数取余
    26     }
    27 }
  • 相关阅读:
    SQL字符串操作汇总
    重构之道清除代码异味
    Html.Action和Html.RederAction来创建子视图
    C#实现Thrift连接池[新]
    CentOS下配置Apache反向代理出错的解决
    entity framework实体用数据库默认值的方法
    为IEnumerable类型添加Add方法
    一个对Entity Framework数据层的封装
    将.netFramework4.5/MVC4/EF5/Oracle网站发布到Server2008/iis7的痛苦经历
    让vs2012运行vs2010插件的方法
  • 原文地址:https://www.cnblogs.com/pshw/p/4754597.html
Copyright © 2011-2022 走看看