找规律/hdu 1005 Number Sequence

题意

  给出a,b,n,已知f[1]=f[2]=1,f[i]=(a*f[i-1]+b*f[i-2]) mod 7

  输出f[n]

  数据范围 1 <= A, B <= 1000, 1 <= n <= 100,000,000

分析

  首先,直接求..肯定是不行的 会tle 会mle

  但是可以找到规律,例如对a=1,b=1 这个数据,有

  f1=1  f2=1  f3=3

  f4=5  f5=4  f6=0

  f7=1  f8=1  f9=3

  f10=5  f11=4  f12=0

  ????

  我们会发现有一定的规律:每当遇到1, 1 时,就代表一次新的周期,所以我们只需求出周期t以及一个周期的答案,f[n mod t]即为答案

  关于利用循环查找周期:保险起见,我循环到10000来查找,但是过后查看别人的题解,发现循环到49即可,但具体证明未能找到

Accepted Code

 1 /*
 2     PROBLEM:hdu1005
 3     AUTHER:NicoleLam
 4     MEMO:找规律
 5 */
 6
 7 #include<cstdio>
 8 using namespace std;
 9 const int N=100;
10 int f[N];
11
12 int main()
13 {
14     int a,b,n;
15     scanf("%d%d%d",&a,&b,&n);
16
17     while ((a!=0) && (b!=0) && (n!=0))
18     {
19         f[1]=1,f[2]=1;
20         int i;
21         for (i=3;i<100;i++)
22         {
23             f[i] = (a*f[i-1] + b*f[i-2])  %  7;
24
25             if ( f[i-1]==1 && f[i]==1) break;
26         }
27         n=n%(i-2);
28         f[0]=f[i-2];
29         printf("%d\n",f[n]);
30         scanf("%d%d%d",&a,&b,&n);
31     }
32     return 0;
33 }
时间: 2024-12-22 20:11:34

找规律/hdu 1005 Number Sequence的相关文章

HDU 1005 Number Sequence (数学规律)

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

HDU 1005 Number Sequence(数论)

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

HDU 1005 Number Sequence【多解,暴力打表,鸽巢原理】

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

HDU 1005 Number Sequence(数列)

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) [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

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 cont

HDU 1005 Number Sequence 矩阵快速幂

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

NYOJ 427 &amp; HDU 1005 Number Sequence(找循环节)

[题目链接]click here~~ [题目大意]已经 f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.求f(n), [解题思路]:此题是大三届的一场比赛题,前几天做了下,结果是不忍直视啊,wa了几乎一页了,最开始想的是用矩阵快速幂,但是想了想,取模数才不到10,7 7=49,也就是说最大结果不超过49种可能,直接模拟递推式试试,结果发现不行,后来想到了用循环节,不难想到: 如果结果有两个答案连着 =1,则后面的全部和

HDU - 1005 Number Sequence(简单矩阵快速幂)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 题意:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. 就是这道题目,然而找了一晚上的错误 \("▔□▔)/\("▔□▔)/\("▔□▔)/. 1 #include <iostream> 2 #include <cstring> 3 using namespace std;

HDU 1005 Number Sequence (循环节)

首先暴力打表就很容易发现有循环节,于是一开始的写法是直接暴力找循环节,结果一直WA, 原因是有的循环并不是从1,1开始的,详细有证明戳这里:http://acm.hdu.edu.cn/discuss/problem/post/reply.php?postid=19818&messageid=1&deep=0 于是借鉴了大神的思路,因为%7,故可用v[7][7]来记录 f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.这个状态.若出现相同的状态,则证明出现