Number Sequence

问题陈述:

  杭州电子科技大学HANGZHOU DIANZI UNIVERSITY Online Judge Problem - 1005

问题解析:

  最初看到这个题目,第一感觉直接套公式递归求解,结果报Runtime Error STACK_OVERFLOW。递归层太多,导致栈溢出。再次分析发现,因为(a + b) mod m = (a mod m + b mod m) mod m,所以本题f(n)可以写成f(n) = (A*f(n-1) mod 7 + B*f(n-2) mod 7) mod 7, 而f(x) mod 7 可以取0~6七种值,所以f(n-1) mod 7  + f(n-2) mod 7至多有49中情况,即f(n)至多有49个值,可以把f(1)~f(49)看做一个循环,超出这个范围,对49去余即可。

代码详解:

 1 #include <iostream>
 2 #include <cstdio>
 3
 4 using namespace std;
 5
 6 int f[50];
 7 int func(int, int, int);
 8
 9 int main()
10 {
11     int a, b, n;
12     while(scanf("%d%d%d", &a, &b, &n) && a||b||n) {
13         printf("%d\n", func(a, b, n));
14     }
15     return 0;
16 }
17
18 int func(int a, int b, int n) {
19     int i;
20     if(n==1 || n==2) {
21         return 1;
22     }else {
23         f[1] = f[2] = 1;
24         for(i=3; i<50; i++) {
25             f[i] = (a*f[i-1] + b*f[i-2])%7;
26             if(n == i) {
27                 return f[i];
28             }
29         }
30         f[0] = f[49];
31         return f[n%49];
32     }
33 }

转载请注明出处:http://www.cnblogs.com/michaelwong/p/4284945.html

时间: 2024-08-11 07:41:07

Number Sequence的相关文章

1005 Number Sequence

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 co

hdu5014 Number Sequence(异或运算)

题目链接: huangjing 题意: 这个题目的意思是给出0~n的排列,然后找出与这个序列的配对使(a0 ⊕ b0) + (a1 ⊕ b1) +·+ (an ⊕ bn)最大.. 思路: 从大到小遍历每个数,然后找到与这个数二进制位数互补的数,那么他们的抑或值必定是pow(2,n)-1,,肯定是最大的.... 题目: Number Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav

HDU 5014 Number Sequence(2014 ACM/ICPC Asia Regional Xi&#39;an Online) 题解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 Number Sequence Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● ai ∈ [0,n] ● ai ≠ aj( i ≠ j ) For sequence a and sequ

TOJ 1203: Number Sequence

1203: Number Sequence Time Limit(Common/Java):1000MS/10000MS     Memory Limit:65536KByte Total Submit: 957            Accepted:208 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. Gi

ACM—Number Sequence(HDOJ1005)

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 主要内容: 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). 看到这样的公式很容易想到递归调用求解,但是在本题中n的取

HDU 1711 Number Sequence(KMP算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 15548    Accepted Submission(s): 6836 Problem Description Given two sequence

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

Number Sequence Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● a i ∈ [0,n] ● a i ≠ a j ( i

poj 1019 Number Sequence 二分

Number Sequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 34391   Accepted: 9879 Description A single positive integer i is given. Write a program to find the digit located in the position i in the sequence of number groups S1S2...

HDU - 5014 Number Sequence

Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● ai ∈ [0,n] ● ai ≠ aj( i ≠ j ) For sequence a and sequence b, the integrating degree t is defined as follows("⊕" deno

KMP算法的定义及KMP练手题 HDU 1711 Number Sequence (我的模板代码)

题意:就是要你来找出b数组在a数组中最先匹配的位置,如果没有则输出-1 思路:直接KMP算法(算法具体思想这位牛写的不错http://blog.csdn.net/v_july_v/article/details/7041827) AC代码: #include<cstdio> #include<cstring> #include<stdlib.h> #include<iostream> using namespace std; #define maxn 100