• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Just Fools.
♕Heart-broken is just a fool.
   首页    新随笔    联系   管理    订阅  订阅

HDU 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 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

 题目就是给你一条递推式,让你求第n项的大小。

假如直接模拟,会超时,因为它可以给你求很后面的数据,所以不能直接模拟。

所以就考虑有没有循环,看到题目的递推式里对于7取模,只有两个变量共有7*7=49种可能,所以可以对所有的可能进行枚举,然后对求的数对49取模就可以过了。

代码如下:

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<cstring>
 5 using namespace std;
 6 int main(){
 7     int a,b,n;
 8     int ans[200];
 9     scanf("%d %d %d",&a,&b,&n);
10     while(n!=0)
11     {
12         ans[1]=1;
13         ans[2]=1;
14         int i=3;
15         for ( ;i<=100;++i)
16         {
17             ans[i]=(ans[i-1]*a+ans[i-2]*b)%7;
18         }
19         n=n%49;
20         printf("%d\n",ans[n]);
21         scanf("%d %d %d",&a,&b,&n);
22     }
23     
24     return 0;
25 }

 

posted @ 2016-02-02 18:19  ♕Heart-broken  阅读(137)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3