2016 百度之星 初赛B - 瞬间移动(逆元)

 规律是杨辉三角,也就是求排列组合。因为要取模,所以需要用到逆元。

#include "algorithm"
#include "iostream"
#include "cstring"
#include "cstdio"
#include "string"
#include "stack"
#include "cmath"
#include "queue"
#include "set"
#include "map"

#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1

typedef long long ll;
using namespace std;

const int inf=0x3f3f3f3f;
const int maxn=1e5+5;
const int mod = 1000000007;

int n,m;

//求ax = 1( mod m) 的x值,就是逆元(0<a<m)
ll inv(long long a,long long m)
{
    if(a == 1)return 1;
    return inv(m%a,m)*(m-m/a)%m;
}

//a<=b
ll C(int a,int b)
{
    ll t1=1,t2=1;
    for(int i = b ; i>=(b-a+1) ;i--)t1 = t1*i%mod;
    for(int i = a ; i>=1       ;i--)t2 = t2*i%mod;
    return t1*inv(t2,mod)%mod;
}

int main()
{
    //freopen("in.txt","r",stdin);
    while(scanf("%d%d",&n,&m)!=EOF)
    {

        if(n>m)swap(n,m);
        printf("%I64d\n", C(m-2,n+m-4) );
    }
    return 0;
}

  

PS:为何需要逆元

以下内容来自:http://www.cnblogs.com/linyujun/p/5194184.html

数论倒数,又称逆元(因为我说习惯逆元了,下面我都说逆元)

数论中的倒数是有特别的意义滴

你以为a的倒数在数论中还是1/a吗

(・∀・)哼哼~天真

 

先来引入求余概念

 

(a +  b) % p = (a%p +  b%p) %p  (对)

(a  -  b) % p = (a%p  -  b%p) %p  (对)

(a  *  b) % p = (a%p *  b%p) %p  (对)

(a  /  b) % p = (a%p  /  b%p) %p  (错)

 

为什么除法错的

证明是对的难,证明错的只要举一个反例

(100/50)%20 = 2       ≠      (100%20) / (50%20) %20 = 0

 

对于一些题目,我们必须在中间过程中进行求余,否则数字太大,电脑存不下,那如果这个算式中出现除法,我们是不是对这个算式就无法计算了呢?

答案当然是 NO (>o<)

 

这时就需要逆元了

转载于:https://www.cnblogs.com/bruce27/p/5528282.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值