【十進制矩陣快速冪】 2019牛客暑期多校訓練營(第五場) B

原文:https://blog.csdn.net/henucm/article/details/98492753

題意:給你x0、x1 a、b、mod,根據 求出\large x_{n}

根據上述公式,再套用十進制矩陣快速冪即可(二進制好像會超時...)

用十進制

設 \large \tbinom{a \ \ \ b}{1\ \ \ 0} 爲res 則可得 \large ans=res^{n[i]-'0'}

我們再設\large res=res^{10*(i-1)}

就是分解n爲每一位,再去相乘。

例如 \large res^{298}=res^8*res^{10*9}*res^{100*2}

講計算出來的\large res.m[1][0]*x1+res.m[1][1]*x0即可


#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
//typedef __int128 bll;
const ll maxn = 1e6+100;
//const ll mod = 1e9+7;
const ld pi = acos(-1.0);
const ll inf = 1e18;
const ld eps = 1e-5;
const ld e = exp(1);

ll x0,x1,a,b,mod;
char str[maxn];

struct mat
{
	ll m[2][2];
};

mat mul(mat a,mat b)
{
	mat res;
	for(int i = 0; i < 2; i++)
		for(int j = 0; j < 2; j++)
			res.m[i][j] = 0;	
	
	for(int i = 0; i < 2; i++)
	{
		for(int j = 0; j < 2; j++)
		{
			for(int k = 0; k < 2; k++)
			{
				res.m[i][j] = (res.m[i][j]%mod + a.m[i][k]*b.m[k][j]%mod )%mod;
			}
		}
	}
	
	return res;
	
}

mat qpow(mat a,int b)
{
	mat e;
	
	for(int i = 0; i < 2; i++)
	{
		for(int j = 0; j < 2; j++)
		{
			i == j? e.m[i][j] = 1 : e.m[i][j] = 0;
		}
	}
	
	while(b)
	{
		if(b&1)
			e = mul(e,a);		
		a = mul(a,a);
		b = b >> 1;
	}
	return e;
}

mat qpow10()
{
	mat x,e;
	x.m[0][0] = a;	x.m[0][1] = b;
	x.m[1][0] = 1;  x.m[1][1] = 0;
	
	for(int i = 0; i < 2; i++)
	{
		for(int j = 0; j < 2; j++)
		{
			i == j? e.m[i][j] = 1 : e.m[i][j] = 0;
		}
	}
	
	int len = strlen(str+1);
	for(int i = len; i >= 1; i--)
	{
		int tmp = str[i]-'0';
		if(tmp > 0)
		{
			mat t = qpow(x,tmp);
			
			e = mul(e,t);
		}
		x = qpow(x,10);
	}
	
	return e;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	
	cin >> x0 >> x1 >> a >> b >> str+1 >> mod;
	
	mat res = qpow10();
	
	//cout << (res.m[0][0]%mod) << endl;
	
	cout <<  ( (res.m[1][0]*x1%mod)+(res.m[1][1]*x0%mod) )%mod << endl;
	
	
	return 0;	
} 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章