60 pts求助,WA on #2#8#9#10
查看原帖
60 pts求助,WA on #2#8#9#10
1010629
qwqSW楼主2024/10/24 20:59
#include<iostream>
#include<cstdio>
using namespace std;

long long n,m,a,c,X0,g;

struct sw{
	long long mx[25][25];
}SW;

long long qmul(long long a,long long b){
	long long ans=0;
	while(b){
		if(b&1) ans=(ans+a)%m;
		a=(a+a)%m;
		b>>=1;
	}
	return ans;
}

sw operator *(const sw a,const sw b){
	sw c;
	for(int i=1;i<=2;i++){
		for(int j=1;j<=2;j++){
			c.mx[i][j]=0;
		}
	}
	for(int i=1;i<=2;i++){
		for(int j=1;j<=2;j++){
			for(int p=1;p<=2;p++){
				c.mx[i][j]=(c.mx[i][j]+qmul(a.mx[i][p],b.mx[p][j]))%m;
			}
		}
	}
	return c;
}

sw qpow(sw a,long long k){
	sw sww;
	for(int i=1;i<=2;i++){
		for(int j=1;j<=2;j++){
			if(i==j) sww.mx[i][j]=1;
			else sww.mx[i][j]=0;
		}
	}
	while(k){
		if(k&1) sww=(sww*a);
		a=(a*a);
		k>>=1;
	}
	return sww;
}

int main(){
	//freopen("random.in","r",stdin);
	//freopen("random.out","w",stdout);
	scanf("%lld%lld%lld%lld%lld%lld",&m,&a,&c,&X0,&n,&g);
	if(n==0){
		printf("%lld",X0);
		return 0;
	}
	SW.mx[1][1]=a;
	SW.mx[2][1]=SW.mx[2][2]=1;
	sw ans=qpow(SW,n);
	//for(int i=1;i<=2;i++){
	//	for(int j=1;j<=2;j++){
	//		cout<<ans.mx[i][j]<<" ";
	//	}
	//	cout<<endl;
	//}
	long long res=(X0*ans.mx[1][1]%m+c*ans.mx[2][1]%m)%m;
	printf("%lld",res%g);
	return 0;
}
2024/10/24 20:59
加载中...