hdu1005

Number Sequence

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

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

 1 #include <stdio.h>
 2 #include <math.h>
 3 #include <string.h>
 4 #include <map>
 5 #include <iostream>
 6 #include <algorithm>
 7 int w[100];
 8 using namespace std;
 9 int main()
10 {
11     int a,b,n,i,flag,first,end,j;
12     while(cin>>a>>b>>n&&(a||b||n))
13     {
14         flag=0;
15         w[0]=1;
16         w[1]=1;
17         w[2]=1;
18         for(i=3;i<=n&&!flag;i++)
19         {
20             w[i]=(a*w[i-1]+b*w[i-2])%7;
21             for(j=2;j<=i-1;j++)
22             {
23                 if(w[i]==w[j]&&w[i-1]==w[j-1])
24                 {
25                     first=j-1;
26                     end=i-1;
27                     flag=1;
28                     break;
29                 }
30             }
31         }
32         if(flag)
33             printf("%d\n",w[first+(n-end)%(end-first)]);
34         else
35             printf("%d\n",w[n]);
36     }
37     return 0;
38 }
时间: 2024-09-29 23:01:38

hdu1005的相关文章

hdu1005 矩阵

1 //Accepted hdu1005 0MS 248K 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <queue> 6 #include <cmath> 7 #include <algorithm> 8 using namespace std; 9 /** 10 * This is a documentation comment

矩阵快速幂(入门) 学习笔记hdu1005, hdu1575, hdu1757

矩阵快速幂是基于普通的快速幂的一种扩展,如果不知道的快速幂的请参见http://www.cnblogs.com/Howe-Young/p/4097277.html.二进制这个东西太神奇了,好多优秀的算法都跟他有关系,这里所说的矩阵快速幂就是把原来普通快速幂的数换成了矩阵而已,只不过重载了一下运算符*就可以了,也就是矩阵的乘法,  当然也可以写成函数,标题中的这三个题都是关于矩阵快速幂的基础题.拿来练习练习熟悉矩阵快速幂,然后再做比较难点的,其实矩阵快速幂比较难的是构造矩阵.下面还是那题目直接说话

HDU1005 找规律 or 循环点 or 矩阵快速幂

http://acm.hdu.edu.cn/showproblem.php?pid=1005 1.一开始就注意到了n的数据范围 <=100 000 000,但是还是用普通的循环做的,自然TLE了,然后朴素打表,= =运行不了,(怎么可能能把数组开到那么大).再然后就想到了寻找下一个1 1 连续在一起的,那就能开始下一轮循环了. 但是,各种WA--(将数组开大一点,寻找到a[ i ] = a[ i -1 ] ==1 即跳出),这个AC代码将102改成100,150,200都可以,但是108,49

HDU-1005 Number Sequence

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

HDU1005&amp;&amp;NEFU67 没有循环节

Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 147161    Accepted Submission(s): 35755 Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A

HDU1005(周期问题)

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

hdu1005 Number Sequence

f(n-1)和f(n-2)所有组合情况有49种,即周期最大为49,但是f(n-1)=f(n-2)=0的情况下,整个数列都为0,而题目条件f(1)=f(2)=1,所以排除这种情况,最大周期为48: #include<math.h> #include<stdio.h> #include<stdlib.h> #include<string.h> int main(void) { int a,b,n; int f[99]; while(scanf("%d%

hdu1005 超规模&amp;gt;&amp;gt;找规律&amp;gt;&amp;gt;有限次数循环

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 //由于题目有两项在变,一项有7种可能,全部共同拥有49种情况. /* 超规模>>算法优化 >>找规律>>规律变化 >>有限次数内循环>>找到变化范围 */ int main() { int f[51]={49,1,1}; int a,b,n,i; while( cin>>a>>b>>n,a+b+n){ fo

A - Number Sequence HDU1005 ( kmp 算法+整数数组的应用)

A - Number Sequence Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M <= 10000, 1 <= N <= 100000