杭电ACM 1005 Number Sequence

</p>

Number Sequence

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

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

  刚拿到这个题时,想用递归从后往前推(但是感觉好复杂),但是似乎从f(3)循环一直计算到f(n),但是超出了时间限制;因此,应该是用到了周期性的思想;   解题思路:1)从式子出发,发现是对7取余,因此初步推算f(n)最多有七种不同的情况2)再者当A,B确定时,f(n-1),f(n-2)最多有7*7种不同的组合3)因此根据鸽巢原理,当列举的组合超过49种时,必定会有重复的组合,因此该数列具有周期性,找出周期  算法步骤:1)循环输入A、B、n,2)对于每种情况的A、B、n,算前50种组合(确保可以出现重复组合),即算到f(52)为止3)对于每种组合,查找之前是否有相同的组合,判断条件为:f(i)==f(j)&&f(i-1)==f(j-1)如果找到了相同的组合,计算其周期,以及周期规律开始的位置,切记要跳出循环4)最后计算f(n)并输出以上步骤针对如下代码,因此代码中不做赘述了
#include<iostream>
using namespace std;
int main()
{
    int A,B,n;
    while(cin>>A>>B>>n&&(A+B+n))
    {
        long s[52];
        int T,t;
        s[1]=1;s[2]=1;
        for(int i=3;i<=52;i++)
        {
            s[i]=(A*s[i-1]+B*s[i-2])%7;
            for(int j=2;j<=i-1;j++)
            {
                if(s[i]==s[j]&&s[i-1]==s[j-1])
                {
                    T=i-j;
                    t=j;
                    break;
                }
            }
        }
    if(n<t)cout<<s[n]<<endl;
    else  cout<<s[(n-t)%T+t]<<endl;
    }
}
时间: 2024-12-09 10:13:14

杭电ACM 1005 Number Sequence的相关文章

HDU ACM 1005 Number Sequence

Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 119732    Accepted Submission(s): 29072 Problem Description A number sequence is defined as follows:f(1) = 1, f(2) = 1, f(n) = (A

杭电ACM分类

杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDIATE DECODABILITY

杭电ACM题目分类

杭电ACM题目分类 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028. 1029.1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092. 1093.1094.1095.1096.1097.1098.1106.1108.1157.1163.1164.1170.1194.1196. 1197.1201.1202.1205.1219.1234.123

杭电acm 1034题

Problem Description A number of students sit in a circle facing their teacher in the center. Each student initially has an even number of pieces of candy. When the teacher blows a whistle, each student simultaneously gives half of his or her candy to

杭电ACM Java实现样例

若使用Java求解杭电ACM,答案提交必须注意两点: 1.类名一定得是Main,否则服务器无法编译: 2.必须用while进行输入判断. 以1000题测试题为例,代码如下: import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan=new Scanner(System.in); while(scan.hasNextInt()) { int a=scan.n

杭电ACM水仙花数

水仙花数 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 96473    Accepted Submission(s): 28632 Problem Description 春天是鲜花的季节,水仙花就是其中最迷人的代表,数学上有个水仙花数,他是这样定义的: "水仙花数"是指一个三位数,它的各位数字的立方和等于其本身,比如:1

杭电 acm 2053 ( Switch Game )

这题思路: 一开始有n盏灯,且全部为关闭状态,都记为 0  就是  The initial condition :      0 0 0 0 0 … 然后之后进行i操作就是对这些灯以是否能被i整除,进行改变状态,如将 0 改为 1 或 将 1 改为 0 正如提醒里的 After the first operation :  1 1 1 1 1 … After the second operation : 1 0 1 0 1 … After the third operation :  1 0 0

杭电acm 2095 find your present (2)

#include<iostream>using namespace std;int main(){    int n,x,y;    while(cin>>n,n)    {         cin>>x;                   while(--n)         {             cin>>y;             x=x^y;         }         cout<<x<<"\n&q

杭电ACM 2046 阿牛的EOF牛肉串

我用到了两个数组,d1[n]表示长度为n的牛肉串最后一个字符不是'O',d2[n]表示长度为n的牛肉串最后一个字符是'O'.这样结果就是d1[n]+d2[n]:对于已经得到了长度为n-1的牛肉串,我们可以来讨论在第n个位置放置何种字符的牛肉串. 已得到第n-1个位置的字符 第n个位置需要放置的字符 结果 不是'O' 不是'O' 得到长度为n的,结尾不是'O'的字符串 不是'O' 是'O' 得到长度为n的,结尾是'O'的字符串 是'O' 不是'O' 得到长度为n的,结尾不是'O'的字符串 是'O'