#include <bits/stdc++.h>using namespace std;
const long long MOD = 1e9 + 7;
class ju {
public:long long h, l;
long long *juzhen;
ju(long long hang, long long lie): h(hang), l(lie) {
juzhen = new long long[h * l]();
}
ju(long long hang, long long lie, initializer_list<long long> values): h(hang), l(lie) {
juzhen = new long long[h * l];
long long i = 0;
for(auto it = values.begin(); it != values.end() && i < h*l; ++it, ++i) {
juzhen[i] = *it % MOD;
}
}
~ju() {
delete[] juzhen;
}
void setValues(initializer_list<long long> values) {
long long i = 0;
for(auto it = values.begin(); it != values.end() && i < h*l; ++it, ++i) {
juzhen[i] = *it % MOD;
}
}
long long& at(long long row, long long col) {
return juzhen[row * l + col];
}
void print() {
for(long long i = 0; i < h; i++) {
for(long long j = 0; j < l; j++) {
cout << juzhen[i * l + j] << " ";
}
cout << endl;
}
}
ju operator + (ju& others) {
if(h != others.h || l != others.l) {
throw invalid_argument("Matrix dimensions must match for addition");
}
ju temp(h, l);
for (long long i = 0; i < h * l; i++) {
temp.juzhen[i] = (juzhen[i] + others.juzhen[i]) % MOD;
}
return temp;
}
ju operator - (ju& others) {
if(h != others.h || l != others.l) {
throw invalid_argument("Matrix dimensions must match for addition");
}
ju temp(h, l);
for (long long i = 0; i < h * l; i++) {
temp.juzhen[i] = (juzhen[i] - others.juzhen[i] + MOD) % MOD;
}
return temp;
}
ju operator * (ju& others) {
if(l != others.h) {
throw invalid_argument("Matrix dimensions incompatible for multiplication");
}
ju result(h, others.l);
for(long long i = 0; i < h; i++) {
for(long long j = 0; j < others.l; j++) {
long long sum = 0;
for(long long k = 0; k < l; k++) {
sum = (sum + juzhen[i * l + k] * others.juzhen[k * others.l + j]) % MOD;
}
result.juzhen[i * others.l + j] = sum;
}
}
return result;
}
ju operator * (long long others) {
ju result(h, l);
for(long long i = 0; i < h * l; i++) {
result.juzhen[i] = (juzhen[i] * (others % MOD)) % MOD;
}
return result;
}
ju(const ju& other): h(other.h), l(other.l) {
juzhen = new long long[h * l];
for(long long i = 0; i < h * l; i++) {
juzhen[i] = other.juzhen[i] % MOD;
}
}
ju& operator=(const ju& other) {
if(this != &other) {
delete[] juzhen;
h = other.h;
l = other.l;
juzhen = new long long[h * l];
for(long long i = 0; i < h * l; i++) {
juzhen[i] = other.juzhen[i] % MOD;
}
}
return *this;
}
};
ju change(3, 3, {1, 0, 1, 1, 0, 0, 0, 1, 0});
ju zh(3, 1, {1, 1, 1});
int main() {
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
if (n <= 3) {
cout << 1 << '\n';
} else {
ju result(3, 3);
for(int i = 0; i < 3; i++) result.at(i,i) = 1;
ju base = change;
long long power = n - 3;
while(power > 0) {
if(power & 1) {
result = result * base;
}
base = base * base;
power >>= 1;
}
ju final = result * zh;
cout << final.juzhen[0] % MOD << '\n';
}
}
return 0;
}
如果你提交,会发现只对了三个测试点,其它都是TLE。这时就要用到快速幂了。
AC Code
C++
#include <bits/stdc++.h>using namespace std;
const long long MOD = 1e9 + 7;
class ju {
public:long long h, l;
long long *juzhen;
ju(long long hang, long long lie): h(hang), l(lie) {
juzhen = new long long[h * l]();
}
ju(long long hang, long long lie, initializer_list<long long> values): h(hang), l(lie) {
juzhen = new long long[h * l];
long long i = 0;
for(auto it = values.begin(); it != values.end() && i < h*l; ++it, ++i) {
juzhen[i] = *it % MOD;
}
}
~ju() {
delete[] juzhen;
}
void setValues(initializer_list<long long> values) {
long long i = 0;
for(auto it = values.begin(); it != values.end() && i < h*l; ++it, ++i) {
juzhen[i] = *it % MOD;
}
}
long long& at(long long row, long long col) {
return juzhen[row * l + col];
}
void print() {
for(long long i = 0; i < h; i++) {
for(long long j = 0; j < l; j++) {
cout << juzhen[i * l + j] << " ";
}
cout << endl;
}
}
ju operator + (ju& others) {
if(h != others.h || l != others.l) {
throw invalid_argument("Matrix dimensions must match for addition");
}
ju temp(h, l);
for (long long i = 0; i < h * l; i++) {
temp.juzhen[i] = (juzhen[i] + others.juzhen[i]) % MOD;
}
return temp;
}
ju operator - (ju& others) {
if(h != others.h || l != others.l) {
throw invalid_argument("Matrix dimensions must match for addition");
}
ju temp(h, l);
for (long long i = 0; i < h * l; i++) {
temp.juzhen[i] = (juzhen[i] - others.juzhen[i] + MOD) % MOD;
}
return temp;
}
ju operator * (ju& others) {
if(l != others.h) {
throw invalid_argument("Matrix dimensions incompatible for multiplication");
}
ju result(h, others.l);
for(long long i = 0; i < h; i++) {
for(long long j = 0; j < others.l; j++) {
long long sum = 0;
for(long long k = 0; k < l; k++) {
sum = (sum + juzhen[i * l + k] * others.juzhen[k * others.l + j]) % MOD;
}
result.juzhen[i * others.l + j] = sum;
}
}
return result;
}
ju operator * (long long others) {
ju result(h, l);
for(long long i = 0; i < h * l; i++) {
result.juzhen[i] = (juzhen[i] * (others % MOD)) % MOD;
}
return result;
}
ju(const ju& other): h(other.h), l(other.l) {
juzhen = new long long[h * l];
for(long long i = 0; i < h * l; i++) {
juzhen[i] = other.juzhen[i] % MOD;
}
}
ju& operator=(const ju& other) {
if(this != &other) {
delete[] juzhen;
h = other.h;
l = other.l;
juzhen = new long long[h * l];
for(long long i = 0; i < h * l; i++) {
juzhen[i] = other.juzhen[i] % MOD;
}
}
return *this;
}
};
ju change(3, 3, {1, 0, 1, 1, 0, 0, 0, 1, 0});
ju zh(3, 1, {1, 1, 1});
int main() {
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
if (n <= 3) {
cout << 1 << '\n';
} else {
ju result(3, 3);
for(int i = 0; i < 3; i++) result.at(i,i) = 1;
ju base = change;
long long power = n - 3;
while(power > 0) {
if(power & 1) {
result = result * base;
}
base = base * base;
power >>= 1;
}
ju final = result * zh;
cout << final.juzhen[0] << '\n';
}
}
return 0;
}