2021-02-03 0x01 基本算法-位运算

一.a^b%c

1.题意:
2.题解:
3.ac代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll quick_pow(ll a,ll b,ll c) {
    ll ans = 1;
    while(b) {
        if(b&1)
            ans = (ans * a) % c;
        a = (a*a) % c;
        b >>= 1;
    }
    return ans%c;
}
int main(){
	ll a,b,p;
	ll ans;
	while(cin>>a>>b>>p){
		ans=quick_pow(a,b,p);
		cout<<ans<<endl;
	}
	
} 

二.快速幂取模

1.题意:
2.题解:

(A1B1+A2B2+ … +AHBH)mod M.

3.ac代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll quick_pow(ll a,ll b,ll c) {
    ll ans = 1;
    while(b) {
        if(b&1)
            ans = (ans * a) % c;
        a = (a*a) % c;
        b >>= 1;
    }
    return ans%c;
}
int main(){
	int t;
	ll m,a,b;

	ll ans;
	while(cin>>t){
		while(t--){
			ans=0;
			cin>>m;
			int h;
			cin>>h;
			while(h--){
				cin>>a>>b;
				ans=(ans+quick_pow(a,b,m))%m;	
			}	
			cout<<ans<<endl;
		}
	}
	
} 

三.64位整数乘法

1.题意:
2.题解:
3.ac代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll quick_mul(ll a, ll b, ll c) {
    ll ans = ((long double) a * b + 0.5) / c;
    return ((a * b - ans * c) % c + c) % c;
}
int main(){
	ll a,b,p;
	ll ans;
	while(cin>>a>>b>>p){
		ans=quick_mul(a,b,p);
		cout<<ans<<endl;
	}

} 

四.状态压缩dp

1.题意:
2.题解:
3.ac代码:
在这里插入代码片
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值