hdu 3360

http://acm.hdu.edu.cn/showproblem.php?pid=3306

 

构造矩阵的方法很巧妙

 

#include <cstdlib>
#include <iostream>

using namespace std;
const int mod=10007;
typedef __int64 ll;
ll a[5][5],b[5][5],c[5][5];

void pow(ll a[][5],ll b[][5],ll c[][5])
{
   for(int i=1;i<=4;i++)    
   for(int j=1;j<=4;j++)
   {
       c[i][j]=0;
       for(int k=1;k<=4;k++)       
        c[i][j]=(c[i][j]+a[i][k]*b[k][j])%mod;
   }
}
int main(int argc, char *argv[])
{
    ll n,x,y;
    while(scanf("%I64d %I64d %I64d",&n,&x,&y)==3)
    {
          memset(a,0,sizeof(a));                  
          a[1][1]=a[1][4]=x*x%mod;
          a[2][1]=a[2][4]=y*y%mod;
          a[3][1]=a[3][4]=2*x*y%mod;
          a[1][2]=a[4][4]=1;
          a[1][3]=x%mod;
          a[3][3]=y%mod;
          memset(c,0,sizeof(c));
          c[1][1]=1;
          c[1][2]=1;
          c[1][3]=1;
          c[1][4]=2;
          n-=1;
          while(n)
          {
              if(n&1)
              {
                  pow(c,a,b);   
                  memcpy(c,b,sizeof(b));       
              }       
              pow(a,a,b);
              memcpy(a,b,sizeof(b));
              n>>=1;
          }
          printf("%I64d\n",c[1][4]%mod);
    }
   // system("PAUSE");
    return EXIT_SUCCESS;
}

 

你可能感兴趣的:(hdu 3360)