zoukankan      html  css  js  c++  java
  • LightOJ

    链接:

    https://vjudge.net/problem/LightOJ-1005

    题意:

    A rook is a piece used in the game of chess which is played on a board of square grids. A rook can only move vertically or horizontally from its current position and two rooks attack each other if one is on the path of the other. In the following figure, the dark squares represent the reachable locations for rook R1 from its current position. The figure also shows that the rook R1 and R2 are in attacking positions where R1 and R3 are not. R2 and R3 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 rooks on an n x n chessboard so that no two of them are in attacking positions.

    思路:

    只需考虑k行,组合数选k。
    (C_n^k*prod_n^{n-k+1})

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<math.h>
    #include<vector>
    #include<map>
    #include<set>
    
    using namespace std;
    typedef long long LL;
    const int INF = 1e9;
    
    const int MAXN = 5e6+10;
    const int MOD = 1e9+7;
    
    int n, k;
    
    int main()
    {
        int cnt = 0;
        int t;
        scanf("%d", &t);
        while(t--)
        {
            printf("Case %d:", ++cnt);
            scanf("%d %d", &n, &k);
            if (k > n)
                printf(" 0
    ");
            else
            {
                LL sum = 1;
                for (int i = n;i > n-k;i--)
                    sum *= i;
                for (int i = 1;i <= k;i++)
                    sum /= i;
                for (int i = n;i > n-k;i--)
                    sum *= i;
                printf(" %lld
    ", sum);
            }
        }
        
        return 0;
    }
    
  • 相关阅读:
    闭包
    this
    函数声明,表达式,构造函数
    算法学习_栈
    LeetCode刷题_140
    2020/3/20 刷题
    2020/3/19 刷题
    2020/3/13_C++实验课
    刷题(主要是DFS) 2020年3月12日
    DFS的一些题2020/3/11
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11886510.html
Copyright © 2011-2022 走看看