Number Sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 178891 Accepted Submission(s): 44454
Total Submission(s): 178891 Accepted Submission(s): 44454
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).
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的范围很大,所以,常规的递归肯定会超时,所以这是一道规律题……因为结果跟A B有关,所以x[i]的值肯定跟x[i-1]和x[i-2]有关,我猜测1 ,1 一定是这段循环体的开头,所以我就用for循环找,直到从新找到1,1这个循环体的开头,进而求出循环体的长度,然后n对长度取余,进而求解,但是提交之后wrong了……
后来,我写了一段代码,模拟了当A,B任意取值时,x[i]的记过。发现了一段特殊的循环体“0”,当A,B的值都是7的倍数时,x[i]的值从第三项起都是0,我把这个特殊的循环体给特意写出来就直接过了……
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<string>
#include<math.h>
#include<map>
#include<queue>
#include<stack>
#define INF 0x3f3f3f3f
#define ll long long
#define f(i,a,b) for(int i=a;i<b;i++)
#define sf(a) scanf("%d",&a)
#define sff(a,b) scanf("%d%d",&a,&b)
#define sfff(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define pf(a) printf("%d\n",a)
#define p printf("\n")
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
#define mod 1000000
int x[10000];
int main()
{
int a,b,n,c=1;
while(~sfff(a,b,n)&&(a,b,n))
{
if(a%7==0&&b%7==0)
{
if(n>2)pf(0);
if(n<=2)pf(1);
continue;
}
x[1]=1,x[2]=1;
f(i,3,10000)
{
x[i]=(a*x[i-1]+x[i-2]*b)%7;
if(x[i]==1)
{
if(x[i-1]==1)
{
c=i-2;
break;
}
}
}
int dd=n%c;
if(dd)pf(x[dd]);
else pf(x[c]);
}
}