Number Sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 37463 Accepted Submission(s): 7885
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
Sample Output
Author
CHEN, Shunbao
Source
ZJCPC2004
Recommend
JGShining
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=3;
int mod=7;
class Matrix
{
int n,m;//矩阵的row and colum
int num[maxn][maxn];//从1开始
public:
Matrix(){memset(num,0,sizeof(num));}
Matrix(const Matrix &a) {n=a.n;m=a.m; memcpy(num,a.num,sizeof(a.num));}
Matrix(int x,int y):n(x),m(y){}
Matrix & operator =(const Matrix & a);
friend Matrix operator + (const Matrix &a,const Matrix &b);
friend Matrix operator - (const Matrix &a,const Matrix &b);
friend Matrix operator * (const Matrix &a,const Matrix &b);
friend Matrix operator % (const Matrix &a,int n);
friend ostream & operator <<(ostream & cout,const Matrix & a);
friend istream & operator >>(istream & cin,Matrix & a);
void Modefy_n_m(int x,int y,int v);
void out();//只在此题中有用,其他题目可删去
};
Matrix & Matrix:: operator =(const Matrix &a)
{
for(int i=1;i<=a.n;i++) for(int j=1;j<=a.m;j++) num[i][j]=a.num[i][j];
return *this;
}
istream & operator >>(istream & cin,Matrix & a)
{
for(int i=1;i<=a.n;i++)
{
for(int j=1;j<=a.m;j++)
{
cin>>a.num[i][j];
}
}
return cin;
}
ostream & operator <<(ostream & cout,const Matrix & a)
{
for(int i=1;i<=a.n;i++)
{
for(int j=1;j<=a.m;j++)
{
cout<<a.num[i][j]<<" ";
}
cout<<endl;
}
return cout;
}
Matrix operator % (const Matrix &a,int n)
{
Matrix t;
for(int i=1;i<=a.n;i++)
{
for(int j=1;j<=a.m;j++)
{
t.num[i][j]=a.num[i][j]%n;
}
}
return t;
}
Matrix operator * (const Matrix &a,const Matrix &b)
{
Matrix t(a.n,b.m);
for(int i=1;i<=a.n;i++)
{
for(int j=1;j<=b.m;j++)
{
int cnt=0;
for(int k=1;k<=a.m;k++)
{
cnt+=a.num[i][k]*b.num[k][j];
cnt%=mod;//这是此题需要,别的题目可删去
}
t.num[i][j]=cnt;
}
}
return t;
}
Matrix operator + (const Matrix &a,const Matrix &b)
{
Matrix t;
for(int i=1;i<=a.n;i++) for(int j=1;j<=a.m;j++) t.num[i][j]=a.num[i][j]+b.num[i][j];
return t;
}
Matrix operator - (const Matrix &a,const Matrix &b)
{
Matrix t;
for(int i=1;i<=a.n;i++) for(int j=1;j<=a.m;j++) t.num[i][j]=a.num[i][j]-b.num[i][j];
return t;
}
void Matrix:: Modefy_n_m(int x,int y,int v)
{
num[x][y]=v;
}
void Matrix::out()
{
printf("%d/n",num[1][2]);
}
Matrix E(2,2);
Matrix bin(Matrix cnt,int n)//cnt
{
if(n==0) return E;
if(n==1) return cnt;
Matrix temp=bin(cnt,n/2);
temp=temp*temp;
if(n&1)
{
temp=temp*cnt;
return temp;
}
else
{
return temp;
}
}
int main()
{
E.Modefy_n_m(1,1,1);E.Modefy_n_m(1,2,0);
E.Modefy_n_m(2,1,0);E.Modefy_n_m(2,2,1);
Matrix pre(1,2);
pre.Modefy_n_m(1,1,1);pre.Modefy_n_m(1,2,1);
int a,b,n;
while(scanf("%d%d%d",&b,&a,&n)==3&&n)
{
if(n<3) {printf("1/n");continue;}
n-=2;
Matrix cnt(2,2);
cnt.Modefy_n_m(1,1,0);cnt.Modefy_n_m(1,2,a);
cnt.Modefy_n_m(2,1,1);cnt.Modefy_n_m(2,2,b);
Matrix temp=bin(cnt,n);
Matrix ans(1,2);
ans=pre*temp;
ans.out();
}
return 0;
}