@wsndy-xx
2018-05-20T10:48:37.000000Z
字数 1204
阅读 887
题解
求 数列的第 项对 取余的值。
由于数据非常大,所以 的递推显然不能满足要求
矩阵加速模板题
#include <iostream>
#include <cstdio>
#include <cstring>
const int N = 10;
const int Mod = 1e9 + 7;
int n;
struct Node{
long long m[N][N];
Node() {memset(m,0,sizeof m);}
void Clear() {for(int i = 1; i <= 3; i ++) m[i][i] = 1;}
};
Node operator * (const Node & a, const Node & b) {
Node ret;
for(int i = 1; i <= 3; i ++)
for(int j = 1; j <= 3; j ++)
for(int k = 1; k <= 3; k ++)
ret.m[i][j] = (ret.m[i][j] + a.m[i][k] * b.m[k][j] % Mod) % Mod;
return ret;
}
Node Ksm(Node a, int Pow) {
Node ret; ret.Clear();
while(Pow) {
if(Pow & 1) ret = ret * a;
a = a * a;
Pow >>= 1;
}
return ret;
}
int main() {
std:: cin >> n;
for(int i = 1; i <= n; i ++) {
int Pow; std:: cin >> Pow; Pow -= 3;
if(Pow < 1) {std:: cout << 1 <<"\n"; continue ;}
Node A;
A.m[1][1] = 0, A.m[1][2] = 0, A.m[1][3] = 1;
A.m[2][1] = 1, A.m[2][2] = 0, A.m[2][3] = 0;
A.m[3][1] = 0, A.m[3][2] = 1, A.m[3][3] = 1;
Node B = Ksm(A, Pow);
Node Ans;
Ans.m[1][1] = Ans.m[1][2] = Ans.m[1][3] = 1;
Ans = Ans * B;
std:: cout << Ans.m[1][3] << "\n";
}
return 0;
}