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=;
int b[N+],w[N+],rb[N+][],rw[N+][];
void init_chessboard(int n)
{
memset(b,,sizeof(b));
memset(w,,sizeof(w));
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
if((i+j)&)
w[(i+j)>>]++;
else
b[(i+j)>>]++;
}
}
void bishops(int n,int k,int c[N+],int R[N+][])
{
for(int i=;i<=n;i++)
R[i][]=;
for(int j=;j<=k;j++)
R[][j]=;
for(int i=;i<=n;i++)
for(int j=;j<=c[i];j++)
R[i][j]=R[i-][j]+R[i-][j-]*(c[i]-j+);
}
int main()
{
int n,k,ans;
while(scanf("%d%d",&n,&k)!=EOF)
{
if(n==&&k==)
break;
init_chessboard(n);
sort(b+,b+n+);
sort(w+,w+n);
bishops(n,k,b,rb);
bishops(n-,k,w,rw);
ans=;
for(int i=;i<=k;i++)
ans+=rb[n][i]*rw[n-][k-i];
printf("%d\n",ans);
}
return ;
}

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的更多相关文章

  1. codeforces Gargari and Bishops(很好的暴力)

    /* 题意:给你一个n*n的格子,每一个格子都有一个数值!将两只bishops放在某一个格子上, 每一个bishop可以攻击对角线上的格子(主对角线和者斜对角线),然后会获得格子上的 数值(只能获取一 ...

  2. CodeForces463C Gargari and Bishops(贪心)

    CodeForces463C Gargari and Bishops(贪心) CodeForces463C 题目大意:  在国际象棋的棋盘上放两个主教,这个两个主教不能攻击到同一个格子,最后的得分是这 ...

  3. B. Wet Shark and Bishops(思维)

    B. Wet Shark and Bishops time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  4. Codeforces 612B. Wet Shark and Bishops 模拟

    B. Wet Shark and Bishops time limit per test: 2 seconds memory limit per test: 256 megabytes input: ...

  5. 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 nu ...

  6. GYM - 101147 F.Bishops Alliance

    题意: 一个n*n的棋盘,有m个主教.每个主教都有自己的权值p.给出一个值C,在棋盘中找到一个最大点集.这个点集中的点在同一条对角线上且对于点集中任意两点(i,j),i和j之间的主教数(包括i,j)不 ...

  7. codeforces 463C. Gargari and Bishops 解题报告

    题目链接:http://codeforces.com/contest/463/problem/C 题目意思:要在一个 n * n 大小的棋盘上放置两个bishop,bishop可以攻击的所有位置是包括 ...

  8. Codeforces--621B--Wet Shark and Bishops(数学)

     B. Wet Shark and Bishops time limit per test 2 seconds memory limit per test 256 megabytes input ...

  9. Bishops Alliance—— 最大上升子序列

    原题链接:http://codeforces.com/gym/101147/problem/F 题意:n*n的棋盘,给m个主教的坐标及其私有距离p,以及常数C,求位于同一对角线上满足条件:dist(i ...

随机推荐

  1. Ubuntu 12.04 安装wine1.7

    从源码安装wine 我的系统环境:ubuntu 12.04.4 X64 wine版本:wine-1.7.12.tar.bz2 解压:tar -xvf wine-1.7.12.tar.bz2 进入win ...

  2. git常用命令[持续更新]

    git commit -am "abc" 提交已经删除过的文件 git reset --hard HEAD 用于清除跟踪文件的修改

  3. ESXi 与其它虚拟化底层产品之比较:

    序号 虚拟化管理程序属性 VMware ESXi 5.0 采用 Hyper-V 的 Windows Server 2008 R2 SP1 Citrix XenServer 5.6 FP1  1 磁盘占 ...

  4. Tomcat线程池,更符合大家想象的可扩展线程池

    因由 说起线程池,大家可能受连接池的印象影响,天然的认为,它应该是一开始有core条线程,忙不过来了就扩展到max条线程,闲的时候又回落到core条线程,如果还有更高的高峰,就放进一个缓冲队列里缓冲一 ...

  5. 窥探JVM内存分配和回收的过程

    一.环境 JDK 垃圾收集器 是否启用TLAB 通用JVM参数(堆内存分配见下图) 1.6.0_65 Serial + Serial Old 否 -Xms20m -Xmx20m -Xmn10m -XX ...

  6. html学习笔记之position

    今天主要一直看试验position的各种属性,现在记录下来以此备忘. position有四种常有属性,分别是static,fixed.absolute,relative fixed就是相对于窗口的位置 ...

  7. [C语言 - 6] static & extern

    A. extern函数 一个c文件生成一个obj文件   外部函数:允许其他文件访问.调用的函数(默认函数为外部函数),不允许存在同名的外部函数   my.c //define a extern fu ...

  8. Objective-c setObject:forKey:和setValue:forKey:的区别

    setObject:forKey: 是NSMutableDictionary类的方法                               key参数类型可以是任意类型对象           ...

  9. UOJ Test Round #2

    昨天晚上打的这个比赛,简直一颗赛艇啊-- 感觉发挥的并不好.比赛的时候比较紧张,最后一题还脑残写了个离散化结果爆零了,哎我怎么这么逗逼-- 讲讲比赛经过吧. 比赛之前逗逼地以为是8:00开始,然后淡定 ...

  10. SQLite多线程写锁文件解决方案

    在sqlite编程中多线程同时写时会出现异常,我写了个类来解决这个问题. 思路很简单,就是在开始写操作时,记下写操作的托管线程id,表示目前有线程正在做写操作:其他线程来写时,需要先检测是否有进程正在 ...