链接:https://ac.nowcoder.com/acm/contest/885/B
来源:牛客网
时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
You are given four positive integers x0,x1,a,bx_0, x_1, a, bx0,x1,a,b. And you know xi=a⋅xi−1+b⋅xi−2x_i = a \cdot x_{i-1} + b \cdot x_{i-2}xi=a⋅xi−1+b⋅xi−2 for all i≥2i \ge 2i≥2.
Given two positive integers n, and MOD, please calculate xnx_nxn modulo MOD.
Does the problem look simple? Surprise! The value of n may have many many digits!
输入描述:
The input contains two lines. The first line contains four integers x0,x1,a,bx_0, x_1, a, bx0,x1,a,b (1≤x0,x1,a,b≤1091 \le x_0, x_1, a, b \le 10^91≤x0,x1,a,b≤109). The second line contains two integers n, MOD (1≤n<10(106),109<MOD≤2×1091 \le n < 10^{(10^6)}, 10^9 < MOD \le 2 \times 10^91≤n<10(106),109<MOD≤2×109, n has no leading zero).
输出描述:
Print one integer representing the answer.
示例1
输入
复制
1 1 1 1 10 1000000001
输出
复制
89
说明
The resulting sequence x is Fibonacci sequence. The 11-th item is 89.
示例2
输入
复制
1315 521 20185 5452831 9999999999999999999999999999999999999 1000000007
输出
复制
914730061
题意:求广义菲波那切数列的第n项,因为n非常大,所以需要找循环节,或者二进制转10进制运算,后者写不好容易T,广义斐波那契数列的循环节是啥,不要问我为啥,+1大神找的神仙规律,代码里有:
第一种做法(循环节):
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
const int MAX = 1e5+10;
ll p[MAX];
bool vis[MAX];
int cnt;
void init(){
vis[0]=vis[1]=true;
for (ll i = 2; i < MAX;i++){
if(!vis[i]) p[cnt++]=i;
for (ll j = 0; j < cnt && i*p[j] < MAX;j++){
vis[i*p[j]]=true;
if(i%p[j]==0) break;
}
}
}
struct hh{
ll a[20][20];
};
ll mull(ll a,ll b,ll c){//快速乘
ll ans=0;
while(b){
if(b&1) ans=(ans%c+a%c)%c;
b>>=1;
a=(a<<1)%c;
}
return ans;
}
hh mul(hh x,hh y,int c){
hh w;
memset(w.a,0,sizeof(w.a));
for (int i = 0; i < 2;i++){
for (int j = 0; j < 2;j++){
for (int k = 0; k < 2;k++){
w.a[i][j]=(w.a[i][j]%c+mull(x.a[i][k],y.a[k][j],c)%c)%c;
}
}
}
return w;
}
hh j_quick(hh a,ll b,ll c){
hh ans;
memset(ans.a,0,sizeof(ans.a));
for (int i = 0; i < 2;i++){
ans.a[i][i]=1;
}
while(b!=0){
if(b&1) ans=mul(ans,a,c);
b>>=1;
a=mul(a,a,c);
}
return ans;
}
ll ans1=1;
ll ans2=1;
ll fenjie(ll n){
ll ww=n;
for (int i = 0; i < cnt&&p[i]*p[i]<= n;i++){
if(n%p[i]==0){
ans1*=p[i];//找寻环节
ans2*=(p[i]-1)*(p[i]+1);//找寻环节
while(n%p[i]==0){
n/=p[i];
}
}
if(n==1) break;
}
if(n>1){
ans1*=n;//找寻环节
ans2*=(n-1)*(n+1);//找寻环节
}
return ww/ans1*ans2;//找寻环节
}
int main(){
init();
ll x0,x1,aa,bb,m;
string x;
cin >> x0 >> x1 >> aa >> bb;
cin >> x;
scanf("%lld",&m);
ll mm=fenjie(m);
ll n=0;
for (int i = 0; i < x.size();i++){
n=mull(n,10ll,mm);
n+=(x[i]-'0');
n%=mm;
}
hh cao;
cao.a[0][0]=aa; cao.a[0][1]=bb; cao.a[1][0]=1; cao.a[1][1]=0;
hh b = j_quick(cao,n-1,m);
printf("%lld\n",(mull(x1,b.a[0][0],m)+mull(x0,b.a[0][1],m))%m);
return 0;
}
第二种做法(二进制转成10进制):
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
const int MAX = 1e6+10;
char s[MAX];
struct hh{
ll a[2][2];
};
//不能用快速乘,超时~~,不用也不爆~~因为long long最大值是:9223372036854775807 约是:9e18
hh mul(hh x,hh y,ll c){
hh w;
memset(w.a,0,sizeof(w.a));
for (int i = 0; i < 2;i++){
for (int j = 0; j < 2;j++){
for (int k = 0; k < 2;k++){
w.a[i][j]=(w.a[i][j]+x.a[i][k]*y.a[k][j])%c;
}
}
}
return w;
}
int main(){
ll x0,x1,aa,bb,m;
scanf("%lld%lld%lld%lld",&x0,&x1,&aa,&bb);
scanf("%s",s);
scanf("%lld",&m);
int len=strlen(s);
hh cao,ans,ok;
cao.a[0][0]=aa; cao.a[0][1]=1; cao.a[1][0]=bb; cao.a[1][1]=0;
ans.a[0][0]=1; ans.a[0][1]=0; ans.a[1][0]=0; ans.a[1][1]=1;
for (int i = len-1; i >= 0;i--){//二进制转10进制
int fuck=s[i]-'0';
for (int j = 1; j <= fuck;j++) ans=mul(ans,cao,m);
ok.a[0][0]=1; ok.a[0][1]=0; ok.a[1][0]=0; ok.a[1][1]=1;
for (int j = 1; j <= 10;j++) ok=mul(ok,cao,m);
cao=ok;
}
printf("%lld\n",(x0*ans.a[1][1]+x1*ans.a[0][1])%m);
return 0;
}