算法进阶指南-基本算法-位运算

1.a^b
题目直通车
在这里插入图片描述
题解:就是快速幂,一模一样(快速幂讲解
java代码:

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    
    

        static  long mod;

    public static void main(String[] args) {
    
    
        Scanner sc=new Scanner(System.in);
       long a,b;
       a=sc.nextLong();
       b=sc.nextLong();
       mod=sc.nextLong();
        System.out.println(kuaishumi(a,b)%mod);
    }

    static long kuaishumi(long a,long b)
    {
    
    
        long x=a;
        long ans=1%mod;
        while(b>0)
        {
    
    
            if((b&1)==1)
            {
    
    
                ans=ans*x%mod;
                

            }
            x=x*x%mod;
           
            b>>=1;

        }
        return ans;

    }



}

c++代码:

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long LL;

int power(int a, int b, int p)
{
    
    
    int ans = 1 % p;
    while(b){
    
    
        if(b & 1) ans = (long long) ans * a % p;
        a = (long long)a * a % p;
        b >>= 1;
    }
    return ans;
}

int main()
{
    
    
    int a, b, p;
    scanf("%d%d%d", &a, &b, &p);
    printf("%d\n", power(a, b, p));
    return 0;
}

2.raising module numble
题目链接
在这里插入图片描述
题目大意:给一个数M,和N对数a,b;求N对a^b的和取余M

题解:其实就是多组快速幂

java代码:

import java.util.*;
//import java.math.*;
public class Main {
    
    
    public static void main(String []args) {
    
    
        Scanner sc = new Scanner(System.in);
        int z = sc.nextInt();
        while(z-- != 0) {
    
    
            int m = sc.nextInt();//余数m
            int h = sc.nextInt();//h对
            int ans = 0;
            while(h-- != 0) {
    
    
                ans += quickPow(sc.nextInt(), sc.nextInt(), m);//分别求快速幂
                ans %= m;
            }
            System.out.println(ans);
        }
    }
    public static int quickPow(int a, int b, int p) {
    
    
        int res = 1 % p;
        while(b != 0) {
    
    
            if((b & 1) == 1) {
    
    
                res = (res % p * a % p) % p;
            }
            a = ((a % p) * (a % p)) % p;
            b >>= 1;
        }
        return res % p;
    }
}

c++代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#define LL long long
using namespace std;
LL pow_mod(LL a, LL p, LL M)
{
    
    
    if(p == 0) return 1;
    LL ans = pow_mod(a, p/2, M);
    ans = ans * ans % M;
    if(p % 2 == 1)
        ans = ans * a % M;
    return ans;
}
int main()
{
    
    
    int t, H;
    LL M;
    scanf("%d", &t);
    while(t--)
    {
    
    
        scanf("%lld", &M);
        scanf("%d", &H);
        LL a, b;
        LL ans = 0;
        while(H--)
        {
    
    
            scanf("%lld%lld", &a, &b);
            ans = (ans + pow_mod(a, b, M)) % M;
        }
        printf("%lld\n", ans);
    }
    return 0;
}

3.64位整数乘法
题目链接
在这里插入图片描述
题解:如果直接乘,就是用long也一定会超范围,而且它题目不可能出的这么简单吧。既然是位运算,我们把b转化成二进制,ab 也就相当于有b个a相加,b转化成二进制后,它的第一位代表a的个数,第二位代表2a的各位…
那么我们也就成功的把它转换成一个位运算的问题,具体做法和快速幂有异曲同工之妙,具体看代码。

java:

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    
    

        static  int mod;

    public static void main(String[] args) {
    
    
        Scanner sc=new Scanner(System.in);
        long a,b,p;
        a=sc.nextLong();
        b=sc.nextLong();
        p=sc.nextLong();
        System.out.println(fx(a,b,p));

    }
    static  long fx(long a,long b,long c)
    {
    
    
        long ans=0;
        while(b>0)//类似快速幂
        {
    
    
            if((b&1)>0)
            {
    
    
                ans=(ans+a)%c;
            }
            a=a*2%c;
            b>>=1;//b的移动导致a的意义不同
        }

    return  ans;

    }


}

