hdu1005-Number Sequence-(循环节)

题意:已知f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7,给出A,B,n,求f(n)

题解:n巨大,循环肯定超时,在模7的条件下,0<=f(n)<=6,一共7种选择,则f(n-1)和f(n-2)各有7种选择,共49种组合,至少在第50个组合必定会和前面的重复,找出循环节。

坑:不知网上为什么都说找连续两个1的循环节,有大神指出这是错误的,当a和b是某两个数时,序列是1,4,6循环,但我忘记是哪两个数了,也找不到博客。

在我看来,两重循环找出相同的比较稳妥,而不是找两个1。

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<string>
#include<map>
#include<queue>
#include<stack>
#include<set>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;

int a,b,n;
int f[10086];

int main()///hdu1005
{
    while(scanf("%d %d %d",&a,&b,&n)!=EOF&&(a+b+n))
    {
        memset(f,0,sizeof(f));
        f[1]=f[2]=1;
        int i,j;
        int minn=min(n,100);
        for(i=3;i<=minn;i++)
            f[i]=(a*f[i-1]+b*f[i-2])%7;

        ///找第一次和第二次出现的循环节
        int cha=-1;
        for(i=3;i<=minn;i++)
        {
            for(j=i+1;j<=minn;j++)///不吝啬这点时间找出循环节
            if( f[i]==f[j] && f[i+1]==f[j+1] )
            {
                cha=j-i;///循环节的差
                break;
            }
            if(cha!=-1)
                break;
        }
        /// 此时i是循环节的起点
        int idx;
        if(cha==-1)///数据太少,没有找到循环节,直接求
        {
            idx=n;
        }
        else
        {
            n=n+cha;
            idx=(n-2)%cha;
            if(idx==0)
                idx=2+cha;
            else
                idx=2+idx;
        }
        printf("%d\n",f[idx]);
    }
    return 0;
}

hdu1005

原文地址:https://www.cnblogs.com/shoulinniao/p/11323003.html

时间: 2024-08-02 13:29:22

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

POJ 1019 Number Sequence (循环递增序列的的第K个值)

Number Sequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 35823   Accepted: 10340 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..

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%

数论基础——循环节和矩阵快速幂的运用

首先我们来看一道基础题: 题目链接:HDU1005 Number Sequence 题目描述: Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 147421    Accepted Submission(s): 35814 Problem Description A number sequence is

SDUT 2044 Number Sequence(循环)

Number Sequence Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 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 inpu

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): 175657    Accepted Submission(s): 43409 Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A

HDU 1005 Number Sequence(数列)

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) [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

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