題目
思路
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);
注意:
當(dāng)n<=7 的時(shí)候直接暴力或打表
注意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) //實(shí)現(xiàn) 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;
}
}
}
// 下邊要實(shí)現(xiàn) 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;
}
反思
這次訓(xùn)練爆零了,前幾題完全看不懂,寫d題寫了半天發(fā)現(xiàn)思路完全理不清是趴,心態(tài)爆炸,e題推完之后沒考慮其他情況澄惊,公式有問題沒有發(fā)現(xiàn)唆途,全程爆炸富雅。