【王道机试笔记18】二次求幂

二次求幂

例4.10 人见人爱A^B

代码4.10
#include<iostream>
using namespace std;
int main(){
	int A,B,ans;
	while(cin >> A >> B){
		if(A == 0 && B == 0)
			break;
		ans = 1;
		A %= 1000;
		while(B != 0){
			if(B % 2 == 1){
				ans *= A;
				ans %= 1000;
			}
			B /= 2;
			A *= A;
			A %= 1000;
		}
		cout << ans << endl;
	}
	return 0;
} 

A sequence of numbers

#include<iostream>
using namespace std;
int main(){
	int N,K;
	long long x1,x2,x3,m,w;
	cin >> N;
	while(N--){
		cin >> x1 >> x2 >> x3 >> K;
		if(x2 - x1 == x3 - x2){
			m = x2 - x1;
			w = ((K-1)%200907) * (m%200907) + (x1%200907);
		}
		else{
			m = (x2 / x1) % 200907;
			K--;
			w = x1 % 200907;
			while(K!=0){
				if(K % 2 == 1){
					w =(w*m)%200907;
				}
				K /= 2;
				m = (m*m)%200907;
			}
		}
		cout << w % 200907 << endl;
	}
	return 0;
}

Tr A

#include<iostream>
using namespace std;
int main(){
	int T;
	cin >> T;
	while(T--){
		int n,k,i,j,x,sum,t;
		cin >> n >> k;
		int a[15][15];
		int b[15][15] = {0};
		int c[15][15] = {0};
		for(i=0;i<n;i++){
			b[i][i] = 1;
			c[i][i] = 1;
		}
		for(i=0;i<n;i++){
			for(j=0;j<n;j++){
				cin >> x;
				a[i][j] = x;
			}
		}
		while(k != 0){
			if(k % 2 == 1){
				for(i=0;i<n;i++){
					for(j=0;j<n;j++){
						sum = 0;
						for(t=0;t<n;t++){
							sum += b[i][t] * a[t][j];
						}
						c[i][j] = sum % 9973;
					}
				}
			}
			k /= 2;
			for(i=0;i<n;i++){
				for(j=0;j<n;j++){
					sum = 0;
					for(t=0;t<n;t++){
						sum += a[i][t] * a[t][j];
					}
					b[i][j] = sum % 9973;
				}
			}
			for(i=0;i<n;i++){
				for(j=0;j<n;j++){
					a[i][j] = b[i][j];
				}
			}
			for(i=0;i<n;i++){
				for(j=0;j<n;j++){
					b[i][j] = c[i][j];
				}
			}
		}
		int summ = 0;
		for(i=0;i<n;i++){
			summ += c[i][i];
		}
		cout << summ % 9973 << endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值