hdu1005 Number Sequence

f(n-1)和f(n-2)所有组合情况有49种,即周期最大为49,但是f(n-1)=f(n-2)=0的情况下,整个数列都为0,而题目条件f(1)=f(2)=1,所以排除这种情况,最大周期为48;

#include<math.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
    int a,b,n;
    int f[99];
    while(scanf("%d%d%d",&a,&b,&n),(a||b||n))
    {
        f[1]=f[2]=1;
        for(int i=3;i<99; i++)
        {
            f[i]=(a*f[i-1]+b*f[i-2])%7;
        }
        printf("%d\n",f[n%48+48]);
    }
    return 0;
}
时间: 2024-10-08 15:10:57

hdu1005 Number Sequence的相关文章

HDU-1005 Number Sequence

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

hdu1005 Number Sequence(找循环节)

题目链接: huangjing 题意: 就是给了一个公式,然后求出第n项是多少... 思路: 题目中n的范围实在是太大,所以肯定直接递推肯定会超时,所以想到的是暴力打表,找循环节,但是也不是那么容易发现啊,所以这时候分析一下,因为最后都会mod7,所以总共有7X7总情况,即A 0,1,2,3,4,5,6,7,B也是如此,所以循环节为49,这么这个问题就解决了... 题目: Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memo

A - Number Sequence HDU1005 ( kmp 算法+整数数组的应用)

A - Number Sequence Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M <= 10000, 1 <= N <= 100000

【HDU 1005】Number Sequence —— 找周期

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

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的取