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;
    }
    
  • 相关阅读:
    内置函数详解
    关于内置函数
    ac自动机练习 HihoCoder 1036
    字典树Trie练习 HihoCoder 1014
    HDU 6170 Two String 动态规划
    NOJ 1190 约瑟夫问题 线段树OR树状数组
    NOJ 1186 灭蚊药水
    LightOJ 1085 树状数组+动态规划
    LightOJ 1066
    LightOJ 1080 树状数组
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11886510.html
Copyright © 2011-2022 走看看