题目:GCJ Round 1A 2008 Problem C. Numbers
思路:构造矩阵水题
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <vector>
#define eps (1e-10)
using namespace std;
#define mod 1000
struct Matrix
{
int m[3][3];
}E,D;
void init()
{
for(int i=1;i<=2;i++)
for(int j=1;j<=2;j++)
E.m[i][j]=(i==j);
D.m[1][1]=3;
D.m[1][2]=5;
D.m[2][1]=1;
D.m[2][2]=3;
}
Matrix Multi(Matrix A,Matrix B)
{
Matrix ans;
for(int i=1;i<=2;i++)
for(int j=1;j<=2;j++)
{
ans.m[i][j]=0;
for(int k=1;k<=2;k++)
ans.m[i][j]=(ans.m[i][j]+A.m[i][k]*B.m[k][j])%mod;
}
return ans;
}
Matrix Pow(Matrix A,long long k)
{
Matrix ans=E;
while(k)
{
if(k&1)
{
k--;
ans=Multi(ans,A);
}
else
{
k/=2;
A=Multi(A,A);
}
}
return ans;
}
int main()
{
freopen("C-large-practice.in","r",stdin);
freopen("C-large-practice.out","w",stdout);
init();
int t;
scanf("%d",&t);
for(int cas=1;cas<=t;cas++)
{
long long n;
scanf("%I64d",&n);
Matrix ans=Pow(D,n);
printf("Case #%d: %03d\n",cas,((ans.m[1][1]*2-1)%mod+mod)%mod);
}
return 0;
}