Little Bishops uva861

Little Bishops

A bishop is a piece used in the game of chess which is played on a board of square grids. A bishop can only move diagonally from its current position and two bishops attack each other if one is on the path of the other. In the following figure, the dark squares represent the reachable locations for bishop B1 form its current position.  The figure also shows that the bishops B1 and B2 are in attacking positions whereas B1 andB3 are not. B2 and B3 are also in non-attacking positions.

Now, given two numbers n and k, your job is to determine the number of ways one can put k bishops on an n × n chessboard so that no two of them are in attacking positions.

Input

The input file may contain multiple test cases. Each test case occupies a single line in the input file and contains two integers n (1 ≤ n ≤ 8) and k(0 ≤ k ≤ n2).

A test case containing two zeros for n and k terminates the input and you won’t need to process this particular input.

Output

For each test case i

#include"iostream"
#include"cstring"
#include"algorithm"
using namespace std;
const int N=8;
int b[N+1],w[N+1],rb[N+1][65],rw[N+1][65];
void init_chessboard(int n)
{
    memset(b,0,sizeof(b));
    memset(w,0,sizeof(w));
    for(int i=1;i<=n;i++)
      for(int j=1;j<=n;j++)
    {
        if((i+j)&1)
           w[(i+j)>>1]++;
        else
           b[(i+j)>>1]++;
    }
}
void bishops(int n,int k,int c[N+1],int R[N+1][65])
{
    for(int i=0;i<=n;i++)
      R[i][0]=1;
    for(int j=1;j<=k;j++)
        R[0][j]=0;
    for(int i=1;i<=n;i++)
      for(int j=1;j<=c[i];j++)
        R[i][j]=R[i-1][j]+R[i-1][j-1]*(c[i]-j+1);
}
int main()
{
    int n,k,ans;
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        if(n==0&&k==0)
          break;
        init_chessboard(n);
        sort(b+1,b+n+1);
        sort(w+1,w+n);
        bishops(n,k,b,rb);
        bishops(n-1,k,w,rw);
        ans=0;
        for(int i=0;i<=k;i++)
          ans+=rb[n][i]*rw[n-1][k-i];
        printf("%d\n",ans);
    }
    return 0;
}

n the input print a line containing the total number of ways one can put the given number of bishops on a chessboard of the given size so that no two of them are in attacking positions. You may safely assume that this number will be less than 1015.

 

Sample Input
8 6
4 4
0 0

 

Sample Output
5599888
260

Little Bishops uva861,布布扣,bubuko.com

时间: 2024-12-28 08:13:17

Little Bishops uva861的相关文章

codeforces Gargari and Bishops(很好的暴力)

1 /* 2 题意:给你一个n*n的格子,每一个格子都有一个数值!将两只bishops放在某一个格子上, 3 每一个bishop可以攻击对角线上的格子(主对角线和者斜对角线),然后会获得格子上的 4 数值(只能获取一次).要求输出两个bishops获取的最大值以及它们所在的位置! 5 6 7 思路:直接暴力!....不错的暴力题目! 8 首先我们都知道每一条主对角线上的横纵坐标的和相同,每一条副对角线上的横纵坐标的差相同! 9 那么我们在输入的时候就可以将所有对角线上的数值之和求出来了! 10

UVA 10237 - Bishops(递推)

UVA 10237 - Bishops 题目链接 题意:问一个n * n棋盘能放k个主教(攻击斜线)的方案数. 思路:递推,首先考虑一个问题,在一个n?n棋盘上,放k个车的方案数. 那么设dp[i][j]为i行用了j个车的方案数,由于每行只能放一个车,那么考虑i行放不放车,如果放车,那么能放的位置有n?(j?1)个位置,为dp[i?1][j?1]?(n?(j?1)). 如果不放那么情况为dp[i?1][j]. 所以递推式为dp[i][j]=dp[i][j?1]+dp[i?1][j?1]?(n?(

uva 10237 - Bishops(dp)

克里斯·厄姆森 谷歌今天在 Code 大会上发布了新的无人驾驶汽车.该汽车看起来像是有轮子的缆车,它既没有驾驶盘,也没有刹车踏板和加速装置.Re/code 采访了谷歌无人驾驶汽车项目主管克里斯·厄姆森(Chris Urmson),期间谈及该项目革命背后的概念.产品何时上路等问题. 谷歌在过去的 5 年里改装了现成车型去试验无人驾驶技术.除了车顶的旋转激光装置外,它们看上去跟普通车没什么不同.而该公司今天发布的汽车看上去则非常怪异.它们又小又圆,配备各种小型黑色传感器(车顶也有旋转激光装置),用泡

UVA - 10237 Bishops

A bishop is a piece used in thegame of chess which is played on a board of square grids. A bishop can only movediagonally from its current position and two bishops attack each other if oneis on the path of the other. In the following figure, the dark

Wet Shark and Bishops(思维)

Today, Wet Shark is given n bishops on a 1000 by 1000 grid. Both rows and columns of the grid are numbered from 1 to 1000. Rows are numbered from top to bottom, while columns are numbered from left to right. Wet Shark thinks that two bishops attack e

UVA 861 Little Bishops

https://vjudge.net/problem/UVA-861 题意: 在n*n棋盘上方k个互不攻击的象,求方案数 若两个象在同意对角线上,则会互相攻击 将棋盘黑白染色,则黑格不会攻击白格,白格不会攻击黑格 所以黑白格分开考虑 最终答案= Σ 黑格放i个*白格放k-i个 将所有黑格抽离出来,旋转45° 这样对角线方向就变成了水平方向和竖直方向 问题转化成了 每一行每一列至多放1个 在按每行格子数量从小到大排序 这样每行依次为 2个.2个.4个.4个.6个.6个.8个.8个…… 或者每行一次

CodeForces 621B Wet Shark and Bishops

记录一下每个对角线上有几个,然后就可以算了 #include<cstdio> #include<cstring> #include<cmath> #include<ctime> #include<vector> #include<algorithm> using namespace std; const int maxn=2000+10; int n; long long w1[maxn]; long long w2[maxn]; l

SGU 221.Big Bishops(DP)

题意: 给一个n*n(n<=50)的棋盘,放上k个主教(斜走),求能放置的种类总数. Solution : 同SGU 220,加个高精度就好了. code #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <algorithm> using namespace std; string f[2][250][250], ans;

UVA11261 Bishops

给出一个n*n的棋盘和m个象,每个象能够覆盖它所在的对角线,问没有被覆盖的点有多少个 对于一头大象它可以覆盖它所在的从对角线和主对角线,但是些对角线可能相互交叉,因此不能直接求对角线上面的点的个数.n*n的暴力方法很好想出来,但是肯定超时. 我们可以把主对角线和从对角线保存下来,预处理好没有被覆盖的点,dp[i]表示第i条从对角线上面没有被覆盖的点,一共有2*n-1条从对角线.对于从对角线的上半部分,dp[i]初始化为dp[i-2]因为他们的奇偶性相同,则除去第i条从对角线的两个端点以外,如果第