题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4686
题意:
思路:
#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <map>
#include <ctype.h>
#include <time.h>
#define abs(x) ((x)>=0?(x):-(x))
#define i64 long long
#define u32 unsigned int
#define u64 unsigned long long
#define clr(x,y) memset(x,y,sizeof(x))
#define CLR(x) x.clear()
#define ph(x) push(x)
#define pb(x) push_back(x)
#define Len(x) x.length()
#define SZ(x) x.size()
#define PI acos(-1.0)
#define sqr(x) ((x)*(x))
#define MP(x,y) make_pair(x,y)
#define EPS 1e-25
#define FOR0(i,x) for(i=0;i<x;i++)
#define FOR1(i,x) for(i=1;i<=x;i++)
#define FOR(i,a,b) for(i=a;i<=b;i++)
#define FORL0(i,a) for(i=a;i>=0;i--)
#define FORL1(i,a) for(i=a;i>=1;i--)
#define FORL(i,a,b)for(i=a;i>=b;i--)
#define rush() int CC;for(scanf("%d",&CC);CC--;)
#define Rush(n) while(scanf("%d",&n)!=-1)
using namespace std;
void RD(int &x){scanf("%d",&x);}
void RD(i64 &x){scanf("%I64d",&x);}
void RD(u64 &x){scanf("%I64u",&x);}
void RD(u32 &x){scanf("%u",&x);}
void RD(double &x){scanf("%lf",&x);}
void RD(int &x,int &y){scanf("%d%d",&x,&y);}
void RD(i64 &x,i64 &y){scanf("%lld%lld",&x,&y);}
void RD(u32 &x,u32 &y){scanf("%u%u",&x,&y);}
void RD(double &x,double &y){scanf("%lf%lf",&x,&y);}
void RD(double &x,double &y,double &z){scanf("%lf%lf%lf",&x,&y,&z);}
void RD(int &x,int &y,int &z){scanf("%d%d%d",&x,&y,&z);}
void RD(i64 &x,i64 &y,i64 &z){scanf("%I64d%I64d%I64d",&x,&y,&z);}
void RD(u32 &x,u32 &y,u32 &z){scanf("%u%u%u",&x,&y,&z);}
void RD(char &x){x=getchar();}
void RD(char *s){scanf("%s",s);}
void RD(string &s){cin>>s;}
void PR(int x) {printf("%d\n",x);}
void PR(int x,int y) {printf("%d %d\n",x,y);}
void PR(i64 x) {printf("%I64d\n",x);}
void PR(i64 x,i64 y) {printf("%lld %lld\n",x,y);}
void PR(u32 x) {printf("%u\n",x);}
void PR(u64 x) {printf("%llu\n",x);}
void PR(double x) {printf("%.10lf\n",x);}
void PR(double x,double y) {printf("%.5lf %.5lf\n",x,y);}
void PR(char x) {printf("%c\n",x);}
void PR(char *x) {printf("%s\n",x);}
void PR(string x) {cout<<x<<endl;}
void upMin(int &x,int y) {if(x>y) x=y;}
void upMin(i64 &x,i64 y) {if(x>y) x=y;}
void upMin(double &x,double y) {if(x>y) x=y;}
void upMax(int &x,int y) {if(x<y) x=y;}
void upMax(i64 &x,i64 y) {if(x<y) x=y;}
void upMax(double &x,double y) {if(x<y) x=y;}
const int mod=1000000007;
const i64 inf=((i64)1)<<60;
const double dinf=1e12;
const int INF=1000000001;
const int N=1005;
i64 n;
i64 f0,a,b;
i64 g0,c,d;
struct Matrix
{
i64 a[5][5];
void init(int x)
{
clr(a,0);
int i;
if(x) FOR0(i,5) a[i][i]=1;
}
Matrix operator*(Matrix p)
{
Matrix ans;
ans.init(0);
int i,j,k;
FOR0(k,5) FOR0(i,5) FOR0(j,5)
{
ans.a[i][j]+=a[i][k]*p.a[k][j]%mod;
ans.a[i][j]%=mod;
}
return ans;
}
Matrix Pow(i64 n)
{
Matrix p=*this,ans;
ans.init(1);
while(n)
{
if(n&1) ans=ans*p;
p=p*p;
n>>=1;
}
return ans;
}
};
Matrix p;
void init()
{
a%=mod; b%=mod; c%=mod; d%=mod;
p.init(0);
p.a[0][0]=1; p.a[0][1]=b; p.a[0][2]=d; p.a[0][3]=b*d%mod; p.a[0][4]=b*d%mod;
p.a[1][1]=a; p.a[1][3]=a*d%mod; p.a[1][4]=a*d%mod;
p.a[2][2]=c; p.a[2][3]=b*c%mod; p.a[2][4]=b*c%mod;
p.a[3][3]=a*c%mod; p.a[3][4]=a*c%mod;
p.a[4][4]=1;
}
int main()
{
while(scanf("%I64d",&n)!=-1)
{
RD(f0,a,b); RD(g0,c,d); f0%=mod; g0%=mod;
if(n==0)
{
puts("0");
continue;
}
i64 ans=0;
init();
p=p.Pow(n-1);
ans+=p.a[0][4]; ans%=mod;
ans+=f0*p.a[1][4]; ans%=mod;
ans+=g0*p.a[2][4]; ans%=mod;
ans+=f0*g0%mod*p.a[3][4]%mod; ans%=mod;
ans+=f0*g0%mod*p.a[4][4]%mod; ans%=mod;
PR(ans);
}
}