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

hdu1005 Number Sequence(找循环节)

时间:2014-09-17 16:51:42      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   io   os   java   ar   strong   

题目链接:

题意:

就是给了一个公式,然后求出第n项是多少。。。

思路:

题目中n的范围实在是太大,所以肯定直接递推肯定会超时,所以想到的是暴力打表,找循环节,但是也不是那么容易发现啊,所以这时候分析一下,因为最后都会mod7,所以总共有7X7总情况,即A 0,1,2,3,4,5,6,7,B也是如此,所以循环节为49,这么这个问题就解决了。。。

题目:

Number Sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 105674    Accepted Submission(s): 25691


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
 

Author
CHEN, Shunbao
 

Source
 

Recommend
JGShining   |   We have carefully selected several similar problems for you:  1021 1019 1009 1012 1071 
代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<cmath>
#include<string>
#include<queue>
#define eps 1e-9
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;

#define mod 7

const int maxn=1000+10;
int a[maxn];
int A,B,n;

int main()
{
    while(~scanf("%d%d%d",&A,&B,&n))
    {
        if(A==0&&B==0&&n==0)  return 0;
        a[1]=1,a[2]=1;
        for(int i=3;i<=100;i++)
             a[i]=(A*a[i-1]+B*a[i-2])%mod;
        printf("%d\n",a[n%49]);
    }
    return 0;
}



hdu1005 Number Sequence(找循环节)

标签:des   style   http   color   io   os   java   ar   strong   

原文地址:http://blog.csdn.net/u014303647/article/details/39343731

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