Advanced algorithm contest Guide 0.1

table of Contents

Bitwise 2

1. Fast power 2

2. 64 bit integer multiplication 3

3. shortest Hamilton path 3

1) which is used care point 3

2) currently parked at which point 3

The first dimension state number 2 ^ 203

The second dimension state number 203

2^20 * 20 = 2 * 10^7 3

Second, Tips: 4

1. virtual space

2.cin optimization

3. pair change

4. lowbit operation 5

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Bit computing

  1. Fast power

int poww (int A, int b)
{
    int ANS =. 1, Base = A;
    the while (! b = 0)
    {
        IF (! b &. 1 = 0) // last bit b is not 0
        {
            ANS * = Base;
        }
        Base Base * =;
        B = >>. 1;
    }
    return ANS;
}

 

long long ksm(long long a,long long b) //快速幂a的b次{
    long long ans=1;
    while(b>0)
    {
        if(b%2==1)ans*=a,ans%=mod;
        a*=a;
        a%=mod;
        b/=2;
    }
    return ans;
}

 

  1. 64- bit integer multiplication

 

#include <bits/stdc++.h>

using namespace std;

typedef unsigned long long ull;

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

 

  1. Shortest Hamilton Path

1) interest which points are used

2) currently stopped at what point

The first dimension state number 2 ^ 20

The second dimension number of states 20

2^20 * 20 = 2 * 10^7

f [State] [j]   State indicate which points are used   j represents the point at which the now

 

F [State] [J] = F [state_k] [K] + weight [K] [J];  
// State which points are represented  traversed collection

 

· State how to represent ?    State of compression!

0 denotes did not come ( not within the set ) 1 represented in the set

Example : through 014 point state = 10011

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn = 1<<20;
int f[maxn][25],val[25][25];
int n;
int main(){
    ios::sync_with_stdio(false); //是否兼容stdio的开关
    cin.tie(0); //tie是将scanf和cin两个stream绑定的的函数
    cin>>n;
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++) cin>>val[i][j];

    memset(f,0x3f,sizeof f);
    f[1][0] = 0;
    for(int i=0;i<1<<n;i++){
        for(int j=0;j<n;j++){
            if(i >> j & 1)
                for(int k=0;k<n;k++){
                    if((i^(1<<j))>>k & 1)
                        f[i][j] = min(f[i][j],f[i^(1<<j)][k]+val[k][j]);
                }
        }
    }
    cout<<f[(1<<n)-1][n-1]<<endl;
    return 0;
}

 

 

Second, Tips:

c ++ default stack space 4M = 2 ^ 22 is approximately equal to 4e6

Stack space  system automatically assigns  

Heap assign users to release  static variables static global variables will be placed on the heap

So accustomed to put global variable variable

  1. cin optimization

ios :: sync_with_stdio (false); // switch is compatible stdio
cin.tie (0); // tie is cin function scanf and two stream of bindings

  1. Conversion paired  with XOR implemented spouse

0 1

2 3

4 5

...

n n+1

Each spouse

 

n ^ 1 = n + 1 ( n spouse )

(n + 1) ^ (n + 1 1 = n spouse )

 

n is an even number nXOR1 = n + 1 n is odd nXOR1 = n-1

Application: FIG stored on the adjacent table edge set  minimum cost flow

 

  1. lowbit operation

Find an integer n in the binary representation of a minimum 1 which

Lowbit(1110011000) = 1000

    110 -> 10

int lowbit(int n){
    return n&(-n);
}

 

Note: These are the b station acwing see the video of the code notes can too  

Since then layout are copied from a word document over so he did not look good only as a record

Guess you like

Origin www.cnblogs.com/hao-tian/p/11100020.html