您的位置:首页 > 产品设计 > UI/UE

HDOJ 1005 Number Sequence

2016-02-16 23:53 501 查看


Number Sequence

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 142128 Accepted Submission(s): 34546



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 contains 3 integers A, B and n on a single line (1 <= A, B <= 1000, 1 <= n <= 100,000,000). Three zeros signal the end of input and this test case is not to be processed.

Output

For each test case, print the value of f(n) on a single line.

Sample Input

1 1 3
1 2 10
0 0 0


Sample Output

2
5//
//  main.cpp
//  1005
//
//  Created by 张嘉韬 on 16/2/16.
//  Copyright © 2016年 张嘉韬. All rights reserved.
//

#include <iostream>
#include <cstring>
using namespace std;
int main(int argc, const char * argv[]) {
int a,b,n,f[500];
while(scanf("%d%d%d",&a,&b,&n)==3&&(a||b||n))
{
int b1,d;
int beg,end;
bool flag=0;

memset(f,0,sizeof(f));
f[1]=1,f[2]=1;
for(int i=3;i<=n&&flag==0;i++)
{
f[i]=(a*f[i-1]+b*f[i-2])%7;
//cout<<f[i]<<endl;
for(int j=2;j<=i-1;j++)
{
if(f[i]==f[j]&&f[i-1]==f[j-1])
{
b1=j-1;
d=i-j;
flag=1;
beg=j;
end=i;
break;
}
}
}
if(flag==0) cout<<f
<<endl;
else //cout<<f[b1-1+((n-(b1-1))%d)]<<endl;
{
//cout<<"("<<b1+((n-(b1))%d)<<")"<<"("<<beg-1+(n-beg+1)%(end-beg)<<")";
cout<<f[beg+(n-beg)%(end-beg)]<<endl;
}
}
return 0;
}
总结首先我们要对几个问题有所思考,在考虑这几个问题的过程中对该问题的思考也就很清楚了。 一,首先根据俄7*7,连续的两个数只会出现49种情况 二,如果出现了连续的两个数和之前的两个数一样的话就会出现循环。1,状态只有49种,那么每一种连续的两位数就一定会再次出现么? 很容易列举特殊情况否定掉,所以我们在数列的开始部分也许会出现一部分不会循环的部分。搞清楚这个该题的思路就很清楚了,但是该问题在输出部分还有一个很重要也很不容易被发现的错误
就是把输出写成这个样子<pre name="code" class="cpp" style="font-size: 14px;">f[b1-1+((n-(b1-1))%d)]
,经过分析就知道,当
(n-(b1-1))%d==0的情况下不能输出争取的结果,所以我们使用取余找位输出时我们可以通过设置初始点位把应该为零的为往前推一位来计算,比如12341234,我们如果采取第一种方式,那么当我们要找4或8时,我们取余后就得到了0,很显然不能得到结果,当时如果我们把1设置为初始位置,那么2和1的位置差取余后是1,4和1的位置差取余后是3,那么就不难找到4和8的位置啦



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: