题目
思路
fib(i) = f(i - 1) + f(i - 2); fib(1) = 1; fib(2) = 2;
f(x + 1) = f(x) + f(x - 1) + sin(pi*x/2); f(1), (2)由输入给出
fibi(i) = fibi(i - 1) + fibi(i - 2); fibi(1) = f(1), fibi(2) = f(2);
推出公式得
f(i) = fibi(i) - fib((i - 4) / 2)^2 // i & 1 == 0;
f(i) = fibi(i) - fib((i - 4) / 2) * fib((i - 3) / 2);
注意:
当n<=7 的时候直接暴力或打表
注意mod 1e9 + 7;
代码
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;
typedef long long LL;
const long long SMod = 1e9 + 7;
LL t1, t2;
const LL MOD = 1e9 + 7;
LL d[4]={0,1,0,-1};
LL f1,f2,n;
LL fast_mod(LL n) // 求 (t^n)%MOD
{
LL t[2][2] = {1, 1, 1, 0};
LL ans[2][2] = {1, 0, 0, 1}; // 初始化为单位矩阵
LL tmp[2][2]; //自始至终都作为矩阵乘法中的中间变量
while(n)
{
if(n & 1) //实现 ans *= t; 其中要先把 ans赋值给 tmp,然后用 ans = tmp * t
{
for(LL i = 0; i < 2; ++i)
for(LL j = 0; j < 2; ++j)
tmp[i][j] = ans[i][j];
ans[0][0] = ans[1][1] = ans[0][1] = ans[1][0] = 0; // 注意这里要都赋值成 0
for(LL i = 0; i < 2; ++i) // 矩阵乘法
{
for(LL j = 0; j < 2; ++j)
{
for(LL k = 0; k < 2; ++k)
ans[i][j] = (ans[i][j] + tmp[i][k] * t[k][j]) % MOD;
}
}
}
// 下边要实现 t *= t 的操作,同样要先将t赋值给中间变量 tmp ,t清零,之后 t = tmp* tmp
for(LL i = 0; i < 2; ++i)
for(LL j = 0; j < 2; ++j)
tmp[i][j] = t[i][j];
t[0][0] = t[1][1] = 0;
t[0][1] = t[1][0] = 0;
for(LL i = 0; i < 2; ++i)
{
for(LL j = 0; j < 2; ++j)
{
for(LL k = 0; k < 2; ++k)
t[i][j] = (t[i][j] + tmp[i][k] * tmp[k][j]) % MOD;
}
}
n >>= 1;
}
return ans[0][1];
}
void solve(){
LL te[8];
te[1] = t1;
te[2] = t2;
te[3] = t1 + t2;
te[4] = te[3] + te[2] - 1;
te[5] = te[4] + te[3];
te[6] = te[5] + te[4] + 1;
te[7] = te[6] + te[5];
LL ans;
if(n <= 7){
ans = te[n];
}
else {
LL fn1 = fast_mod(n - 1);
LL fn2 = fast_mod(n - 2);
ans = fn1 * t2 % MOD + fn2 * t1 % MOD;
ans %= MOD;
LL temp = n - 4;
LL b;
if(temp % 2 == 0){
LL fn1 = fast_mod(temp / 2 - 1);
LL fn2 = fast_mod(temp / 2 - 2);
b = fn1 * 2 % MOD + fn2 * 1 % MOD;
b %= MOD;
b *= b;
b %= MOD;
}
else {
LL fn1 = fast_mod(temp / 2 - 1);
LL fn2 = fast_mod(temp / 2 - 2);
b = fn1 * 2 % MOD + fn2 * 1 % MOD;
b %= MOD;
fn1 = fast_mod(temp / 2);
fn2 = fast_mod(temp / 2 - 1);
b *= (fn1 * 2 % MOD + fn2 * 1 % MOD);
b %= MOD;
}
ans = (ans + SMod - b) % SMod;
}
printf("%lld\n", ans);
}
int main(){
while(scanf("%lld%lld%lld", &t1, &t2, &n) != EOF){
solve();
}
return 0;
}
反思
这次训练爆零了,前几题完全看不懂,写d题写了半天发现思路完全理不清,心态爆炸,e题推完之后没考虑其他情况,公式有问题没有发现,全程爆炸。