upc6568 Reconciled?(石油大学,暑假集训题)

6568: Reconciled?

时间限制: 1 Sec  内存限制: 128 MB
提交: 140  解决: 64
[提交] [状态] [讨论版] [命题人:admin]

题目描述

Snuke has N dogs and M monkeys. He wants them to line up in a row.
As a Japanese saying goes, these dogs and monkeys are on bad terms. ("ken'en no naka", literally "the relationship of dogs and monkeys", means a relationship of mutual hatred.) Snuke is trying to reconsile them, by arranging the animals so that there are neither two adjacent dogs nor two adjacent monkeys.
How many such arrangements there are? Find the count modulo 109+7 (since animals cannot understand numbers larger than that). Here, dogs and monkeys are both distinguishable. Also, two arrangements that result from reversing each other are distinguished.

Constraints
1≤N,M≤105

 

输入

Input is given from Standard Input in the following format:
N M

 

输出

Print the number of possible arrangements, modulo 109+7.

 

样例输入

2 2

 

样例输出

8

 

提示

We will denote the dogs by A and B, and the monkeys by C and D. There are eight possible arrangements: ACBD, ADBC, BCAD, BDAC, CADB, CBDA, DACB and DBCA.

解析:本题首先要判断n和m的关系,

1.如果n >= m+2 || m >= n+2那么,不论怎么安排,总会出现两只dog或者两只monkey相邻的情况,因此直接输出安排数目为0;

2.如果n = m的话, 那么先对所有的dog进行全排列,再对所有的monkey进行全排列,这样不是两堆吗,因为个数一样,你就需要在相邻的两个dog之间插入一个monkey(#1、#2表示dog, &1、&2表示monkey),这样因为我们两堆的全排列我都考虑完了,现在只要考虑怎么插入,使得题目满足就可以了。我们取出dog全排列的一种情况比如:#1#2

那么现在这两个dog之间有三个空缺:_#1_#2_对吧,我们把monkey插入进去让其满足题目要求就可以了,我们就要两大中方案:方案1:X#1X#2;方案2:#1X#2X(这里的X是monkey)说道这里就公式就明了吧。

3.如果n = m+1 || m = n+1, 那么上面的两种方案就变成了一种,只能将少的插入到多的内部空格里。

#include <iostream>
#define mod 1000000007
using namespace std;

long long fun(int n){
    long long res = 1;
    for(int i = 1; i <= n; i++){
        res = (res * i) % mod;
    }
    return res;
}

int main()
{
    int n, m;
    long long ans;
    while(cin>>n>>m){
        if(n >= m+2 || m >= n+2){
            cout << "0" << endl;
            continue;
        }
        else if(n == m){
            ans = (((fun(n) * fun(m)) % mod) * 2) % mod;
            cout << ans << endl;
        }
        else{
            ans = (fun(n) * fun(m)) % mod;
            cout << ans << endl;
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值