据说微软校园招聘题:
Problem Statement for RooksParty
Problem Statement
The black and white chess rooks were bored, so they decided to invite their colorful friends to a party. However, as the evening progressed, many pairs of rooks of different colors started arguing and threatening each other.
To prevent a massacre, you now need to place all the rooks in such a way that no two rooks of different colors attack each other.
You are given the dimensions of the chessboard: ints rows and columns. You are also given a int[] counts, where counts[i] is the number of rooks of the i-th color you have.
Compute and return the value (X mod 1,000,000,009), where X is the number of valid arrangements of all the given rooks on the given chessboard. No square of the chessboard may contain more than one rook. Rooks of the same color are undistinguishable.
Definition
Class: RooksParty
Method: countArrangements
Parameters: int, int, int[]
Returns: int
Method signature: int countArrangements(int rows, int columns, int[] counts)
(be sure your method is public)
Notes
- Two rooks attack each other if they are either in the same row or in the same column, and all squares between them are empty.
Constraints
- rows will be between 1 and 30, inclusive.
- columns will be between 1 and 30, inclusive.
- counts will contain between 1 and 10 elements, inclusive.
- Each element of counts will be positive.
- The sum of all elements of counts will not exceed rows*columns.
Examples
0)
2
3
{1,1}
Returns: 12
Here are all 12 valid placements:
1.. 1.. .1. .1. ..1 ..1
.2. ..2 2.. ..2 2.. .2.
.2. ..2 2.. ..2 2.. .2.
1.. 1.. .1. .1. ..1 ..1
1)
5
2
{3}
Returns: 120
As all three rooks have the same color, we can put them on any three squares.
2)
5
2
{1,1,1}
Returns: 0
It is impossible to place these rooks correctly.
3)
8
8
{1,1,1,1,1,1,1,1}
Returns: 625702391
Here the answer is (8! * 8!) modulo 1,000,000,009.
4)
4
2
{3,1}
Returns: 8
--------------------------------------------------------------------------------
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2010, TopCoder, Inc. All rights reserved.
一个棋盘上,最多有10种颜色的棋子,每种颜色棋子数不定,行列中不能有不同颜色棋子,求出可实行的方案数。
采用遍历算法是不可能的,计算时间不可接受,我这里只讨论简单的情况,也就是每种颜色只有一个棋子,其他情况可能需要组合数学知识。
以下是我的代码,请多多指教。
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;
int countArrangements(int rows, int columns, int counts[])
{
int nResult = 0;
int nColor = 0;
int nfilter = 0;
int nNext = 0;
int ntemp = 0;
for (int i = 0; i < 10; ++i)
{
if (counts[i] > 0)
{
nColor++;
}
}
if (nColor > rows || nColor > columns)
{
return nResult;
}
nResult = rows*columns;
for(int i = 1;i < nColor; ++i)
{
nNext = rows * columns - i*(rows + columns) + i*i;
nfilter = 1000000009/nNext;
if (nResult <= nfilter)
{
nResult = nResult * nNext;
}else
{
ntemp = nResult;
for (int j = 1; j < nNext; j++)
{
nResult += ntemp;
if (nResult > 1000000009)
{
nResult -= 1000000009;
}
}
}
}
return nResult;
}
int main()
{
clock_t start,finish;
start = clock();
int a[] ={1,1,1,1,1,1,1,1,1,1};
int nresult = 0;
nresult = countArrangements(30,30,a);
cout << nresult << endl;
finish = clock();
cout << (double)(finish - start)/CLOCKS_PER_SEC << endl;
return 0;
}