问题描述

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 这道题之前用递归做的,结果runtimeError了,估计递归非常慢,想想会超出堆栈.后来百度,原来是个循环节的概念,因为有mod 7,所以f(n)取值是0-6,7个取值,而f(n)又由上头两个决定,因此有7*7=49种答案,因此在50以内必然出现循环,所以我们用数组模拟前49组数组,后面的数据只要mod (模除)循环节就可以了,对应的的数组里头取值,下面附上代码 ------------------------------------下面附上AC代码-----------------------
#include <iostream>
using namespace std;
int main()
{
        int A,B;
        int N;
        int f[50];
        while(cin>>A>>B>>N)
        {
                if(A==0&&B==0&&N==0)break;
                int i;
                f[1]=f[2]=1;
                for(i=3;i!=50;++i)
                {
                        f[i]=(A*f[i-1]+B*f[i-2])%7;
                        if(f[i]==1&&f[i-1]==1)break;
                }
                N=N%(i-2);//理解i-2,求的循环节N
                if(N==0)cout<<f[(i-2)]<<endl;
                else cout<<f[N]<<endl;
        }
        
        return 0;
}

杭电1005-Number Sequence的更多相关文章

  1. 杭电 1711 Number Sequence

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

  2. 1005 Number Sequence(HDU)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 Number Sequence Time Limit: 2000/1000 MS (Java/O ...

  3. HDU 1005 Number Sequence(数列)

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

  4. HDU 1005 Number Sequence(数论)

    HDU 1005 Number Sequence(数论) Problem Description: A number sequence is defined as follows:f(1) = 1, ...

  5. HDU - 1005 Number Sequence 矩阵快速幂

    HDU - 1005 Number Sequence Problem Description A number sequence is defined as follows:f(1) = 1, f(2 ...

  6. HDU 1005 Number Sequence【多解,暴力打表,鸽巢原理】

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

  7. HDU 1005 Number Sequence

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

  8. hdu 1005:Number Sequence(水题)

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

  9. 1005 Number Sequence

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

  10. HDU 1005 Number Sequence (模拟)

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

随机推荐

  1. 遇到could not find developer disk image 问题怎么解决

    一般是设备的版本低于或者高于当前的xcode

  2. 删除NSMutableArray中的二维数组

    // 删除模型数据 [self.mutableArr[indexPath.section] removeObjectAtIndex:indexPath.row]; //删除UI(刷新数据,UI) [s ...

  3. F面经:painting house

    There are a row of houses, each house can be painted with three colors red, blue and green. The cost ...

  4. eclipse JAVA 类元素 快速添加set和get方法

    鼠标在代码窗口,鼠标右键 然后选择如下图 然后在下面图中,选择需要个类的元素添加的set和get 选择好了点击OK,这样eclipse就自动生成get和set方法

  5. hdu1251(字典树)

    统计难题(hdu1251) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Tota ...

  6. win8, VS2013 .NET 4.5在哪找svcutil.exe?

    我这个纠结呀,公司用win8, .NET 4.5.想做一个很简单的项目,就是wcf宿主iis,项目根目录下有aspx文件和svc文件.于是参考了一个博客http://www.cnblogs.com/y ...

  7. C++初学者 const使用详解

     关于C++中的const关键字的用法非常灵活,而使用const将大大改善程序的健壮性,参考了康建东兄的const使用详解一文,对其中进行了一些补充,写下了本文. 1. const常量 如: cons ...

  8. struts文件上传(多文件)

    第01步:配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version= ...

  9. 为VirtualBox里的Linux系统安装增强功能

    先说下为什么要安装增强功能, 很重要的原因是分辨率,没有安装增强功能的虚拟机里的系统往往不能全屏的,屏幕显示的内容没有充满整个屏幕. 还有主机和客户机之间共享文件夹也需要客户机的系统安装了增强功能. ...

  10. 帮初学者改代码——playerc之“练习:求完数问题”(上)

    原文:“练习:求完数问题” 原代码: // #include <stdio.h> #include <stdlib.h> #include <math.h> #de ...