标签:
#include <iostream> #include <cstdio> #include <cstring> #define REP(i, s, n) for(int i = s; i <= n; i ++) #define REP_(i, s, n) for(int i = n; i >= s; i --) #define MAX_N 3 #define mod 7 using namespace std; int a, b, n; struct node{ int Mtx[MAX_N][MAX_N]; }tmp, coe; node operator+(node a,node b){ node c; REP(i, 1, 2) REP(j, 1, 2){ c.Mtx[i][j] = (a.Mtx[i][j] + b.Mtx[i][j]) % mod; } return c; } node operator*(node a,node b){ node c; REP(i, 1, 2) REP(j, 1, 2){ c.Mtx[i][j] = 0; REP(k, 1, 2) c.Mtx[i][j] = (c.Mtx[i][j] + a.Mtx[i][k] * b.Mtx[k][j]) % mod; } return c; } node operator^(node a,int k){ if(k == 0){ memset(a.Mtx, 0, sizeof(a.Mtx)); REP(i, 1, 2) a.Mtx[i][i] = 1; return a; } if(k == 1) return a; node c = a ^ (k >> 1); if(k & 1) return c * c * a; return c * c; } int main(){ while(scanf("%d%d%d", &a, &b, &n) != EOF){ if(a == 0 && b == 0 && n == 0) break; if(n == 1) { cout << 1 << endl; continue; } if(n == 2) { cout << 1 << endl; continue; } else { tmp.Mtx[1][1] = a * 1 + b * 1; tmp.Mtx[1][2] = 1; tmp.Mtx[2][1] = 1; tmp.Mtx[2][2] = 1; coe.Mtx[1][1] = a; coe.Mtx[1][2] = 1; coe.Mtx[2][1] = b; coe.Mtx[2][2] = 0; n -= 3; coe = coe ^ n; tmp = tmp * coe; cout << tmp.Mtx[1][1] << endl; } } return 0; }
标签:
原文地址:http://www.cnblogs.com/ALXPCUN/p/4588218.html