poj2696

简单dp

View Code
   
     
#include < iostream >
#include
< cstdio >
#include
< cstdlib >
#include
< cstring >
using namespace std;

int main()
{
// freopen("t.txt", "r", stdin);
int t;
scanf(
" %d " , & t);
while (t -- )
{
int a, b, c, d, e, f, g, h, n, fun[ 1005 ];
scanf(
" %d%d%d%d%d%d%d%d%d " , & a, & b, & c, & d, & e, & f, & g, & h, & n);
fun[
0 ] = a;
fun[
1 ] = b;
fun[
2 ] = c;
for ( int i = 3 ; i <= n; i ++ )
{
if (i & 1 )
{
fun[i]
= ((d * fun[i - 1 ] + e * fun[i - 2 ] - f * fun[i - 3 ])
% g + g) % g;
}
else
{
fun[i]
= ((f * fun[i - 1 ] - d * fun[i - 2 ] + e * fun[i - 3 ])
% h + h) % h;
}
}
printf(
" %d\n " , fun[n]);
}
return 0 ;
}

你可能感兴趣的:(poj)