Compression and binary state

Zuifan own binary, but still work hard, learn to be patient ...

Common numbers are decimal, binary is every 2 into a, I think it can represent the largest use for a number of things not on, compared with 1, not 0, for example, four objects, you can use 1111 indicates, then 1111 represents a decimal 15, 15 so that this state can be expressed.

Speaking from the most basic to say: The general object of n, with n binary representation need to have a position, when the decimal is how much? << can be expressed, for example 2 << 1, it means 100, n number of objects that you need 1 << n, n can represent a 0 in front for more than a 1 to 1, because the situation you want to n-1 also includes, for example: n = 4,1 << 4 says 10,000, from 0 count array after opening, may comprise 1111 in this state, in fact, difficult to find, 1 << n th power of 2 and n are the same the so when data is typically range 2 ^ n times the pressure can be considered like.

Second: To understand some of the basic usage bit computing: & represent, take 0 0 | representation or, 1 take 1, ^ represents a different or the same set to 0, or 1. take this quite for me hard to remember, I remember this: &, |, ^, three sequential remember well, then that is 0, 1, if there is an equal ... << = To the left is the number of a few, followed by 0 padded, for example: 2,4 << 4 represented in binary as 100, the operation becomes 10000; the contrary is the original number of wanted >> = right, discarding the last several, for example: 4 >> 2 will change from 100 It became 1;

Said a number of other uses, like pressure DP is basically needed: how to determine a bit of a binary number is 1 or 0 it? You can use &, | left and 1, for example: if (i & 1 << (j-1)) continue; if binary number indicates that if i j-bit is 1, then, it skipped; in fact well understood, 1 << (j-1) denotes a j-th bit to 1, the remaining bits of the binary number 0. I do with it a matter & calculation, taking 0 0, the j-th bit in addition, some remaining bits set to 0, then the j-th bit is 0 if the whole is 0, is not performed. On the contrary, if the j-th bit is 1 overall there is value, conduct continue. If you want to determine is 0 is skipped, just in front with! . After you can say that is for example how to modify the 1011, 1111 how to change, change the 0 to another state then we will spend |, that is, take a 1-1, for example:?. I | 1 << (j -1) (this time is determined through the j-th bit i is 0) indicates a j-th bit i is 0 becomes 1. Since this is well understood 1 << (j-1) except the j-th bit is 1 , the remaining bits are 0, in this case | other bits corresponding to the i copy down, and modify only the j-th bit is 1;

Well, said so common questions:

 

I first saw this question can not think of DP, because it is the type of arrangement problem. I do not know how to use state said that after this time like they would understand the pressure DP of examples ...

Since it is arranged, then we can mark for not using used to push not used ...

Code:

#include<bits/stdc++.h>
#define ll long long
#define _ 0
using namespace std;
const int maxn=20;
ll f[1<<maxn][maxn],n,H,h[maxn],ans;
int main()
{
    freopen("1.in","r",stdin);
    cin>>n>>H;
    for(int i=1;i<=n;i++) cin>>h[i];
    for(int i=1;i<=n;i++) f[1<<(i-1)][i]=1; //对第i位只放i的初始化;
    for(int i=1;i<=(1<<n)-1;i++)
        for(int j=1;j<=n;j++) //枚举上一个 
            for(int k=1;k<=n;k++) //枚举当前放的
            {
                if(i&1<<(k-1)) continue; //假如说当前k已经放过则跳过.
                if(abs(h[k]-h[j])>H) f[i|1<<(k-1)][k]+=f[i][j];
            }
    for(int i=1;i<=n;i++) ans+=f[(1<<n)-1][i];
    cout<<ans<<endl;
    return (0^_^0);         
}

好了,就到这吧!

Guess you like

Origin www.cnblogs.com/gcfer/p/11076578.html