A题肥波纳妾数列,mod数为什么要-1 ?求解答
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<string>
#include<iomanip>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
//const double PI = 3.14159265;
//const double wc = 1e-7;
const int maxn = 100050;
ll sum[maxn], ans2, f;
int a, b, m, n;
bool pd = 0;
ll qpow(ll base, ll power) {
ll ans = 1;
while (power > 0) {
if (power & 1) {
ans = ans * base % mod;
}
power >>= 1;
base = base * base % mod;
}
return ans;
}
ll ff[maxn];
int main() {
//cin >> n;
scanf("%d%d%d%d", &a, &b, &m, &n);
ff[1] = 1;
ff[2] = 1;
for (int i = 3; i <= n; i++) {
ff[i] = (a*ff[i-1]+b*ff[i-2])%(mod-1);
}
//cout << "p=" << p << endl;
ans2 = qpow(m, ff[n]);
cout << ans2 << endl;
return 0;
}
#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<string>
#include<iomanip>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
//const double PI = 3.14159265;
//const double wc = 1e-7;
const int maxn = 100050;
ll sum[maxn], ans2, f;
int a, b, m, n;
bool pd = 0;
ll qpow(ll base, ll power) {
ll ans = 1;
while (power > 0) {
if (power & 1) {
ans = ans * base % mod;
}
power >>= 1;
base = base * base % mod;
}
return ans;
}
ll ff[maxn];
int main() {
//cin >> n;
scanf("%d%d%d%d", &a, &b, &m, &n);
ff[1] = 1;
ff[2] = 1;
for (int i = 3; i <= n; i++) {
ff[i] = (a*ff[i-1]+b*ff[i-2])%(mod-1);
}
//cout << "p=" << p << endl;
ans2 = qpow(m, ff[n]);
cout << ans2 << endl;
return 0;
}