您的位置:首页 > 其它

hdu 1005(找规律--循环节)

2014-04-01 21:53 316 查看
题目链接:点击打开链接

题目大意: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).

题目分析:一般数学题:1.结论问题;

2.递推

3.打表找规律

这道题之前还去用过递推,解方程,但是由于f【n】和F【n-1】等是有关系的,所以完全可以找循环节(即前后连着的与某个位置的前后两个位置相同这一定是循环节),再则相邻2个数之间的搭配(7*7=49 )种,所以完全不用担心循环节的长度

题目总结:姿势要优美,一般正确的代码一定是简洁干净的

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
using namespace std;
int f[105];
int main()
{
    int a,b,n,flag=1,i,j,start,end;
    while(1)
    {
        scanf("%d%d%d",&a,&b,&n);
        if(!a||!b||!n)
            break;
        f[1]=f[2]=1;
        flag=0;
        for(i=3; i<=n&&!flag; i++)
        {
            f[i]=(a*f[i-1]+b*f[i-2])%7;
           // printf("n:%d %xbd\n",i,f[i]);
            for(j=2; j<i-1; j++)
            {
                if(f[j]==f[i]&&f[j-1]==f[i-1])
                {
                    flag=1;start=j,end=i;
                    break;
                }
            }

        }
        if(flag)
            n=(n-start)%(end-start)+start;
        printf("%d\n",f
);
        }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: