第一天
第二天
第三天
第四天
第五天
所以第n天就是
(感谢刘晟大佬指正以及帮忙debug)
这个题还挺恶心的:
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll mod = 1e9 + 7; struct node { //矩阵类 ll matrix[2][2]; node() { memset(matrix, 0, sizeof(matrix)); } //初始化 void one() { matrix[0][0] = 1; matrix[1][1] = 1; } //单位矩阵E node operator*(node other) { //对*重载 定义矩阵乘法 node ans; //记录乘积 for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) for (int k = 0; k < 2; k++) { ans.matrix[i][j] += (matrix[i][k] * other.matrix[k][j]) % (mod-1); ans.matrix[i][j] %= (mod-1); } return ans; } }; node power(node a, ll b) { //快速幂 矩阵a的b次方 node res; res.one(); //单位矩阵 while (b) { if (b & 1) res = res * a; a = a * a; b >>= 1; } return res; } ll qkpow(ll a, ll b) { ll ans = 1; while (b) { if (b & 1) ans = ans*a % mod; a = a*a % mod; b >>= 1; } return ans; } int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); node temp; ll n, x, y, a, b, flag = 0; cin >> n >> x >> y >> a >> b; x %= mod, y %= mod, a %= mod; if (n == 1) return cout << x, 0; if (n == 2) return cout << y, 0; if (x == 0 || y == 0 || a == 0) return cout << 0, 0; temp.matrix[0][0] = 1; temp.matrix[0][1] = 1; temp.matrix[1][0] = 1; temp.matrix[1][1] = 0; b%=(mod-1); node A = power(temp, n - 2); ll fib2 = A.matrix[1][0]; ll fib1 = A.matrix[0][0]; ll fib0 = (fib1+fib2-1)%(mod-1)*b%(mod-1); ll ans = qkpow(x, fib2); ans = ans* qkpow(y, fib1)%mod*qkpow(a,fib0)%mod; cout << ans; return 0; }
def mul(a, b): # 首先定义二阶矩阵乘法运算 c = [[0, 0], [0, 0]] # 定义一个空的二阶矩阵,存储结果 mod=1000000006 for i in range(2): # row for j in range(2): # col for k in range(2): # 新二阶矩阵的值计算 c[i][j] += a[i][k] * b[k][j]%mod;c[i][j]%=mod return c n,x,y,a,b=map(int,input().split()) mod=1000000007 x%=mod;y%=mod;a%=mod;b%=(mod-1) if n==1:print(x) elif n==2:print(y) elif x%mod==0 or y%mod==0 or a%mod==0:print(0) else: m=n-2 res = [[1, 0], [0, 1]] A = [[1, 1], [1, 0]] while m: if m & 1: res = mul(res, A) A = mul(A, A) m >>= 1 fib2=res[0][1] fib1=res[0][0] fib0=(fib1+fib2-1)%1000000006 c=fib0*b%1000000006 ans=(pow(x,fib2,mod)*pow(y,fib1,mod)*pow(a,c,mod))%mod print(ans)