码迷,mamicode.com
首页 > 其他好文 > 详细

Number Sequence hdu1005

时间:2018-10-19 02:03:36      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:sequence   name   amp   i++   str   end   algo   分析   algorithm   

题意

算第n项,f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.

分析

可能的情况0,1,2,3,4,5,6,f(n-1),f(n-2)
所以f(n)最多有49种方案有f(n-1),f(n-2)得来
最大循环节49,n%49即可

code

#include<iostream>
#include<algorithm>
#include<map>
#include<string>
using namespace std;
int main(){
    int f[50];
    f[1]=f[2]=1;
    int a,b,n,i;
    //freopen("in.txt","r",stdin);
    while(cin>>a>>b>>n&&a!=0&&b!=0&&n!=0){
            int maxn=n<50?n:49;
            for(i=3;i<=maxn;i++){
                f[i]=(a*f[i-1]+b*f[i-2])%7;
                }
                cout<<f[n%49]<<endl;
            }
    return 0;
}

Number Sequence hdu1005

标签:sequence   name   amp   i++   str   end   algo   分析   algorithm   

原文地址:https://www.cnblogs.com/mch5201314/p/9813919.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!