假期总结5(快速幂)

1.快速幂

#include<iostream>
using namespace std;

int qpow(int x, int y)
{
	int res = 1;
	while (y)
	{
		if (y & 1)res *= x;
		x = x * x;
		y >>= 1;
	}
	return res;
}
signed main()
{
	int num,n; cin >> num>>n;
	int ans = qpow(num, n);
	cout << ans << endl;
}

当指数n较大时,使用快速幂可以大大降低循环次数,对qpow()函数的实现,我的理解是,任何一个整数都可以写成二进制形式,如10可写成1010,自然7^10=7^8*7^2,即当指数的二进制表示下的位数为1时,进行乘积,以为是依据二进制完成的操作,因此底数要每次平方来呼应二进制表示的位数

 2.矩阵快速幂

即为矩阵乘法+快速幂

矩阵乘法的实现

note mul(note a, note b)//note为结构体类型
{
	note res;
	memset(res.m, 0, sizeof (res.m));
	for(int i=1;i<=2;i++)
		for(int j=1;j<=2;j++)
			for (int k = 1; k <= 2; k++)
			{
				res.m[i][j] += (a.m[i][k] * b.m[k][j])%N;
			}
	return res;
}

例题

 经典斐波那契

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 10000;

struct note
{
	ll m[3][3];
};

note mul(note a, note b)
{
	note res;
	memset(res.m, 0, sizeof (res.m));
	for(int i=1;i<=2;i++)
		for(int j=1;j<=2;j++)
			for (int k = 1; k <= 2; k++)
			{
				res.m[i][j] += (a.m[i][k] * b.m[k][j])%N;
			}
	return res;
}


note ksm(note a, ll b)
{
	note ans;
	memset(ans.m, 0, sizeof(ans.m));
	for (int i = 1; i <= 2; i++)ans.m[i][i] = 1;
	while (b)
	{
		
		if (b & 1)ans = mul(ans, a);
		a = mul(a, a);
		b >>= 1;
	}
	return ans;
}

signed main()
{
	ll n;
	while (scanf("%lld", &n)!=EOF&&n!=-1)
	{
		note a, b;
		a.m[1][1] = 1; a.m[1][2] = 1;
		a.m[2][1] = 1; a.m[2][2] = 0;
		b = ksm(a, n);
		cout << (b.m[2][1]+N)%N << endl;
	}
}

矩阵的迹

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 9973;
ll n,k;

struct note
{
	ll m[11][11];
};

note mul(note a, note b)
{
	note res;
	memset(res.m, 0, sizeof(res.m));

	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			for (int k = 1; k <= n; k++)
			{
				res.m[i][j] += (a.m[i][k] * b.m[k][j]) % N;
			}
	return res;
}


note ksm(note a, ll b)
{
	note ans;
	memset(ans.m, 0, sizeof(ans.m));
	ans = a;
	b--;
	while (b)
	{

		if (b & 1)ans = mul(ans, a);
		a = mul(a, a);
		b >>= 1;
	}
	return ans;
}

signed main()
{
	ll t; cin >> t;
	while (t--)
	{
		scanf("%lld%lld", &n,&k);
		note a, b;
		for(int i=1;i<=n;i++)
			for (int j = 1; j <= n; j++)
			{
				scanf("%lld", &a.m[i][j]);
			}
		b = ksm(a, k);

		

		ll ans = 0;
		for(int i=1;i<=n;i++)
			{
			//cout << b.m[i][i] << " ";
			ans = (ans + b.m[i][i] + N) % N;
			//cout << ans << " ";
			}
		cout << ans << endl;
	}
	return 0;
}

A Simple Math Problem

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;

struct note
{
	ll m[11][11];
};

note mul(note a, note b,ll N)
{
	note res;
	memset(res.m, 0, sizeof(res));
	for (int i = 1; i <= 10; i++)
		for (int j = 1; j <= 10; j++)
			for (int k = 1; k <= 10; k++)
				res.m[i][j] +=( a.m[i][k] * b.m[k][j])%N;

	return res;
}

note ksm(note a, ll b,ll N)
{
	note ans;
	memset(ans.m, 0, sizeof(ans.m));
	for (int i = 1; i <= 10; i++)ans.m[1][i] = 10 - i;

	while (b)
	{
		if (b & 1)ans = mul(ans, a,N);
		a = mul(a, a, N);
		b >>= 1;
	}
	return ans;
}

signed main()
{
	ll n, k;
	while (cin >> n >> k)
	{
		note a, b;
		memset(a.m, 0, sizeof(a.m));
		for (int i = 1; i <= 10; i++)
		{
			cin >> a.m[i][1];
			if (i !=10 )a.m[i][i +1 ] = 1;
		}
		b = ksm(a, n - 9,k);
		
		cout <<b.m[1][1] % k << endl;
	}
	return 0;
}

ProjectEuler 48

#include<iostream>
using namespace std;
typedef long long ll;
ll ans;
ll modmul(int a, int n)
{
	ll res = 1;
	for (int i = 1; i <= n; i++)
	{
		res = (res * a) % 10000000000;
	}
	return res;
}

signed main()
{
	int t; cin >> t;
	while (t--)
	{
		int n; cin >> n;
		for (int i = 1; i <= n; i++)
		{
			ans = (ans + modmul(i,i)+ 10000000000) % 10000000000;
		}
		cout << ans << endl;
		ans = 0;
	}
	return 0;
}

 

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值