01 bit operation (fast exponentiation)? ?

Bit operation

20190702~

1. Basic concepts

  1. List item1. The data in the computer (positive and negative) are stored in binary
  2. Four operations:
与 		x & y
或 		x | y
非		| x
异或		x ^ y
  1. The int type is 32 bits, where the first bit is the sign bit.
    Negative numbers are represented by one's complement-n = n reverse +1
  2. Shift operator >>: Note that all shifts in the computer are arithmetic shifts, so when shifting to the right, you need to complement the sign bit
  3. When initializing an array, it is usually initialized to positive infinity
memset(f, 0x3f, sizeof f);	//将f中的每一个字节都初始化为输入的数 所以最后为0x3f3f3f3f

In dynamic programming or graph theory, two numbers are often used for addition
, ie: 0x3f3f3f3f * 2 <0xffffffff, no overflow will occur

Two, XOR operation

(1) Realizing spouse through exclusive OR

  1. If the array is stored in pairs, you can quickly get to another number in the array by XOR
  2. The minimum cost flow of graph theory will use this idea
  3. In the array simulation adjoining table, the forward and reverse edges of the edges will be retained. After the forward and reverse edges are stored together, fast evaluation can be achieved during calculation.

(2 and 3 have not been exposed yet, and still can’t fully understand the meaning. I will come back to add after learning the relevant content later)

(Two) lowbit operation

  1. Lowbit function: find the lowest 1 of a binary number
// E.G.  lowbit(1001011000) = 1000

  n   =  1001011000
 ~n   =  0110100111
 ~n+1 =  0110101000
 -n   =  ~n+1

//代码实现为:
int lowbit(int n)
{
	return (~n+1) & n;
或	return (-n) & n;
}

Three, example x3

(1) Find the value of a modulo p to the b power

Knowledge point: fast power
problem .
It also introduces matrix fast power (no requirement now)

Topic:
Input format: three integers a, b, p separated by spaces on the same line
Output format: output an integer, representing the value of a ^ b mod p
Data range: 1 <= a, b, p <= 10 9

Input sample: 3 2 7
Output sample: 2

  1. Simplify the calculation times of a b by the binary of b

3 7 is equivalent to multiplying 7 3s, so if you do it directly, you need to multiply 6 times. At this time, the disadvantage is not obvious, but when the power b is very large, the calculation amount is too large, which will cause the calculation time to extend. However, considering the binary idea, the number of times can be simplified by the multiple relationship between adjacent powers.
7 = 111

3 1 = 3
3 2 = 9
3 4 = 81
This turns 6 operations into a multiplication of three numbers

Similarly, 2 13 is to find the corresponding power through the binary number of 13 and divide 13 into 1+4+8

21101 1 1 0 1
b each bit of the corresponding a n 1*38 1*34 0*32 1*31
  1. ( ab ) mod p =(m n )mod p
    若m = ( a mod p) n=(b mod p)
#include <iostream>;
using namespace std;

int main(){
   int a,b,p;          	   //C++中输入变量的值--直接使用cin输入即可
   cin >> a >> b >> p ;
   int res = 1 % p;        //先假设一个结果:新变量res(求a^b的本质是乘法 所以设res=1 ;若求a*b 其本质为加法 设res=0)
                           //%p:如果b为0 直接没有进入循环
                           //或int res = 1; 在最后输出的时候 cout << res % p
   while(b){           	   //首先考虑变量b的个位
       if(b&1) res = res * 1ll * a % p;          //b&1:判断b的个位是否为1 
                                                 /*因为a b p 的数字比较大 可能会发生溢出 
                                                   所以要改变数据类型至long long 1ll是long long类型的整数1 
                                                   通过*1ll实现数据类型的强制转换*/
                                                 //现在已经处理完个位了
       a = a * 1ll * a % p;    //在第二次循环的时候 操作b的十位 将b的十位不断平方
       b >>= 1;       //b右移1位 去掉个位
   }
   cout << res << endl;
   return 0;
}

(2) Find the value of a multiplied by b modulo p

Input format: the first line of integer a, the second line of integer b, and the third line of integer p.
Output format: output an integer, representing the value of a*b mod p.
Data range: 1≤a,b,p≤1018
input Sample:
3
4
5
Output sample:
2

The principle is the same as example one

#include <iostream>
using namespace std;
typedef unsigned long long ULL;        //如何判断什么时候使用无符号数?
                                       //注意使用替换的关键字是typedef 并且最后需要加分号

int main(){
    ULL a, b, p;
    cin >> a >> b >> p;
    ULL res = 0;   
    while(b){
        if(b & 1) res = (res + a) % p;
        b >>= 1;                        //注意 >>= 中间是不能加空格的
        a = a * 2 % p;
    }
    
    cout << res << endl;
    return 0;
}

(3) The shortest Hanilton path

Given a weighted undirected graph with n points, the points are labeled from 0 to n-1, find the shortest Hamilton path from the starting point 0 to the end point n-1. The Hamilton path is defined as passing through each point exactly once from 0 to n-1.

Input format: input integer n in the first line and n integers in
each line in the next n lines, where the j-th integer in the i-th line represents the distance from point i to j (denoted as a[i,j]).
For any x, y, z, the data guarantee a[x,x]=0, a[x,y]=a[y,x] and a[x,y]+a[y,z]>=a [x,z]

Output format: output an integer, representing the length of the shortest Hamilton path

Data range
1≤n≤20 0≤a
[i,j]≤107
Input example:
5
0 2 4 5 1
2 0 6 5 3
4 6 0 8 3
5 5 8 0 5
1 3 3 5 0
Output example :
18


Guess you like

Origin blog.csdn.net/weixin_45349512/article/details/94563762