2019牛客暑期多校训练营(第五场)B generator 1 【10进制矩阵快速幂】【斐波那契循环节】

时间限制: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

题目链接:

https://ac.nowcoder.com/acm/contest/885/B

题意:

给你五个数字 x_{0},x_{1},a,b, mod,x_{i} = a * x_{i-1} + b * x_{i-2}

让你求出x_{n}对mod取模,注意第n项的这个n特别大,要用字符串接收

1\leqslant n< 10^{(10^{6})} ,10^{9}< mod\leq 2*10^{9}

题解:

法1:

首先先构造矩阵,然后跑十进制的矩阵快速幂即可

还有种方法是有循环结

矩阵乘法不满足交换律,所以写代码的时候注意谁前谁后

法2:

打表可以发现,mod数的结果存在循环节,并且符合积性函数的性质,即:

1、f(a ) = (a - 1)*(a +1)(a为质数)

2、f(a*b) = f(a) * f(b) (a , b 互质)

3、f(a*b) = f(a) * b (a%b==0)

所以,先求出循环节,然后快速幂,注意要用到快速乘

代码:

10进制矩阵快速幂

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
using namespace std;
#define ll long long
const int M = 1e9+7;
ll a1,a2,a,b,n,mod;
string str;
struct Matrix {
	long long a[2][2];
	Matrix() {
		memset(a, 0, sizeof(a));
	}
	Matrix operator * (const Matrix y) {
		Matrix ans;
		for(int i = 0; i <= 1; i++)
			for(int j = 0; j <= 1; j++)
				for(int k = 0; k <= 1; k++)
					ans.a[i][j] += (a[i][k]*y.a[k][j])%mod;
		for(int i = 0; i <= 1; i++)
			for(int j = 0; j <= 1; j++)
				ans.a[i][j] %= mod;
		return ans;
	}
	void operator = (const Matrix b) {
		for(int i = 0; i <= 1; i++)
			for(int j = 0; j <= 1; j++)
				a[i][j] = b.a[i][j];
	}
}num;

Matrix QuickPow2(Matrix a, int b) { //2进制快速幂
	Matrix ans = num;
	ans.a[0][0] = ans.a[1][1] = 1;
	while(b) {
		if(b & 1) {
			ans = ans * a;
		}
		a = a * a;
		b >>= 1;
	}
	return ans;
}
Matrix QuickPow10(Matrix a, string str) { //10进制快速幂
	Matrix ans = num;
	ans.a[0][0] = ans.a[1][1] = 1;
	int len = str.size();
	for(int i = len-1; i >= 0; i--) {
		int num = str[i]-'0';
		ans = ans * QuickPow2(a, num);
		a = QuickPow2(a, 10);
	}
	return ans;
}
/*
a b  a1
1 0  a2
*/
int main() {
	cin>>a1>>a2>>a>>b>>str>>mod;
	Matrix ans, trs;
	trs.a[0][0] = a;
	trs.a[0][1] = b;
	trs.a[1][0] = 1;
	trs.a[1][1] = 0;
	ans.a[0][0] = a2;
	ans.a[1][0] = a1;
	trs = QuickPow10(trs, str);
	ans = trs*ans;
	cout <<ans.a[1][0]<< endl;
	return 0;
}

循环结:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int N = 1e6 + 10;
struct node {
	ll mat[2][2];
};

int prime[N], len;
int ok[N];
ll x0, x1, a, b, mod, p;
node ans, O, tmp, z;
char n[N];

void init() {
	for(int i = 2; i < N; i++) {
		if(!ok[i])
			prime[len++] = i;
		for(int j = 0; j < len && (ll)i * prime[j] < N; j++) {
			ok[i * prime[j]] = 1;
			if(i % prime[j] == 0) break;
		}
	}

}

ll ksc (ll x, ll y, ll z) {
	x %= z;
	y %= z;
	ll res = 0;
	while(y) {
		if(y & 1)
			res = (res + x) % z;
		x = (x + x) % z;
		y >>= 1;
	}
	return res;
}

node cul (node x, node y) {
	for(int i = 0; i < 2; i++)
		for(int j = 0; j < 2; j++) {
			z.mat[i][j] = 0;
			for(int k = 0 ; k < 2; k++) {
				z.mat[i][j] = (z.mat[i][j] + ksc(x.mat[i][k] , y.mat[k][j], mod)) % mod;
			}
		}
	return z;
}

ll getmod(ll x) {
	ll ans1 = 1, ans2 = 1, cnt = x;
	for(int i = 0; i < len && (ll)prime[i] * prime[i] <= x; i++) {
		if(x % prime[i] == 0) {
			ans1 *= (prime[i] - 1) * (prime[i] + 1);
			ans2 *= prime[i];
			while(x % prime[i] == 0) x /= prime[i];
		}
	}
	if(x > 1) {
		ans1 *= (x - 1) * (x + 1);
		ans2 *= x;
	}
//   cout << cnt << " " << ans1 << " " << ans2 << endl;
	return cnt / ans2 * ans1;
}

void solve(ll x) {
	ans.mat[0][0] = ans.mat[1][1] = 1;
	ans.mat[0][1] = ans.mat[1][0] = 0;
	tmp.mat[0][0] = a;
	tmp.mat[0][1] = b;
	tmp.mat[1][0] = 1;
	tmp.mat[1][1] = 0;
	while(x) {
		if(x & 1) ans = cul(ans, tmp);
		tmp = cul(tmp, tmp);
		x >>= 1;
	}

	printf("%lld\n", (ksc(ans.mat[1][0], x1, mod) + ksc(ans.mat[1][1], x0, mod)) % mod);
}

int main() {

	init();
	ll x = 0;
	scanf("%lld %lld %lld %lld %s %lld", &x0, &x1, &a, &b, n, &mod);
	int len = strlen(n);
	p = getmod(mod);
//   cout << mod << endl;
	for(int i = 0; i < len; i++) {
		x = ksc (x , 10, p) ;
		x = (x + n[i] -'0') % p;
	}
	solve(x);
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值