hdu-5698 瞬间移动(数论+快速幂)

题目链接:

瞬间移动

Problem Description
 

有一个无限大的矩形,初始时你在左上角(即第一行第一列),每次你都可以选择一个右下方格子,并瞬移过去(如从下图中的红色格子能直接瞬移到蓝色格子),求到第nn行第mm列的格子有几种方案,答案对1000000007取模。

https://i-blog.csdnimg.cn/blog_migrate/6be27c7c8f0609e142553195f49b804c.jpeg

 
Input
 

多组测试数据。

两个整数n,m(2<= n,m<=100000)

 

Output
 

一个整数表示答案

 

Sample Input
 
4 5
 
Sample Output
 
10

题意:



思路:

跟那个只能向下和向右走有多少种方案差不多,最后的结果就是一个那个是同一个组合数,用快速幂幂和费马小定理搞;

AC代码
#include <bits/stdc++.h>
/*
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio>
*/
using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=0x3f3f3f3f;
const int N=2e5+25;
int n,m;
LL dp[2*N];
int Init()
{
    dp[1]=1;
    for(int i=2;i<2*N;i++)
    {
        dp[i]=dp[i-1]*(LL)i;
        dp[i]%=mod;
    }
}
LL fastpow(LL x,LL y)
{
    LL ans=1,base=x;
    while(y)
    {
        if(y&1)
        {
            ans*=base;
            ans%=mod;
        }
        base*=base;
        base%=mod;
        y>>=1;
    }
    return ans;
}
int main()
{
        Init();
       while(scanf("%d%d",&n,&m)!=EOF)
       {
           if(n==2||m==2)cout<<"1"<<"\n";
           else
           {
           n-=2;
           m-=2;
           LL x=dp[n],y=dp[m];
           LL ans=dp[n+m]*fastpow(x*y%mod,mod-2)%mod;
           cout<<ans<<"\n";
           }
       }


    return 0;
}

 




转载于:https://www.cnblogs.com/zhangchengc919/p/5524406.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值