zoukankan      html  css  js  c++  java
  • bzoj1087【SCOI2005】互不侵犯King

    1087: [SCOI2005]互不侵犯King

    Time Limit: 10 Sec  Memory Limit: 162 MB
    Submit: 2405  Solved: 1413
    [Submit][Status][Discuss]

    Description

    在N×N的棋盘里面放K个国王,使他们互不攻击。共同拥有多少种摆放方案。

    国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子。

    Input

    仅仅有一行,包括两个数N。K ( 1 <=N <=9, 0 <= K <= N * N)

    Output

    方案数。

    Sample Input

    3 2

    Sample Output

    16

    HINT

    Source




    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    #define F(i,j,n) for(int i=j;i<=n;i++)
    #define D(i,j,n) for(int i=j;i>=n;i--)
    #define ll long long
    using namespace std;
    int n,m,all,cnt[520];
    ll ans,f[10][100][520];
    bool p[520],g[520][520];
    inline int read()
    {
    	int x=0,f=1;char ch=getchar();
    	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
    	while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    	return x*f;
    }
    inline bool check(int x,int y)
    {
    	return ((x&y)==0)&&((x&(y>>1))==0)&&((y&(x>>1))==0);
    }
    inline void pre()
    {
    	int s=0;
    	F(i,0,all) if ((i&(i>>1))==0)
    	{
    		s=0;
    		for(int x=i;x;x>>=1) s+=(x&1);
    		cnt[i]=s;
    		p[i]=true;
    	}
    	F(i,0,all) if (p[i]) F(j,0,all) if (p[j]) g[i][j]=check(i,j);
    }
    int main()
    {
    	n=read();m=read();
    	all=(1<<n)-1;
    	pre();
    	F(i,0,all) if (p[i]) f[1][cnt[i]][i]=1;
    	F(i,1,n-1) F(j,0,all) if (p[j]) F(k,0,all) if (p[k]&&g[j][k])
    		F(l,cnt[j],m-cnt[k]) f[i+1][l+cnt[k]][k]+=f[i][l][j];
    	F(i,0,all) ans+=f[n][m][i];
    	printf("%lld
    ",ans);
    }
    


  • 相关阅读:
    uva 147 Dollars
    hdu 2069 Coin Change(完全背包)
    hdu 1708 Fibonacci String
    hdu 1568 Fibonacci
    hdu 1316 How Many Fibs?
    poj 1958 Strange Towers of Hanoi
    poj 3601Tower of Hanoi
    poj 3572 Hanoi Tower
    poj 1920 Towers of Hanoi
    筛选法——素数打表
  • 原文地址:https://www.cnblogs.com/yfceshi/p/7103294.html
Copyright © 2011-2022 走看看