c++:

#include<iostream>
using namespace std;
#define ll long long

ll mul(ll a,ll b,ll p){
    
    
    ll ans=0%p;
    while(b){
    
    
        if(b&1==1){
    
    
            ans=(ans+a)%p;
        }
        a=a*2%p;
        b>>=1;
    }
    return ans;
}
int main(){
    
    
    ll a,b,c;
    cin>>a>>b>>c;
    cout<<mul(a,b,c)<<endl;
    return 0;
}

4.最短Hamilton路径
题目链接
在这里插入图片描述
题解:其实这题放在这不是很好,没错,这题是用到了位运算,但不是单纯的位运算,它是dp和位运算的结合,对于没有接触过dp的同学,这题就很不友好。这里我就大致提一下位运算在这里面是干什么的 对于dp的知识不过涉及

题目中说20个点,每个点只能去一次,我们知道在二进制里只有1和0二个数,那么我们就可以用一个十进制数表示那些点是去过的,那些点没去过,对于11111这个二进制(假设1表示去过),那么这个数则表示到过12345这些点,11011表示除了3号点其他全到过,又因为每一个二进制数唯一对应了一个十进制数,那么我们就可以用一个十进制数表示去过那些点,当判断某个点是否去过时,只需要判断这个数的二进制的对应位是否为1即可

代码放在这,当接触过dp后这题就迎刃而解了
java:

import java.util.Scanner;

public class Main {
    
    
	
	static int MAXN = 20, MAXM = 1 << 20;
	static int[][] dp = new int[MAXM][MAXN];
	static int[][] weight = new int[MAXN][MAXN];
	
	public static void main(String[] args) {
    
    
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		for(int i = 0; i < n; i ++) {
    
    
			for(int j = 0; j < n; j ++) {
    
    
				weight[i][j] = in.nextInt();
			}
		}
		for(int i = 0; i < 1 << 20; i ++) {
    
    
			for(int j = 0; j < 20; j ++) {
    
    
				dp[i][j] = 0x3f3f3f3f;
			}
		}
		dp[1][0] = 0;
		for(int i = 0; i < 1 << n; i ++) {
    
    
			for(int j = 0; j < n; j ++) {
    
    
				if((i >> j & 1) == 1) {
    
    
					for(int k = 0; k < n; k ++) {
    
    
						if((i - (1 << j) >> k & 1) == 1) {
    
    
							dp[i][j] = Math.min(dp[i][j], dp[i - (1 << j)][k] + weight[k][j]);
						}
					}
				}
			}
		}
		System.out.println(dp[(1 << n) - 1][n - 1]);
	}

}

c++:

#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;

int Map[25][25];
int dp[(1<<20)+5][25];

int main()
{
    
    
    int n;
    scanf("%d",&n);
    for(int i=0;i<=n-1;i++){
    
    
        for(int j=0;j<=n-1;j++){
    
    
            scanf("%d",&Map[i][j]);
        }
    }
    memset(dp,0x3f,sizeof(dp));
    dp[1][0]=0;
    for(int i=1;i<=(1<<n)-1;i++){
    
    
        for(int j=0;j<=n-1;j++){
    
    
            if((i>>j)&1){
    
    //如果i的第j位是1,也就是如果经过点j
                for(int k=0;k<=n-1;k++){
    
    
                    if((i>>k)&1){
    
    //如果i的第k位是1,也就是如果经过点k
                        dp[i][j]=min(dp[i][j],dp[i^(1<<j)][k]+Map[k][j]);
                    }
                }
            }
        }
    }
    printf("%d\n",dp[(1<<n)-1][n-1]);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/jahup/article/details/107892581
今日推荐